Skip to content

Commit

Permalink
Rewrite in OOP + add compact terminal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 4, 2023
1 parent fa19e06 commit 5947ced
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 64 deletions.
21 changes: 15 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ export const FN_SYNC = () => {
export const OPTIONS: Options = {
cpu: {
chunkSize: 100,
compareSize: 10,
rangePercent: 10,
compareSize: 25,
rangePercent: 1,
},
ram: {
chunkSize: 5,
compareSize: 5,
rangePercent: 5,
rangePercent: 1,
},
offset: {
allow: true,
rangePercent: 5,
rangePercent: 1,
},
gc: {
allow: true,
Expand All @@ -33,9 +33,8 @@ export const OPTIONS: Options = {

// Default offset
export const OFFSET: Offset = {
min: 0,
max: 0,
median: 0,
deviation: 0,
cycles: 0,
};

Expand Down Expand Up @@ -77,3 +76,13 @@ export const GLOBAL: {
options: OPTIONS,
stores: STORES,
};

export const IS_NODE = typeof process !== "undefined";

export const TIME_UNIT = IS_NODE ? "ns" : "ms";

export const NS_IN_SECOND = 1_000_000_000;

export const MS_IN_SECOND = 1_000;

export const UNITS_IN_SECOND = IS_NODE ? NS_IN_SECOND : MS_IN_SECOND;
30 changes: 21 additions & 9 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
import { eve } from "ueve/async";
import { Name, Offset, Offsets } from "./types";
import { Offset, Offsets } from "./types";

// Before suite gets run
export const $suiteBefore = eve<{ suite: Name; benchmarks: string[] }>();
export const $suiteBefore = eve<{
suiteName: string;
benchmarkNames: string[];
}>();

// After suite offsets get calculated
export const $suiteOffsets = eve<{
suite: Name;
suiteName: string;
offsets: Offsets;
}>();

// After suite gets run
export const $suiteAfter = eve<{ suite: Name }>();
export const $suiteAfter = eve<{ suiteName: string }>();

// Before benchmark of one type gets run
export const $benchmarkBeforeAll = eve<{ suite: Name; benchmark: Name }>();
export const $benchmarkBeforeAll = eve<{
suiteName: string;
benchmarkName: string;
}>();

// After benchmark of one type gets run
export const $benchmarkAfterAll = eve<{
suite: Name;
benchmark: Name;
suiteName: string;
benchmarkName: string;
cpu: Offset;
ram: Offset;
}>();

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

// After each benchmark of one type gets run
export const $benchmarkAfterEach = eve<{ benchmark: Name }>();
export const $benchmarkAfterEach = eve<{
suiteName: string;
benchmarkName: string;
}>();

export { sub, clr, has } from "ueve/async";
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./types";
export * from "./preset";
export * from "./suite";
export * from "./events";
export * from "./modes";
1 change: 1 addition & 0 deletions src/modes/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./terminal";
export * from "./terminalCompact";
61 changes: 19 additions & 42 deletions src/modes/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sub } from "ueve/async";
import { Either, Noop } from "elfs";
import { red, green, bold, gray, blue, cyan } from "chalk";
import {
$suiteBefore,
Expand All @@ -9,38 +8,34 @@ import {
} from "../events";
import { Offset } from "../types";
import { newLine, writeLine } from "../utils";

let suiteStart: Either<[null, Noop]> = null;
let suiteEnd: Either<[null, Noop]> = null;
let benchmarkStart: Either<[null, Noop]> = null;
let benchmarkEnd: Either<[null, Noop]> = null;
import { MS_IN_SECOND, TIME_UNIT, UNITS_IN_SECOND } from "../constants";

/**
* Listens to events and prints suite and benchmark results into a terminal.
*
* @example
* ```ts
* // subscribe to events
* await useTerminal();
* useTerminal();
*
* // run suite which publishes data to the events
* await runBenchmarks();
* await testSuite.run();
* ```
* */
export async function useTerminal() {
let results: { name: string; cpu: Offset; ram: Offset }[] = [];
let longestBenchmarkName = 0;

suiteStart ??= sub($suiteBefore, async ({ suite, benchmarks }) => {
sub($suiteBefore, async ({ suiteName, benchmarkNames }) => {
results = [];
longestBenchmarkName = benchmarks.sort((a, b) => b.length - a.length)[0]
longestBenchmarkName = benchmarkNames.sort((a, b) => b.length - a.length)[0]
.length;

writeLine(bold.underline(`${suite[1]}:`));
writeLine(bold.underline(`${suiteName}:`));
newLine();
});

suiteEnd ??= sub($suiteAfter, async () => {
sub($suiteAfter, async () => {
newLine();
writeLine(
`=> Slowest is ${red.bold.underline(
Expand All @@ -57,36 +52,22 @@ export async function useTerminal() {
newLine();
});

benchmarkStart ??= sub($benchmarkBeforeAll, async ({ benchmark }) => {
const name = benchmark[1];

sub($benchmarkBeforeAll, async ({ benchmarkName }) => {
newLine();
writeLine(bold(name));
writeLine(bold(benchmarkName));
});

benchmarkEnd ??= sub($benchmarkAfterAll, async ({ benchmark, cpu, ram }) => {
const name = benchmark[1].padEnd(longestBenchmarkName);
sub($benchmarkAfterAll, async ({ benchmarkName, cpu, ram }) => {
const name = benchmarkName.padEnd(longestBenchmarkName);

const isCpuZero = cpu.median === 0;
const ops = (
isCpuZero ? 1_000_000_000 : (1_000_000_000 / cpu.median) | 0
cpu.median === 0 ? Infinity : UNITS_IN_SECOND / cpu.median
).toLocaleString();
const cpuMin = (isCpuZero ? 0 : cpu.min / cpu.median)
.toPrecision(1)
.toLocaleString();
const cpuMax = (isCpuZero ? 0 : cpu.median / cpu.max)
.toPrecision(1)
.toLocaleString();
const cpuDeviation = cpu.deviation.toPrecision(1).toLocaleString();
const cpuCycles = cpu.cycles.toLocaleString();

const isRamZero = ram.median === 0;
const kb = (ram.median | 0).toLocaleString();
const ramMin = (isRamZero ? 0 : ram.min / ram.median)
.toPrecision(1)
.toLocaleString();
const ramMax = (isRamZero ? 0 : ram.median / ram.max)
.toPrecision(1)
.toLocaleString();
const ramDeviation = ram.deviation.toPrecision(1).toLocaleString();
const ramCycles = ram.cycles.toLocaleString();

results.push({
Expand All @@ -95,24 +76,20 @@ export async function useTerminal() {
ram,
});

const cpuSecondaryInfo = gray(
`[-${cpuMin}, +${cpuMax}]% (${cpuCycles} cycles)`,
);
const cpuSecondaryInfo = gray(${cpuDeviation}% x${cpuCycles}`);
const cpuTime = gray(
`(${
cpu.median < 1_000_000
TIME_UNIT === "ns" && cpu.median < 1_000_000
? `${cpu.median.toLocaleString()} ns`
: `${(cpu.median / 1_000_000).toPrecision(3)} ms`
: `${(cpu.median / MS_IN_SECOND).toPrecision(3)} ms`
})`,
);
const ramSecondaryInfo = gray(
`[-${ramMin}, +${ramMax}]% (${ramCycles} cycles)`,
);
const ramSecondaryInfo = gray(${ramDeviation}% x${ramCycles}`);

writeLine(`${bold(name)} ${blue(ops)} op/s ${cpuTime} ${cpuSecondaryInfo}`);
newLine();
writeLine(
`${"".padEnd(longestBenchmarkName)} ${cyan(kb)} Kb ${ramSecondaryInfo}`,
`${"".padEnd(longestBenchmarkName)} ${cyan(kb)} kB ${ramSecondaryInfo}`,
);
newLine();
});
Expand Down
46 changes: 46 additions & 0 deletions src/modes/terminalCompact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { sub } from "ueve/async";
import { bold, gray, blue, cyan } from "chalk";
import { MS_IN_SECOND, TIME_UNIT, UNITS_IN_SECOND } from "../constants";
import { $benchmarkAfterAll } from "../events";

/**
* Listens to events and prints suite and benchmark results into a terminal.
*
* @example
* ```ts
* // subscribe to events
* useTerminalCompact();
*
* // run suite which publishes data to the events
* await testSuite.run();
* ```
* */
export function useTerminalCompact() {
sub($benchmarkAfterAll, async ({ benchmarkName, cpu, ram }) => {
const ops = (
cpu.median === 0 ? Infinity : Math.round(UNITS_IN_SECOND / cpu.median)
).toLocaleString();
const cpuDeviation = cpu.deviation.toPrecision(1).toLocaleString();
const cpuCycles = cpu.cycles.toLocaleString();
const kb = Math.round(ram.median).toLocaleString();
const ramDeviation = ram.deviation.toPrecision(1).toLocaleString();
const ramCycles = ram.cycles.toLocaleString();
const cpuSecondaryInfo = gray(${cpuDeviation}% x${cpuCycles}`);
const cpuTime = gray(
`(${
TIME_UNIT === "ns" && cpu.median < 1_000_000
? `${cpu.median.toLocaleString()} ns`
: `${(cpu.median / MS_IN_SECOND).toPrecision(3)} ms`
})`,
);
const ramSecondaryInfo = gray(${ramDeviation}% x${ramCycles}`);

console.log(
`${bold(benchmarkName)} ${blue(
ops,
)} op/s ${cpuTime} ${cpuSecondaryInfo} | ${cyan(
kb,
)} kB ${ramSecondaryInfo}`,
);
});
}
Loading

0 comments on commit 5947ced

Please sign in to comment.