diff --git a/src/events.ts b/src/events.ts index a6e07af..3d8bee4 100644 --- a/src/events.ts +++ b/src/events.ts @@ -3,22 +3,31 @@ import { Offset, Offsets } from "./types"; export type Name = [symbol, string]; -// Suite -export const $suiteStart = eve<{ suite: Name; benchmarks: string[] }>(); +// Before suite gets run +export const $suiteBefore = eve<{ suite: Name; benchmarks: string[] }>(); +// After suite offsets get calculated export const $suiteOffsets = eve<{ suite: Name; offsets: Offsets; }>(); -export const $suiteEnd = eve<{ suite: Name }>(); +// After suite gets run +export const $suiteAfter = eve<{ suite: Name }>(); -// Benchmark -export const $benchmarkStart = eve<{ suite: Name; benchmark: Name }>(); +// Before benchmark of one type gets run +export const $benchmarkBeforeAll = eve<{ suite: Name; benchmark: Name }>(); -export const $benchmarkEnd = eve<{ +// After benchmark of one type gets run +export const $benchmarkAfterAll = eve<{ suite: Name; benchmark: Name; cpu: Offset; ram: Offset; }>(); + +// Before each benchmark of one type gets run +export const $benchmarkBeforeEach = eve(); + +// After each benchmark of one type gets run +export const $benchmarkAfterEach = eve(); diff --git a/src/modes/terminal.ts b/src/modes/terminal.ts index 9a1d32a..165a8fa 100644 --- a/src/modes/terminal.ts +++ b/src/modes/terminal.ts @@ -2,10 +2,10 @@ import { sub } from "ueve/async"; import { Either, Noop } from "elfs"; import { red, green, bold, gray, blue, cyan } from "chalk"; import { - $suiteStart, - $suiteEnd, - $benchmarkEnd, - $benchmarkStart, + $suiteBefore, + $suiteAfter, + $benchmarkAfterAll, + $benchmarkBeforeAll, } from "../events"; import { Offset } from "../types"; import { newLine, writeLine } from "../utils"; @@ -19,7 +19,7 @@ export async function useTerminal() { let results: { name: string; cpu: Offset; ram: Offset }[] = []; let longestBenchmarkName = 0; - suiteStart ??= sub($suiteStart, async ({ suite, benchmarks }) => { + suiteStart ??= sub($suiteBefore, async ({ suite, benchmarks }) => { results = []; longestBenchmarkName = benchmarks.sort((a, b) => b.length - a.length)[0] .length; @@ -28,7 +28,7 @@ export async function useTerminal() { newLine(); }); - suiteEnd ??= sub($suiteEnd, async () => { + suiteEnd ??= sub($suiteAfter, async () => { newLine(); writeLine( `=> Slowest is ${red.bold.underline( @@ -45,14 +45,14 @@ export async function useTerminal() { newLine(); }); - benchmarkStart ??= sub($benchmarkStart, async ({ benchmark }) => { + benchmarkStart ??= sub($benchmarkBeforeAll, async ({ benchmark }) => { const name = benchmark[1]; newLine(); writeLine(bold(name)); }); - benchmarkEnd ??= sub($benchmarkEnd, async ({ benchmark, cpu, ram }) => { + benchmarkEnd ??= sub($benchmarkAfterAll, async ({ benchmark, cpu, ram }) => { const name = benchmark[1].padEnd(longestBenchmarkName); const isCpuZero = cpu.median === 0; diff --git a/src/preset.ts b/src/preset.ts index 5e9b726..dac1eb7 100644 --- a/src/preset.ts +++ b/src/preset.ts @@ -5,11 +5,11 @@ import { getOptions } from "./utils"; import { DeepPartial, Options, Benchmarks } from "./types"; import { stats } from "./stats"; import { - $benchmarkEnd, - $benchmarkStart, - $suiteEnd, + $benchmarkAfterAll, + $benchmarkBeforeAll, + $suiteAfter, $suiteOffsets, - $suiteStart, + $suiteBefore, Name, } from "./events"; import { pub } from "ueve/async"; @@ -25,7 +25,7 @@ export function preset(partialOptions?: DeepPartial) { const suite = [Symbol(), suiteName] as Name; return async function runSuite() { - await pub($suiteStart, { suite, benchmarks: Object.keys(benchmarks) }); + await pub($suiteBefore, { suite, benchmarks: Object.keys(benchmarks) }); GLOBAL.stores = stores; GLOBAL.options = options; @@ -37,14 +37,14 @@ export function preset(partialOptions?: DeepPartial) { for (const benchmarkName in benchmarks) { const benchmark = [Symbol(), benchmarkName] as Name; - await pub($benchmarkStart, { suite, benchmark }); + await pub($benchmarkBeforeAll, { suite, benchmark }); // We GC here so memory from one benchmark doesn't leak to the next one GLOBAL.options.gc.allow && global.gc?.(); const fn = benchmarks[benchmarkName]; - await pub($benchmarkEnd, { + await pub($benchmarkAfterAll, { suite, benchmark, cpu: await stats(fn, "cpu", offsets), @@ -52,7 +52,7 @@ export function preset(partialOptions?: DeepPartial) { }); } - await pub($suiteEnd, { suite }); + await pub($suiteAfter, { suite }); }; }; } diff --git a/src/run.ts b/src/run.ts index 7c0ac5e..d6d0a8e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,10 +1,14 @@ +import {pub} from "ueve/async"; import { GLOBAL } from "./constants"; +import {$benchmarkAfterEach, $benchmarkBeforeEach} from "./events"; import { RunData } from "./types"; export async function run({ benchmark, mode, type }: RunData) { const isAsync = type === "async"; const store = GLOBAL.stores[mode].chunk; + pub($benchmarkBeforeEach, null); + if (mode === "cpu") { const start = process.hrtime.bigint(); @@ -24,4 +28,6 @@ export async function run({ benchmark, mode, type }: RunData) { store.array[++store.index] = Math.round(Number(end - start)); } + + pub($benchmarkAfterEach, null); }