Skip to content

Commit

Permalink
Pass benchmark name into benchmark(Before/After)Each
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 2, 2023
1 parent 4681af6 commit a7c6adb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const $benchmarkAfterAll = eve<{
}>();

// Before each benchmark of one type gets run
export const $benchmarkBeforeEach = eve<null>();
export const $benchmarkBeforeEach = eve<{ benchmark: Name }>();

// After each benchmark of one type gets run
export const $benchmarkAfterEach = eve<null>();
export const $benchmarkAfterEach = eve<{ benchmark: Name }>();
4 changes: 2 additions & 2 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export function preset(partialOptions?: DeepPartial<Options>) {
await pub($benchmarkAfterAll, {
suite,
benchmark,
cpu: await stats(fn, "cpu", offsets),
ram: await stats(fn, "ram", offsets),
cpu: await stats(benchmark, fn, "cpu", offsets),
ram: await stats(benchmark, fn, "ram", offsets),
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {pub} from "ueve/async";
import { pub } from "ueve/async";
import { GLOBAL } from "./constants";
import {$benchmarkAfterEach, $benchmarkBeforeEach} from "./events";
import { $benchmarkAfterEach, $benchmarkBeforeEach, Name } from "./events";
import { RunData } from "./types";

export async function run({ benchmark, mode, type }: RunData) {
export async function run(name: Name, { benchmark, mode, type }: RunData) {
const isAsync = type === "async";
const store = GLOBAL.stores[mode].chunk;

pub($benchmarkBeforeEach, null);
await pub($benchmarkBeforeEach, { benchmark: name });

if (mode === "cpu") {
const start = process.hrtime.bigint();
Expand All @@ -29,5 +29,5 @@ export async function run({ benchmark, mode, type }: RunData) {
store.array[++store.index] = Math.round(Number(end - start));
}

pub($benchmarkAfterEach, null);
await pub($benchmarkAfterEach, { benchmark: name });
}
4 changes: 3 additions & 1 deletion src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { GLOBAL } from "./constants";
import { getMedian, getMinMax, positive } from "./utils";
import { run } from "./run";
import { Benchmark, Mode, Offsets } from "./types";
import { Name } from "./events";

export async function stats(
name: Name,
benchmark: Benchmark,
mode: Mode,
offsets: Offsets,
Expand Down Expand Up @@ -37,7 +39,7 @@ export async function stats(
main.index = 0;
}

await run({ benchmark, mode, type });
await run(name, { benchmark, mode, type });
}

const { array, index } = main;
Expand Down

0 comments on commit a7c6adb

Please sign in to comment.