Skip to content

Commit

Permalink
Add and rename events
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 2, 2023
1 parent 031e5f1 commit 2ac847c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
21 changes: 15 additions & 6 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null>();

// After each benchmark of one type gets run
export const $benchmarkAfterEach = eve<null>();
16 changes: 8 additions & 8 deletions src/modes/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -28,7 +28,7 @@ export async function useTerminal() {
newLine();
});

suiteEnd ??= sub($suiteEnd, async () => {
suiteEnd ??= sub($suiteAfter, async () => {
newLine();
writeLine(
`=> Slowest is ${red.bold.underline(
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,7 +25,7 @@ export function preset(partialOptions?: DeepPartial<Options>) {
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;
Expand All @@ -37,22 +37,22 @@ export function preset(partialOptions?: DeepPartial<Options>) {
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),
ram: await stats(fn, "ram", offsets),
});
}

await pub($suiteEnd, { suite });
await pub($suiteAfter, { suite });
};
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -24,4 +28,6 @@ export async function run({ benchmark, mode, type }: RunData) {

store.array[++store.index] = Math.round(Number(end - start));
}

pub($benchmarkAfterEach, null);
}

0 comments on commit 2ac847c

Please sign in to comment.