Loading...
Loading...
Use this skill when writing or running performance benchmarks for Jazz packages. Covers cronometro setup, file conventions, gotchas with worker threads, and how to compare implementations.
npx skill4agent add garden-co/jazz benchmarkingjazz-performancebench/bench/
├── package.json # Dependencies: cronometro, cojson, jazz-tools, vitest
├── jazz-tools/ # jazz-tools benchmarks
│ └── *.bench.ts<subject>.<operation>.bench.ts@latest@workspacecomap.create.jazz-tools.bench.tsfilestream.getChunks.bench.tsfilestream.asBase64.bench.tsbinaryCoStream.write.bench.tsimport cronometro from "cronometro";
const TOTAL_BYTES = 5 * 1024 * 1024;
let data: SomeType;
await cronometro(
{
"operation - @latest": {
async before() {
// Setup — runs once before the test iterations
data = prepareTestData(TOTAL_BYTES);
},
test() {
// The code being benchmarked — runs many times
latestImplementation(data);
},
async after() {
// Cleanup — runs once after all iterations
cleanup();
},
},
"operation - @workspace": {
async before() {
data = prepareTestData(TOTAL_BYTES);
},
test() {
workspaceImplementation(data);
},
async after() {
cleanup();
},
},
},
{
iterations: 50,
warmup: true,
print: {
colors: true,
compare: true,
},
onTestError: (testName: string, error: unknown) => {
console.error(`\nError in test "${testName}":`);
console.error(error);
},
},
);cronometro()import cronometro from "cronometro";
const TOTAL_BYTES = 5 * 1024 * 1024;
let data: InputType;
await cronometro(
{
"operationName - @latest": {
async before() {
data = generateInput(TOTAL_BYTES);
},
test() {
latestImplementation(data);
},
async after() {
cleanup();
},
},
"operationName - @workspace": {
async before() {
data = generateInput(TOTAL_BYTES);
},
test() {
workspaceImplementation(data);
},
async after() {
cleanup();
},
},
},
{
iterations: 50,
warmup: true,
print: { colors: true, compare: true },
onTestError: (testName: string, error: unknown) => {
console.error(`\nError in test "${testName}":`);
console.error(error);
},
},
);getChunksasBase64write@latest@workspaceconst TOTAL_BYTES = 5 * 1024 * 1024"operation - @implementation"bench/package.json{
"dependencies": {
"cojson": "workspace:*",
"cojson-latest": "npm:cojson@0.20.7",
"jazz-tools": "workspace:*",
"jazz-tools-latest": "npm:jazz-tools@0.20.7"
}
}pnpm installbench/import * as localTools from "jazz-tools";
import * as latestPublishedTools from "jazz-tools-latest";
import { WasmCrypto as LocalWasmCrypto } from "cojson/crypto/WasmCrypto";
import { WasmCrypto as LatestPublishedWasmCrypto } from "cojson-latest/crypto/WasmCrypto";@ts-expect-errorctx = await createContext(
// @ts-expect-error version mismatch
latestPublishedTools,
LatestPublishedWasmCrypto,
);async function createContext(tools: typeof localTools, wasmCrypto: typeof LocalWasmCrypto) {
const ctx = await tools.createJazzContextForNewAccount({
creationProps: { name: "Bench Account" },
peers: [],
crypto: await wasmCrypto.create(),
sessionProvider: new tools.MockSessionProvider(),
});
return { account: ctx.account, node: ctx.node };
}peers: []MockSessionProvider(ctx.node as any).gracefulShutdown()after()beforeconst TOTAL_BYTES = 5 * 1024 * 1024; // 5MB
let chunks: Uint8Array[];
await cronometro({
"operationName - @workspace": {
async before() {
chunks = makeChunks(TOTAL_BYTES, CHUNK_SIZE);
},
test() {
doWork(chunks);
},
},
}, options);beforebench/package.json{
"scripts": {
"bench:mytest": "node --experimental-strip-types --no-warnings ./jazz-tools/mytest.jazz-tools.bench.ts"
}
}bench/cd bench
pnpm run bench:mytestnode --experimental-strip-typestsxnode --experimental-strip-types --no-warnings"bench:foo": "node --experimental-strip-types --no-warnings ./jazz-tools/foo.bench.ts"beforeafterasync// BAD — test never starts, benchmark hangs
{
before() {
data = generateInput(); // sync, no callback, no promise
},
test() { ... },
}
// GOOD — async function returns a Promise
{
async before() {
data = generateInput();
},
test() { ... },
}
// ALSO GOOD — callback style
{
before(cb: () => void) {
data = generateInput();
cb();
},
test() { ... },
}test()beforeaftertestasync--experimental-strip-typesas!enumconstnamespaceconstructor(private x: number)import =export =getChunks()import cronometro from "cronometro";
import * as localTools from "jazz-tools";
import * as latestPublishedTools from "jazz-tools-latest";
import { WasmCrypto as LocalWasmCrypto } from "cojson/crypto/WasmCrypto";
import { cojsonInternals } from "cojson";
import { WasmCrypto as LatestPublishedWasmCrypto } from "cojson-latest/crypto/WasmCrypto";
const CHUNK_SIZE = cojsonInternals.TRANSACTION_CONFIG.MAX_RECOMMENDED_TX_SIZE;
const TOTAL_BYTES = 5 * 1024 * 1024;
function makeChunks(totalBytes: number, chunkSize: number): Uint8Array[] {
const chunks: Uint8Array[] = [];
let remaining = totalBytes;
while (remaining > 0) {
const size = Math.min(chunkSize, remaining);
const chunk = new Uint8Array(size);
for (let i = 0; i < size; i++) {
chunk[i] = Math.floor(Math.random() * 256);
}
chunks.push(chunk);
remaining -= size;
}
return chunks;
}
type Tools = typeof localTools;
async function createContext(tools: Tools, wasmCrypto: typeof LocalWasmCrypto) {
const ctx = await tools.createJazzContextForNewAccount({
creationProps: { name: "Bench Account" },
peers: [],
crypto: await wasmCrypto.create(),
sessionProvider: new tools.MockSessionProvider(),
});
return { account: ctx.account, node: ctx.node, FileStream: tools.FileStream };
}
function populateStream(ctx: Awaited<ReturnType<typeof createContext>>, chunks: Uint8Array[]) {
let totalBytes = 0;
for (const c of chunks) totalBytes += c.length;
const stream = ctx.FileStream.create({ owner: ctx.account });
stream.start({ mimeType: "application/octet-stream", totalSizeBytes: totalBytes });
for (const chunk of chunks) stream.push(chunk);
stream.end();
return stream;
}
const benchOptions = {
iterations: 50,
warmup: true,
print: { colors: true, compare: true },
onTestError: (testName: string, error: unknown) => {
console.error(`\nError in test "${testName}":`);
console.error(error);
},
};
let readCtx: Awaited<ReturnType<typeof createContext>>;
let readStream: ReturnType<typeof populateStream>;
await cronometro(
{
"getChunks - @latest": {
async before() {
readCtx = await createContext(
// @ts-expect-error version mismatch
latestPublishedTools,
LatestPublishedWasmCrypto,
);
readStream = populateStream(readCtx, makeChunks(TOTAL_BYTES, CHUNK_SIZE));
},
test() {
readStream.getChunks();
},
async after() {
(readCtx.node as any).gracefulShutdown();
},
},
"getChunks - @workspace": {
async before() {
readCtx = await createContext(localTools, LocalWasmCrypto);
readStream = populateStream(readCtx, makeChunks(TOTAL_BYTES, CHUNK_SIZE));
},
test() {
readStream.getChunks();
},
async after() {
(readCtx.node as any).gracefulShutdown();
},
},
},
benchOptions,
);filestream.getChunks.bench.tscronometro()@latest@workspaceconst TOTAL_BYTES = 5 * 1024 * 1024bench/jazz-tools/*.bench.tsbench/package.jsonnode --experimental-strip-types --no-warningsbeforeafterasynciterationswarmup: trueonTestError"operation - @implementation""getChunks - @workspace"bench/package.jsonpnpm installgracefulShutdown()after()before()test()