Skip to content

Commit

Permalink
Rename a bunch of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 5, 2023
1 parent 5947ced commit 6d86917
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 87 deletions.
57 changes: 14 additions & 43 deletions src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import {
Options,
Stores,
Type,
Offset,
Benchmarks,
Events,
} from "./types";
import { getMedian } from "./utils/getMedian";
import { getOptions } from "./utils/getOptions";
import { positive } from "./utils/positive";
import { getChunkDeviation } from "./utils/getChunkDeviation";
import { now } from "./utils/now";
import { collectGarbage } from "./utils/collectGarbage";
import { getMedian, clampToZero, getOptions, getChunkDeviation , now, collectGarbage } from "./utils";
import { pub } from "ueve/async";
import {
$benchmarkAfterAll,
Expand All @@ -26,30 +22,8 @@ import {
$suiteBefore,
} from "./events";

type Events = Partial<{
beforeOne: Fn<[], Promise<void>>;
afterOne: Fn<[], Promise<void>>;
beforeAll: Fn<[], Promise<void>>;
afterAll: Fn<
[
{
cpu: Offset;
ram: Offset;
},
],
Promise<void>
>;
}>;

type Benchmarks<$Data> = Record<
string,
{
benchmark: Benchmark<$Data>;
events: Events;
}
>;

class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
private _name: string;
private _options: Options;
private _setup: Fn<[], $Data>;
private _before: Fn<[$Data], Promise<void>>;
Expand All @@ -59,9 +33,8 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
private _offsets: Offsets;
private _stores: Stores;

public name: string;

constructor(name: string, options?: DeepPartial<Options>) {
this._name = name;
this._options = getOptions(options);
this._setup = () => null as $Data;
this._before = FN_ASYNC;
Expand All @@ -79,8 +52,6 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
main: this._createStore("ram"),
},
};

this.name = name;
}

public setup<$Type extends $Data>(setup: Fn<[], $Type>) {
Expand All @@ -89,7 +60,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
return this as unknown as Suite<$Type, $Benchmarks>;
}

public before(before: Fn<[$Data], Promise<void>>) {
public before(before: Fn<[], Promise<void>>) {
this._before = before;

return this;
Expand Down Expand Up @@ -126,7 +97,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {

await this._before(this._data);
await pub($suiteBefore, {
suiteName: this.name,
suiteName: this._name,
benchmarkNames: Object.keys(this._benchmarks),
});

Expand All @@ -144,7 +115,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
for (const name in this._benchmarks) {
await this._benchmarks[name]?.events?.beforeAll?.();
await pub($benchmarkBeforeAll, {
suiteName: this.name,
suiteName: this._name,
benchmarkName: name,
});

Expand All @@ -160,15 +131,15 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {
ram,
});
await pub($benchmarkAfterAll, {
suiteName: this.name,
suiteName: this._name,
benchmarkName: name,
cpu,
ram,
});
}

await this._after(this._data);
await pub($suiteAfter, { suiteName: this.name });
await pub($suiteAfter, { suiteName: this._name });
}

private async _getOffset(type: Type, mode: Mode) {
Expand Down Expand Up @@ -243,7 +214,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {

const { array, index } = main;
const offset = offsets[type][mode];
const median = positive(getMedian(array, index) - offset.median);
const median = clampToZero(getMedian(array, index) - offset.median);
const cycles = main.index * chunkSize;
const deviation = getChunkDeviation(median, array, index - compareSize);

Expand All @@ -265,7 +236,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {

await this._benchmarks[name]?.events?.beforeOne?.();
await pub($benchmarkBeforeEach, {
suiteName: this.name,
suiteName: this._name,
benchmarkName: name,
});

Expand All @@ -281,7 +252,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {

await this._benchmarks[name]?.events?.afterOne?.();
await pub($benchmarkAfterEach, {
suiteName: this.name,
suiteName: this._name,
benchmarkName: name,
});
} else {
Expand All @@ -298,7 +269,7 @@ class Suite<$Data, $Benchmarks extends Benchmarks<$Data>> {

await this._benchmarks[name]?.events?.afterOne?.();
await pub($benchmarkAfterEach, {
suiteName: this.name,
suiteName: this._name,
benchmarkName: name,
});
}
Expand Down
68 changes: 35 additions & 33 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Either, Fn } from "elfs";

// Record wrapper that provides default types
// Mainly used to make the code more readable (especially in generics)
export type Rec<
$Input extends Either<[string, number, symbol]> = string,
$Output = unknown,
> = Record<$Input, $Output>;
export type NumberArray =
| Uint8Array
| Uint8ClampedArray
| Int8Array
| Uint16Array
| Int16Array
| Uint32Array
| Int32Array
| Float32Array
| Float64Array
| number[];

// Option object used in `preset` to configure the suite
export type Options = {
Expand Down Expand Up @@ -55,17 +60,37 @@ export type Options = {
};

// Recursively make all properties of an object optional
export type DeepPartial<$Object extends Rec<string, Rec>> = Partial<{
[$Key in keyof $Object]: $Object[$Key] extends Rec
export type DeepPartial<$Object extends Record<string, Record<string, unknown>>> = Partial<{
[$Key in keyof $Object]: $Object[$Key] extends Record<string, unknown>
? Partial<$Object[$Key]>
: $Object[$Key];
}>;

// Benchmark function
export type Benchmark<$Data = any> = Fn<[$Data], Either<[void, Promise<void>]>>;

// Object containing all benchmark functions
export type Benchmarks = Record<string, Benchmark>;
export type Events = Partial<{
beforeOne: Fn<[], Promise<void>>;
afterOne: Fn<[], Promise<void>>;
beforeAll: Fn<[], Promise<void>>;
afterAll: Fn<
[
{
cpu: Offset;
ram: Offset;
},
],
Promise<void>
>;
}>;

export type Benchmarks<$Data> = Record<
string,
{
benchmark: Benchmark<$Data>;
events: Events;
}
>;

// Store used to collect data
export type Store = {
Expand All @@ -91,16 +116,6 @@ export type Mode = Either<["cpu", "ram"]>;
// Offset type
export type Type = Either<["sync", "async"]>;

// Offset type and mode
export type TypeMode = {
type: Type;
mode: Mode;
};

export type RunData = {
benchmark: Benchmark;
} & TypeMode;

// Offset data
export type Offset = {
median: number;
Expand All @@ -119,16 +134,3 @@ export type Offsets = {
ram: Offset;
};
};

// Results data
export type Results = {
name: string;
cpu: Offset;
ram: Offset;
};

// Suite function
export type Suite = Fn<[], AsyncGenerator<Results>>;

// Tuple of Suite/Benchmark symbol and name
export type Name = [symbol, string];
3 changes: 3 additions & 0 deletions src/utils/clampToZero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function clampToZero(value: number) {
return value < 0 ? 0 : value;
}
6 changes: 4 additions & 2 deletions src/utils/getMedian.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function getMedian(array: Uint32Array | number[], length: number) {
import {NumberArray} from "../types";

export function getMedian(array: NumberArray, length: number) {
return array
.slice(0, length)
.sort((a, b) => a - b)
.sort((a: number, b: number) => a - b)
.at(Math.floor(length / 2)) as number;
}
10 changes: 5 additions & 5 deletions src/utils/getOptions.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { OPTIONS } from "../constants";
import { DeepPartial, Options } from "../types";

export function getOptions(partialOptions?: DeepPartial<Options>) {
export function getOptions(options?: DeepPartial<Options>) {
return {
cpu: {
...OPTIONS.cpu,
...(partialOptions?.cpu || {}),
...(options?.cpu || {}),
},
ram: {
...OPTIONS.ram,
...(partialOptions?.ram || {}),
...(options?.ram || {}),
},
offset: {
...OPTIONS.offset,
...(partialOptions?.offset || {}),
...(options?.offset || {}),
},
gc: {
...OPTIONS.gc,
...(partialOptions?.gc || {}),
...(options?.gc || {}),
},
} satisfies Options;
}
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./getMedian";
export * from "./positive";
export * from "./clampToZero";
export * from "./getOptions";
export * from "./console";
export * from "./collectGarbage";
export * from "./now";
export * from "./getChunkDeviation";
3 changes: 0 additions & 3 deletions src/utils/positive.ts

This file was deleted.

0 comments on commit 6d86917

Please sign in to comment.