Loading...
Loading...
How to write benchmarks in Motoko using bench‑helper. Covers project setup (mops.toml), bench file layout in bench/*.bench.mo, the Bench.Schema rows/cols model, and safe patterns for encode/decode, hashing, crypto, and allocation benches.
npx skill4agent add research-ag/motoko-skills motoko-benchmarks-generationbench-helperrun(row, col)bench/*.bench.momops.tomlbench-helper[dev-dependencies]mo:basemo:coremo:basebench/.bench.mobench/base64.bench.momodule { ... }public func init() : Bench.V1initBench.SchemaBench.V1(schema, run)run : (rowIndex : Nat, colIndex : Nat) -> ()import Array "mo:core/Array";
import Text "mo:core/Text";
import Bench "mo:bench-helper";
module {
public func init() : Bench.V1 {
let schema : Bench.Schema = {
name = "My bench";
description = "What this bench measures";
rows = ["size 16", "size 64", "size 256"]; // your row labels
columns = ["operation A", "operation B"]; // your column labels
};
// Prepare inputs outside of `run` so they are not re-created on every iteration
let inputs : [[Nat8]] = [
Array.init<Nat8>(16, 0),
Array.init<Nat8>(64, 0),
Array.init<Nat8>(256, 0),
];
// Build a table of routines to measure: routines[row][col] : () -> ()
let routines : [[() -> ()]] = Array.tabulate(
rows.size(),
func(ri) {
let input = inputs[ri]; // capture precomputed input
[
func() { ignore input.size() }, // operation A @ inputs[ri]
func() { ignore input.toArray() }, // operation B @ inputs[ri]
]
},
);
// The runner calls this many times; keep it tiny and branch-free.
Bench.V1(schema, func(ri : Nat, ci : Nat) = routines[ri][ci]());
};
};namedescriptionrowscolsBench.V1(schema, run)run(rowIndex, colIndex)runignore ...routinesrows.size() x cols.size()routinesruninitrunignorerunbench-helpermops.toml.bench.momops installmops benchbench-helper