overtake 1.0.0-rc.2 → 1.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cli.cjs +7 -7
- package/build/cli.cjs.map +1 -1
- package/build/cli.js +1 -1
- package/build/cli.js.map +1 -1
- package/build/index.cjs +228 -11
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +65 -3
- package/build/index.js +187 -1
- package/build/index.js.map +1 -1
- package/build/reporter.cjs +1 -1
- package/build/reporter.cjs.map +1 -1
- package/build/reporter.js +1 -1
- package/build/reporter.js.map +1 -1
- package/examples/array-copy.ts +17 -0
- package/package.json +1 -1
- package/src/cli.ts +2 -2
- package/src/index.ts +232 -3
- package/src/reporter.ts +1 -1
- package/build/__tests__/runner.d.ts +0 -1
- package/build/benchmark.cjs +0 -237
- package/build/benchmark.cjs.map +0 -1
- package/build/benchmark.d.ts +0 -64
- package/build/benchmark.js +0 -189
- package/build/benchmark.js.map +0 -1
- package/overtakes/array-copy.md +0 -80
- package/overtakes/array-delete-element.md +0 -44
- package/overtakes/async.md +0 -25
- package/overtakes/class-vs-function.md +0 -31
- package/overtakes/postgres-vs-mongo.md +0 -55
- package/src/__tests__/runner.ts +0 -34
- package/src/benchmark.ts +0 -231
package/build/index.js
CHANGED
|
@@ -1,3 +1,189 @@
|
|
|
1
|
-
|
|
1
|
+
import { cpus } from 'node:os';
|
|
2
|
+
import { createExecutor } from "./executor.js";
|
|
3
|
+
import { DEFAULT_CYCLES } from "./types.js";
|
|
4
|
+
export const DEFAULT_WORKERS = cpus().length;
|
|
5
|
+
export const AsyncFunction = (async ()=>{}).constructor;
|
|
6
|
+
export const DEFAULT_REPORT_TYPES = [
|
|
7
|
+
'ops'
|
|
8
|
+
];
|
|
9
|
+
export class MeasureContext {
|
|
10
|
+
title;
|
|
11
|
+
run;
|
|
12
|
+
pre;
|
|
13
|
+
post;
|
|
14
|
+
constructor(title, run){
|
|
15
|
+
this.title = title;
|
|
16
|
+
this.run = run;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class Measure {
|
|
20
|
+
#ctx;
|
|
21
|
+
constructor(ctx){
|
|
22
|
+
this.#ctx = ctx;
|
|
23
|
+
}
|
|
24
|
+
pre(fn) {
|
|
25
|
+
this.#ctx.pre = fn;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
post(fn) {
|
|
29
|
+
this.#ctx.post = fn;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class TargetContext {
|
|
34
|
+
title;
|
|
35
|
+
setup;
|
|
36
|
+
teardown;
|
|
37
|
+
measures;
|
|
38
|
+
constructor(title, setup){
|
|
39
|
+
this.title = title;
|
|
40
|
+
this.setup = setup;
|
|
41
|
+
this.measures = [];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class Target {
|
|
45
|
+
#ctx;
|
|
46
|
+
constructor(ctx){
|
|
47
|
+
this.#ctx = ctx;
|
|
48
|
+
}
|
|
49
|
+
teardown(fn) {
|
|
50
|
+
this.#ctx.teardown = fn;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
measure(title, run) {
|
|
54
|
+
const measure = new MeasureContext(title, run);
|
|
55
|
+
this.#ctx.measures.push(measure);
|
|
56
|
+
return new Measure(measure);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class FeedContext {
|
|
60
|
+
title;
|
|
61
|
+
fn;
|
|
62
|
+
constructor(title, fn){
|
|
63
|
+
this.title = title;
|
|
64
|
+
this.fn = fn;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class Benchmark {
|
|
68
|
+
#targets = [];
|
|
69
|
+
#feeds = [];
|
|
70
|
+
#executed = false;
|
|
71
|
+
static create(title, fn) {
|
|
72
|
+
if (fn) {
|
|
73
|
+
return new Benchmark(title, fn);
|
|
74
|
+
} else {
|
|
75
|
+
return new Benchmark(title);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
constructor(title, fn){
|
|
79
|
+
if (fn) {
|
|
80
|
+
this.feed(title, fn);
|
|
81
|
+
} else {
|
|
82
|
+
this.feed(title);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
feed(title, fn) {
|
|
86
|
+
const self = this;
|
|
87
|
+
self.#feeds.push(fn ? new FeedContext(title, fn) : new FeedContext(title));
|
|
88
|
+
return self;
|
|
89
|
+
}
|
|
90
|
+
target(title, setup) {
|
|
91
|
+
const target = new TargetContext(title, setup);
|
|
92
|
+
this.#targets.push(target);
|
|
93
|
+
return new Target(target);
|
|
94
|
+
}
|
|
95
|
+
async execute({ workers = DEFAULT_WORKERS, warmupCycles = 20, maxCycles = DEFAULT_CYCLES, minCycles = 50, absThreshold = 1_000, relThreshold = 0.02, reportTypes = DEFAULT_REPORT_TYPES }) {
|
|
96
|
+
if (this.#executed) {
|
|
97
|
+
throw new Error("Benchmark is executed and can't be reused");
|
|
98
|
+
}
|
|
99
|
+
this.#executed = true;
|
|
100
|
+
const executor = createExecutor({
|
|
101
|
+
workers,
|
|
102
|
+
warmupCycles,
|
|
103
|
+
maxCycles,
|
|
104
|
+
minCycles,
|
|
105
|
+
absThreshold,
|
|
106
|
+
relThreshold,
|
|
107
|
+
reportTypes
|
|
108
|
+
});
|
|
109
|
+
const reports = [];
|
|
110
|
+
for (const target of this.#targets){
|
|
111
|
+
const targetReport = {
|
|
112
|
+
target: target.title,
|
|
113
|
+
measures: []
|
|
114
|
+
};
|
|
115
|
+
for (const measure of target.measures){
|
|
116
|
+
const measureReport = {
|
|
117
|
+
measure: measure.title,
|
|
118
|
+
feeds: []
|
|
119
|
+
};
|
|
120
|
+
for (const feed of this.#feeds){
|
|
121
|
+
const data = await feed.fn?.();
|
|
122
|
+
executor.push({
|
|
123
|
+
setup: target.setup,
|
|
124
|
+
teardown: target.teardown,
|
|
125
|
+
pre: measure.pre,
|
|
126
|
+
run: measure.run,
|
|
127
|
+
post: measure.post,
|
|
128
|
+
data
|
|
129
|
+
}).then((data)=>{
|
|
130
|
+
measureReport.feeds.push({
|
|
131
|
+
feed: feed.title,
|
|
132
|
+
data
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
targetReport.measures.push(measureReport);
|
|
137
|
+
}
|
|
138
|
+
reports.push(targetReport);
|
|
139
|
+
}
|
|
140
|
+
await executor.drain();
|
|
141
|
+
executor.kill();
|
|
142
|
+
return reports;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export const printSimpleReports = (reports)=>{
|
|
146
|
+
for (const report of reports){
|
|
147
|
+
for (const { measure, feeds } of report.measures){
|
|
148
|
+
console.group('\n', report.target, measure);
|
|
149
|
+
for (const { feed, data } of feeds){
|
|
150
|
+
const output = Object.entries(data).map(([key, report])=>`${key}: ${report.toString()}`).join('; ');
|
|
151
|
+
console.log(feed, output);
|
|
152
|
+
}
|
|
153
|
+
console.groupEnd();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
export const printTableReports = (reports)=>{
|
|
158
|
+
for (const report of reports){
|
|
159
|
+
for (const { measure, feeds } of report.measures){
|
|
160
|
+
console.log('\n', report.target, measure);
|
|
161
|
+
const table = {};
|
|
162
|
+
for (const { feed, data } of feeds){
|
|
163
|
+
table[feed] = Object.fromEntries(Object.entries(data).map(([key, report])=>[
|
|
164
|
+
key,
|
|
165
|
+
report.toString()
|
|
166
|
+
]));
|
|
167
|
+
}
|
|
168
|
+
console.table(table);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
export const printJSONReports = (reports, padding)=>{
|
|
173
|
+
const output = {};
|
|
174
|
+
for (const report of reports){
|
|
175
|
+
for (const { measure, feeds } of report.measures){
|
|
176
|
+
const row = {};
|
|
177
|
+
for (const { feed, data } of feeds){
|
|
178
|
+
row[feed] = Object.fromEntries(Object.entries(data).map(([key, report])=>[
|
|
179
|
+
key,
|
|
180
|
+
report.toString()
|
|
181
|
+
]));
|
|
182
|
+
}
|
|
183
|
+
output[`${report.target} ${measure}`] = row;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
console.log(JSON.stringify(output, null, padding));
|
|
187
|
+
};
|
|
2
188
|
|
|
3
189
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './benchmark.js';\nimport { Benchmark as _Benchmark } from './benchmark.js';\n\ndeclare global {\n const benchmark: (typeof _Benchmark)['create'];\n}\n"],"names":[],"mappings":"AAAA,cAAc,iBAAiB"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { cpus } from 'node:os';\nimport { createExecutor, ExecutorOptions, ExecutorReport } from './executor.js';\nimport { MaybePromise, StepFn, SetupFn, TeardownFn, FeedFn, ReportType, ReportTypeList, DEFAULT_CYCLES } from './types.js';\n\ndeclare global {\n const benchmark: typeof Benchmark.create;\n}\n\nexport const DEFAULT_WORKERS = cpus().length;\n\nexport const AsyncFunction = (async () => {}).constructor;\n\nexport interface TargetReport<R extends ReportTypeList> {\n target: string;\n measures: MeasureReport<R>[];\n}\n\nexport interface MeasureReport<R extends ReportTypeList> {\n measure: string;\n feeds: FeedReport<R>[];\n}\n\nexport interface FeedReport<R extends ReportTypeList> {\n feed: string;\n data: ExecutorReport<R>;\n}\n\nexport const DEFAULT_REPORT_TYPES = ['ops'] as const;\nexport type DefaultReportTypes = (typeof DEFAULT_REPORT_TYPES)[number];\n\nexport class MeasureContext<TContext, TInput> {\n public pre?: StepFn<TContext, TInput>;\n public post?: StepFn<TContext, TInput>;\n\n constructor(\n public title: string,\n public run: StepFn<TContext, TInput>,\n ) {}\n}\n\nexport class Measure<TContext, TInput> {\n #ctx: MeasureContext<TContext, TInput>;\n\n constructor(ctx: MeasureContext<TContext, TInput>) {\n this.#ctx = ctx;\n }\n\n pre(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n this.#ctx.pre = fn;\n return this;\n }\n post(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n this.#ctx.post = fn;\n return this;\n }\n}\n\nexport class TargetContext<TContext, TInput> {\n public teardown?: TeardownFn<TContext>;\n public measures: MeasureContext<TContext, TInput>[] = [];\n\n constructor(\n readonly title: string,\n readonly setup?: SetupFn<MaybePromise<TContext>>,\n ) {}\n}\n\nexport class Target<TContext, TInput> {\n #ctx: TargetContext<TContext, TInput>;\n\n constructor(ctx: TargetContext<TContext, TInput>) {\n this.#ctx = ctx;\n }\n teardown(fn: TeardownFn<TContext>): Target<TContext, TInput> {\n this.#ctx.teardown = fn;\n\n return this;\n }\n measure(title: string, run: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n const measure = new MeasureContext(title, run);\n this.#ctx.measures.push(measure);\n\n return new Measure(measure);\n }\n}\n\nexport class FeedContext<TInput> {\n constructor(\n readonly title: string,\n readonly fn?: FeedFn<TInput>,\n ) {}\n}\n\nexport class Benchmark<TInput> {\n #targets: TargetContext<unknown, TInput>[] = [];\n #feeds: FeedContext<TInput>[] = [];\n #executed = false;\n\n static create(title: string): Benchmark<void>;\n static create<I>(title: string, fn: FeedFn<I>): Benchmark<I>;\n static create<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<I> {\n if (fn) {\n return new Benchmark(title, fn);\n } else {\n return new Benchmark(title);\n }\n }\n\n constructor(title: string);\n constructor(title: string, fn: FeedFn<TInput>);\n constructor(title: string, fn?: FeedFn<TInput> | undefined) {\n if (fn) {\n this.feed(title, fn);\n } else {\n this.feed(title);\n }\n }\n\n feed(title: string): Benchmark<TInput | void>;\n feed<I>(title: string, fn: FeedFn<I>): Benchmark<TInput | I>;\n feed<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<TInput | I> {\n const self = this as Benchmark<TInput | I>;\n self.#feeds.push(fn ? new FeedContext(title, fn) : new FeedContext(title));\n\n return self;\n }\n\n target<TContext>(title: string): Target<void, TInput>;\n target<TContext>(title: string, setup: SetupFn<Awaited<TContext>>): Target<TContext, TInput>;\n target<TContext>(title: string, setup?: SetupFn<Awaited<TContext>> | undefined): Target<TContext, TInput> {\n const target = new TargetContext<TContext, TInput>(title, setup);\n this.#targets.push(target as TargetContext<unknown, TInput>);\n\n return new Target<TContext, TInput>(target);\n }\n\n async execute<R extends readonly ReportType[] = typeof DEFAULT_REPORT_TYPES>({\n workers = DEFAULT_WORKERS,\n warmupCycles = 20,\n maxCycles = DEFAULT_CYCLES,\n minCycles = 50,\n absThreshold = 1_000,\n relThreshold = 0.02,\n reportTypes = DEFAULT_REPORT_TYPES as unknown as R,\n }: ExecutorOptions<R>): Promise<TargetReport<R>[]> {\n if (this.#executed) {\n throw new Error(\"Benchmark is executed and can't be reused\");\n }\n this.#executed = true;\n\n const executor = createExecutor<unknown, TInput, R>({\n workers,\n warmupCycles,\n maxCycles,\n minCycles,\n absThreshold,\n relThreshold,\n reportTypes,\n });\n\n const reports: TargetReport<R>[] = [];\n for (const target of this.#targets) {\n const targetReport: TargetReport<R> = { target: target.title, measures: [] };\n for (const measure of target.measures) {\n const measureReport: MeasureReport<R> = { measure: measure.title, feeds: [] };\n for (const feed of this.#feeds) {\n const data = await feed.fn?.();\n executor\n .push<ExecutorReport<R>>({\n setup: target.setup,\n teardown: target.teardown,\n pre: measure.pre,\n run: measure.run,\n post: measure.post,\n data,\n })\n .then((data) => {\n measureReport.feeds.push({\n feed: feed.title,\n data,\n });\n });\n }\n targetReport.measures.push(measureReport);\n }\n reports.push(targetReport);\n }\n await executor.drain();\n executor.kill();\n\n return reports;\n }\n}\n\nexport const printSimpleReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n console.group('\\n', report.target, measure);\n for (const { feed, data } of feeds) {\n const output = Object.entries(data)\n .map(([key, report]) => `${key}: ${report.toString()}`)\n .join('; ');\n console.log(feed, output);\n }\n console.groupEnd();\n }\n }\n};\n\nexport const printTableReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n console.log('\\n', report.target, measure);\n const table: Record<string, unknown> = {};\n for (const { feed, data } of feeds) {\n table[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));\n }\n console.table(table);\n }\n }\n};\n\nexport const printJSONReports = <R extends ReportTypeList>(reports: TargetReport<R>[], padding?: number) => {\n const output = {} as Record<string, Record<string, Record<string, string>>>;\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n const row = {} as Record<string, Record<string, string>>;\n for (const { feed, data } of feeds) {\n row[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));\n }\n output[`${report.target} ${measure}`] = row;\n }\n }\n console.log(JSON.stringify(output, null, padding));\n};\n"],"names":["cpus","createExecutor","DEFAULT_CYCLES","DEFAULT_WORKERS","length","AsyncFunction","constructor","DEFAULT_REPORT_TYPES","MeasureContext","pre","post","title","run","Measure","ctx","fn","TargetContext","teardown","measures","setup","Target","measure","push","FeedContext","Benchmark","create","feed","self","target","execute","workers","warmupCycles","maxCycles","minCycles","absThreshold","relThreshold","reportTypes","Error","executor","reports","targetReport","measureReport","feeds","data","then","drain","kill","printSimpleReports","report","console","group","output","Object","entries","map","key","toString","join","log","groupEnd","printTableReports","table","fromEntries","printJSONReports","padding","row","JSON","stringify"],"mappings":"AAAA,SAASA,IAAI,QAAQ,UAAU;AAC/B,SAASC,cAAc,QAAyC,gBAAgB;AAChF,SAAwFC,cAAc,QAAQ,aAAa;AAM3H,OAAO,MAAMC,kBAAkBH,OAAOI,MAAM,CAAC;AAE7C,OAAO,MAAMC,gBAAgB,AAAC,CAAA,WAAa,CAAA,EAAGC,WAAW,CAAC;AAiB1D,OAAO,MAAMC,uBAAuB;IAAC;CAAM,CAAU;AAGrD,OAAO,MAAMC;;;IACJC,IAA+B;IAC/BC,KAAgC;IAEvCJ,YACE,AAAOK,KAAa,EACpB,AAA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AK,EAAE,IAAIM,OAAO9B,QAAQ,CAAE;YAChD+B,QAAQS,GAAG,CAAC,MAAMV,OAAOpB,MAAM,EAAEP;YACjC,MAAMwC,QAAiC,CAAC;YACxC,KAAK,MAAM,EAAEnC,IAAI,EAAEiB,IAAI,EAAE,IAAID,MAAO;gBAClCmB,KAAK,CAACnC,KAAK,GAAG0B,OAAOU,WAAW,CAACV,OAAOC,OAAO,CAACV,MAAMW,GAAG,CAAC,CAAC,CAACC,KAAKP,OAAO,GAAK;wBAACO;wBAAKP,OAAOQ,QAAQ;qBAAG;YACvG;YACAP,QAAQY,KAAK,CAACA;QAChB;IACF;AACF,EAAE;AAEF,OAAO,MAAME,mBAAmB,CAA2BxB,SAA4ByB;IACrF,MAAMb,SAAS,CAAC;IAChB,KAAK,MAAMH,UAAUT,QAAS;QAC5B,KAAK,MAAM,EAAElB,OAAO,EAAEqB,KAAK,EAAE,IAAIM,OAAO9B,QAAQ,CAAE;YAChD,MAAM+C,MAAM,CAAC;YACb,KAAK,MAAM,EAAEvC,IAAI,EAAEiB,IAAI,EAAE,IAAID,MAAO;gBAClCuB,GAAG,CAACvC,KAAK,GAAG0B,OAAOU,WAAW,CAACV,OAAOC,OAAO,CAACV,MAAMW,GAAG,CAAC,CAAC,CAACC,KAAKP,OAAO,GAAK;wBAACO;wBAAKP,OAAOQ,QAAQ;qBAAG;YACrG;YACAL,MAAM,CAAC,GAAGH,OAAOpB,MAAM,CAAC,CAAC,EAAEP,SAAS,CAAC,GAAG4C;QAC1C;IACF;IACAhB,QAAQS,GAAG,CAACQ,KAAKC,SAAS,CAAChB,QAAQ,MAAMa;AAC3C,EAAE"}
|
package/build/reporter.cjs
CHANGED
package/build/reporter.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["import { div, max, divs } from './utils.js';\nimport { ReportType } from './types.js';\n\nconst units = [\n { unit: 'ns', factor: 1 },\n { unit: 'µs', factor: 1e3 },\n { unit: 'ms', factor: 1e6 },\n { unit: 's', factor: 1e9 },\n { unit: 'm', factor: 60 * 1e9 },\n { unit: 'h', factor: 3600 * 1e9 },\n] as const;\n\nfunction smartFixed(n: number): string {\n return n.toLocaleString(undefined, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 2,\n useGrouping:
|
|
1
|
+
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["import { div, max, divs } from './utils.js';\nimport { ReportType } from './types.js';\n\nconst units = [\n { unit: 'ns', factor: 1 },\n { unit: 'µs', factor: 1e3 },\n { unit: 'ms', factor: 1e6 },\n { unit: 's', factor: 1e9 },\n { unit: 'm', factor: 60 * 1e9 },\n { unit: 'h', factor: 3600 * 1e9 },\n] as const;\n\nfunction smartFixed(n: number): string {\n return n.toLocaleString(undefined, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 2,\n useGrouping: true,\n });\n}\nexport class Report {\n constructor(\n public readonly type: ReportType,\n public readonly value: bigint,\n public readonly uncertainty: number = 0,\n public readonly scale: bigint = 1n,\n ) {}\n valueOf() {\n return Number(div(this.value, this.scale));\n }\n toString() {\n const uncertainty = this.uncertainty ? ` ± ${smartFixed(this.uncertainty)}%` : '';\n\n const value = this.valueOf();\n if (this.type === 'ops') {\n return `${smartFixed(value)} ops/s${uncertainty}`;\n }\n let display = value;\n let unit = 'ns';\n\n for (const { unit: u, factor } of units) {\n const candidate = value / factor;\n if (candidate < 1000) {\n display = candidate;\n unit = u;\n break;\n }\n }\n return `${smartFixed(display)} ${unit}${uncertainty}`;\n }\n}\n\nexport const createReport = (durations: BigUint64Array, type: ReportType): Report => {\n const n = durations.length;\n if (n === 0) {\n return new Report(type, 0n);\n }\n switch (type) {\n case 'min': {\n return new Report(type, durations[0]);\n }\n case 'max': {\n return new Report(type, durations[n - 1]);\n }\n case 'median': {\n const mid = Math.floor(n / 2);\n const med = n % 2 === 0 ? (durations[mid - 1] + durations[mid]) / 2n : durations[mid];\n return new Report(type, med);\n }\n\n case 'mode': {\n const freq = new Map<bigint, bigint>();\n let maxCount = 0n;\n let modeVal = durations[0];\n for (const d of durations) {\n const count = (freq.get(d) || 0n) + 1n;\n freq.set(d, count);\n if (count > maxCount) {\n maxCount = count;\n modeVal = d;\n }\n }\n let lower = modeVal;\n let upper = modeVal;\n const firstIdx = durations.indexOf(modeVal);\n const lastIdx = durations.lastIndexOf(modeVal);\n if (firstIdx > 0) lower = durations[firstIdx - 1];\n if (lastIdx < n - 1) upper = durations[lastIdx + 1];\n const gap = max(modeVal - lower, upper - modeVal);\n const uncertainty = modeVal > 0 ? Number(((gap / 2n) * 100n) / modeVal) : 0;\n return new Report(type, modeVal, uncertainty);\n }\n\n case 'ops': {\n let sum = 0n;\n for (const duration of durations) {\n sum += duration;\n }\n const avgNs = sum / BigInt(n);\n const nsPerSec = 1_000_000_000n;\n const raw = Number(nsPerSec) / Number(avgNs);\n const extra = raw < 1 ? Math.ceil(-Math.log10(raw)) : 0;\n\n const exp = raw > 100 ? 0 : 2 + extra;\n\n const scale = 10n ** BigInt(exp);\n\n const value = avgNs > 0n ? (nsPerSec * scale) / avgNs : 0n;\n const deviation = durations[n - 1] - durations[0];\n const uncertainty = avgNs > 0 ? Number(div(deviation * scale, 2n * avgNs)) : 0;\n return new Report(type, value, uncertainty, scale);\n }\n case 'mean': {\n let sum = 0n;\n for (const duration of durations) {\n sum += duration;\n }\n const value = divs(sum, BigInt(n), 1n);\n return new Report(type, value);\n }\n\n default: {\n const p = Number(type.slice(1));\n if (p === 0) {\n return new Report(type, durations[0]);\n }\n if (p === 100) {\n return new Report(type, durations[n - 1]);\n }\n const idx = Math.ceil((p / 100) * n) - 1;\n const value = durations[Math.min(Math.max(idx, 0), n - 1)];\n const prev = idx > 0 ? durations[idx - 1] : value;\n const next = idx < n - 1 ? durations[idx + 1] : value;\n const gap = max(value - prev, next - value);\n const uncertainty = value > 0 ? Number(div(divs(gap, 2n, 100_00n), value)) / 100 : 0;\n\n return new Report(type, value, uncertainty);\n }\n }\n};\n"],"names":["Report","createReport","units","unit","factor","smartFixed","n","toLocaleString","undefined","minimumFractionDigits","maximumFractionDigits","useGrouping","constructor","type","value","uncertainty","scale","valueOf","Number","div","toString","display","u","candidate","durations","length","mid","Math","floor","med","freq","Map","maxCount","modeVal","d","count","get","set","lower","upper","firstIdx","indexOf","lastIdx","lastIndexOf","gap","max","sum","duration","avgNs","BigInt","nsPerSec","raw","extra","ceil","log10","exp","deviation","divs","p","slice","idx","min","prev","next"],"mappings":";;;;;;;;;;;IAmBaA,MAAM;eAANA;;IAgCAC,YAAY;eAAZA;;;0BAnDkB;AAG/B,MAAMC,QAAQ;IACZ;QAAEC,MAAM;QAAMC,QAAQ;IAAE;IACxB;QAAED,MAAM;QAAMC,QAAQ;IAAI;IAC1B;QAAED,MAAM;QAAMC,QAAQ;IAAI;IAC1B;QAAED,MAAM;QAAKC,QAAQ;IAAI;IACzB;QAAED,MAAM;QAAKC,QAAQ,KAAK;IAAI;IAC9B;QAAED,MAAM;QAAKC,QAAQ,OAAO;IAAI;CACjC;AAED,SAASC,WAAWC,CAAS;IAC3B,OAAOA,EAAEC,cAAc,CAACC,WAAW;QACjCC,uBAAuB;QACvBC,uBAAuB;QACvBC,aAAa;IACf;AACF;AACO,MAAMX;;;;;IACXY,YACE,AAAgBC,IAAgB,EAChC,AAAgBC,KAAa,EAC7B,AAAgBC,cAAsB,CAAC,EACvC,AAAgBC,QAAgB,EAAE,CAClC;aAJgBH,OAAAA;aACAC,QAAAA;aACAC,cAAAA;aACAC,QAAAA;IACf;IACHC,UAAU;QACR,OAAOC,OAAOC,IAAAA,aAAG,EAAC,IAAI,CAACL,KAAK,EAAE,IAAI,CAACE,KAAK;IAC1C;IACAI,WAAW;QACT,MAAML,cAAc,IAAI,CAACA,WAAW,GAAG,CAAC,GAAG,EAAEV,WAAW,IAAI,CAACU,WAAW,EAAE,CAAC,CAAC,GAAG;QAE/E,MAAMD,QAAQ,IAAI,CAACG,OAAO;QAC1B,IAAI,IAAI,CAACJ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Q;gBACX,IAAI8B,MAAM,EAAE;gBACZ,KAAK,MAAMC,YAAYvB,UAAW;oBAChCsB,OAAOC;gBACT;gBACA,MAAMjC,QAAQ2C,IAAAA,cAAI,EAACX,KAAKG,OAAO3C,IAAI,EAAE;gBACrC,OAAO,IAAIN,OAAOa,MAAMC;YAC1B;QAEA;YAAS;gBACP,MAAM4C,IAAIxC,OAAOL,KAAK8C,KAAK,CAAC;gBAC5B,IAAID,MAAM,GAAG;oBACX,OAAO,IAAI1D,OAAOa,MAAMW,SAAS,CAAC,EAAE;gBACtC;gBACA,IAAIkC,MAAM,KAAK;oBACb,OAAO,IAAI1D,OAAOa,MAAMW,SAAS,CAAClB,IAAI,EAAE;gBAC1C;gBACA,MAAMsD,MAAMjC,KAAK0B,IAAI,CAAC,AAACK,IAAI,MAAOpD,KAAK;gBACvC,MAAMQ,QAAQU,SAAS,CAACG,KAAKkC,GAAG,CAAClC,KAAKkB,GAAG,CAACe,KAAK,IAAItD,IAAI,GAAG;gBAC1D,MAAMwD,OAAOF,MAAM,IAAIpC,SAAS,CAACoC,MAAM,EAAE,GAAG9C;gBAC5C,MAAMiD,OAAOH,MAAMtD,IAAI,IAAIkB,SAAS,CAACoC,MAAM,EAAE,GAAG9C;gBAChD,MAAM8B,MAAMC,IAAAA,aAAG,EAAC/B,QAAQgD,MAAMC,OAAOjD;gBACrC,MAAMC,cAAcD,QAAQ,IAAII,OAAOC,IAAAA,aAAG,EAACsC,IAAAA,cAAI,EAACb,KAAK,EAAE,EAAE,OAAO,GAAG9B,UAAU,MAAM;gBAEnF,OAAO,IAAId,OAAOa,MAAMC,OAAOC;YACjC;IACF;AACF"}
|
package/build/reporter.js
CHANGED
package/build/reporter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["import { div, max, divs } from './utils.js';\nimport { ReportType } from './types.js';\n\nconst units = [\n { unit: 'ns', factor: 1 },\n { unit: 'µs', factor: 1e3 },\n { unit: 'ms', factor: 1e6 },\n { unit: 's', factor: 1e9 },\n { unit: 'm', factor: 60 * 1e9 },\n { unit: 'h', factor: 3600 * 1e9 },\n] as const;\n\nfunction smartFixed(n: number): string {\n return n.toLocaleString(undefined, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 2,\n useGrouping:
|
|
1
|
+
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["import { div, max, divs } from './utils.js';\nimport { ReportType } from './types.js';\n\nconst units = [\n { unit: 'ns', factor: 1 },\n { unit: 'µs', factor: 1e3 },\n { unit: 'ms', factor: 1e6 },\n { unit: 's', factor: 1e9 },\n { unit: 'm', factor: 60 * 1e9 },\n { unit: 'h', factor: 3600 * 1e9 },\n] as const;\n\nfunction smartFixed(n: number): string {\n return n.toLocaleString(undefined, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 2,\n useGrouping: true,\n });\n}\nexport class Report {\n constructor(\n public readonly type: ReportType,\n public readonly value: bigint,\n public readonly uncertainty: number = 0,\n public readonly scale: bigint = 1n,\n ) {}\n valueOf() {\n return Number(div(this.value, this.scale));\n }\n toString() {\n const uncertainty = this.uncertainty ? ` ± ${smartFixed(this.uncertainty)}%` : '';\n\n const value = this.valueOf();\n if (this.type === 'ops') {\n return `${smartFixed(value)} ops/s${uncertainty}`;\n }\n let display = value;\n let unit = 'ns';\n\n for (const { unit: u, factor } of units) {\n const candidate = value / factor;\n if (candidate < 1000) {\n display = candidate;\n unit = u;\n break;\n }\n }\n return `${smartFixed(display)} ${unit}${uncertainty}`;\n }\n}\n\nexport const createReport = (durations: BigUint64Array, type: ReportType): Report => {\n const n = durations.length;\n if (n === 0) {\n return new Report(type, 0n);\n }\n switch (type) {\n case 'min': {\n return new Report(type, durations[0]);\n }\n case 'max': {\n return new Report(type, durations[n - 1]);\n }\n case 'median': {\n const mid = Math.floor(n / 2);\n const med = n % 2 === 0 ? (durations[mid - 1] + durations[mid]) / 2n : durations[mid];\n return new Report(type, med);\n }\n\n case 'mode': {\n const freq = new Map<bigint, bigint>();\n let maxCount = 0n;\n let modeVal = durations[0];\n for (const d of durations) {\n const count = (freq.get(d) || 0n) + 1n;\n freq.set(d, count);\n if (count > maxCount) {\n maxCount = count;\n modeVal = d;\n }\n }\n let lower = modeVal;\n let upper = modeVal;\n const firstIdx = durations.indexOf(modeVal);\n const lastIdx = durations.lastIndexOf(modeVal);\n if (firstIdx > 0) lower = durations[firstIdx - 1];\n if (lastIdx < n - 1) upper = durations[lastIdx + 1];\n const gap = max(modeVal - lower, upper - modeVal);\n const uncertainty = modeVal > 0 ? Number(((gap / 2n) * 100n) / modeVal) : 0;\n return new Report(type, modeVal, uncertainty);\n }\n\n case 'ops': {\n let sum = 0n;\n for (const duration of durations) {\n sum += duration;\n }\n const avgNs = sum / BigInt(n);\n const nsPerSec = 1_000_000_000n;\n const raw = Number(nsPerSec) / Number(avgNs);\n const extra = raw < 1 ? Math.ceil(-Math.log10(raw)) : 0;\n\n const exp = raw > 100 ? 0 : 2 + extra;\n\n const scale = 10n ** BigInt(exp);\n\n const value = avgNs > 0n ? (nsPerSec * scale) / avgNs : 0n;\n const deviation = durations[n - 1] - durations[0];\n const uncertainty = avgNs > 0 ? Number(div(deviation * scale, 2n * avgNs)) : 0;\n return new Report(type, value, uncertainty, scale);\n }\n case 'mean': {\n let sum = 0n;\n for (const duration of durations) {\n sum += duration;\n }\n const value = divs(sum, BigInt(n), 1n);\n return new Report(type, value);\n }\n\n default: {\n const p = Number(type.slice(1));\n if (p === 0) {\n return new Report(type, durations[0]);\n }\n if (p === 100) {\n return new Report(type, durations[n - 1]);\n }\n const idx = Math.ceil((p / 100) * n) - 1;\n const value = durations[Math.min(Math.max(idx, 0), n - 1)];\n const prev = idx > 0 ? durations[idx - 1] : value;\n const next = idx < n - 1 ? durations[idx + 1] : value;\n const gap = max(value - prev, next - value);\n const uncertainty = value > 0 ? Number(div(divs(gap, 2n, 100_00n), value)) / 100 : 0;\n\n return new Report(type, value, uncertainty);\n }\n }\n};\n"],"names":["div","max","divs","units","unit","factor","smartFixed","n","toLocaleString","undefined","minimumFractionDigits","maximumFractionDigits","useGrouping","Report","constructor","type","value","uncertainty","scale","valueOf","Number","toString","display","u","candidate","createReport","durations","length","mid","Math","floor","med","freq","Map","maxCount","modeVal","d","count","get","set","lower","upper","firstIdx","indexOf","lastIdx","lastIndexOf","gap","sum","duration","avgNs","BigInt","nsPerSec","raw","extra","ceil","log10","exp","deviation","p","slice","idx","min","prev","next"],"mappings":"AAAA,SAASA,GAAG,EAAEC,GAAG,EAAEC,IAAI,QAAQ,aAAa;AAG5C,MAAMC,QAAQ;IACZ;QAAEC,MAAM;QAAMC,QAAQ;IAAE;IACxB;QAAED,MAAM;QAAMC,QAAQ;IAAI;IAC1B;QAAED,MAAM;QAAMC,QAAQ;IAAI;IAC1B;QAAED,MAAM;QAAKC,QAAQ;IAAI;IACzB;QAAED,MAAM;QAAKC,QAAQ,KAAK;IAAI;IAC9B;QAAED,MAAM;QAAKC,QAAQ,OAAO;IAAI;CACjC;AAED,SAASC,WAAWC,CAAS;IAC3B,OAAOA,EAAEC,cAAc,CAACC,WAAW;QACjCC,uBAAuB;QACvBC,uBAAuB;QACvBC,aAAa;IACf;AACF;AACA,OAAO,MAAMC;;;;;IACXC,YACE,AAAgBC,IAAgB,EAChC,AAAgBC,KAAa,EAC7B,AAAgBC,cAAsB,CAAC,EACvC,AAAgBC,QAAgB,EAAE,CAClC;aAJgBH,OAAAA;aACAC,QAAAA;aACAC,cAAAA;aACAC,QAAAA;IACf;IACHC,UAAU;QACR,OAAOC,OAAOpB,IAAI,IAAI,CAACgB,KAAK,EAAE,IAAI,CAACE,KAAK;IAC1C;IACAG,WAAW;QACT,MAAMJ,cAAc,IAAI,CAACA,WAAW,GAAG,CAAC,GAAG,EAAEX,WAAW,IAAI,CAACW,WAAW,EAAE,CAAC,CAAC,GAAG;QAE/E,MAAMD,QAAQ,IAAI,CAACG,OAAO;QAC1B,IAAI,IAAI,CAACJ,IAAI,KAAK,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gBACZ,KAAK,MAAMC,YAAYtB,UAAW;oBAChCqB,OAAOC;gBACT;gBACA,MAAMhC,QAAQd,KAAK6C,KAAKG,OAAO3C,IAAI,EAAE;gBACrC,OAAO,IAAIM,OAAOE,MAAMC;YAC1B;QAEA;YAAS;gBACP,MAAM0C,IAAItC,OAAOL,KAAK4C,KAAK,CAAC;gBAC5B,IAAID,MAAM,GAAG;oBACX,OAAO,IAAI7C,OAAOE,MAAMW,SAAS,CAAC,EAAE;gBACtC;gBACA,IAAIgC,MAAM,KAAK;oBACb,OAAO,IAAI7C,OAAOE,MAAMW,SAAS,CAACnB,IAAI,EAAE;gBAC1C;gBACA,MAAMqD,MAAM/B,KAAKyB,IAAI,CAAC,AAACI,IAAI,MAAOnD,KAAK;gBACvC,MAAMS,QAAQU,SAAS,CAACG,KAAKgC,GAAG,CAAChC,KAAK5B,GAAG,CAAC2D,KAAK,IAAIrD,IAAI,GAAG;gBAC1D,MAAMuD,OAAOF,MAAM,IAAIlC,SAAS,CAACkC,MAAM,EAAE,GAAG5C;gBAC5C,MAAM+C,OAAOH,MAAMrD,IAAI,IAAImB,SAAS,CAACkC,MAAM,EAAE,GAAG5C;gBAChD,MAAM8B,MAAM7C,IAAIe,QAAQ8C,MAAMC,OAAO/C;gBACrC,MAAMC,cAAcD,QAAQ,IAAII,OAAOpB,IAAIE,KAAK4C,KAAK,EAAE,EAAE,OAAO,GAAG9B,UAAU,MAAM;gBAEnF,OAAO,IAAIH,OAAOE,MAAMC,OAAOC;YACjC;IACF;AACF,EAAE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const suite = benchmark('1M array of strings', () => Array.from({ length: 1_000_000 }, (_, idx) => `${idx}`))
|
|
2
|
+
.feed('1M array of numbers', () => Array.from({ length: 1_000_000 }, (_, idx) => idx))
|
|
3
|
+
.feed('1M typed array', () => new Uint32Array(1_000_000).map((_, idx) => idx));
|
|
4
|
+
|
|
5
|
+
suite.target('for loop').measure('copy half', (_, input) => {
|
|
6
|
+
const n = input?.length ?? 0;
|
|
7
|
+
const mid = n / 2;
|
|
8
|
+
for (let i = 0; i < mid; i++) {
|
|
9
|
+
input[i + mid] = input[i];
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
suite.target('copyWithin').measure('copy half', (_, input) => {
|
|
14
|
+
const n = input?.length ?? 0;
|
|
15
|
+
const mid = n / 2;
|
|
16
|
+
input.copyWithin(mid, 0, mid);
|
|
17
|
+
});
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createRequire, Module } from 'node:module';
|
|
2
|
-
import { SyntheticModule, createContext, SourceTextModule
|
|
2
|
+
import { SyntheticModule, createContext, SourceTextModule } from 'node:vm';
|
|
3
3
|
import { stat, readFile } from 'node:fs/promises';
|
|
4
4
|
import { parse, print } from '@swc/core';
|
|
5
5
|
import { Command, Option } from 'commander';
|
|
6
6
|
import { glob } from 'glob';
|
|
7
|
-
import { Benchmark, printTableReports, printJSONReports, printSimpleReports, DEFAULT_REPORT_TYPES, DEFAULT_WORKERS } from './
|
|
7
|
+
import { Benchmark, printTableReports, printJSONReports, printSimpleReports, DEFAULT_REPORT_TYPES, DEFAULT_WORKERS } from './index.js';
|
|
8
8
|
import { REPORT_TYPES } from './types.js';
|
|
9
9
|
|
|
10
10
|
const require = createRequire(import.meta.url);
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,235 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import { cpus } from 'node:os';
|
|
2
|
+
import { createExecutor, ExecutorOptions, ExecutorReport } from './executor.js';
|
|
3
|
+
import { MaybePromise, StepFn, SetupFn, TeardownFn, FeedFn, ReportType, ReportTypeList, DEFAULT_CYCLES } from './types.js';
|
|
3
4
|
|
|
4
5
|
declare global {
|
|
5
|
-
const benchmark:
|
|
6
|
+
const benchmark: typeof Benchmark.create;
|
|
6
7
|
}
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_WORKERS = cpus().length;
|
|
10
|
+
|
|
11
|
+
export const AsyncFunction = (async () => {}).constructor;
|
|
12
|
+
|
|
13
|
+
export interface TargetReport<R extends ReportTypeList> {
|
|
14
|
+
target: string;
|
|
15
|
+
measures: MeasureReport<R>[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MeasureReport<R extends ReportTypeList> {
|
|
19
|
+
measure: string;
|
|
20
|
+
feeds: FeedReport<R>[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FeedReport<R extends ReportTypeList> {
|
|
24
|
+
feed: string;
|
|
25
|
+
data: ExecutorReport<R>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const DEFAULT_REPORT_TYPES = ['ops'] as const;
|
|
29
|
+
export type DefaultReportTypes = (typeof DEFAULT_REPORT_TYPES)[number];
|
|
30
|
+
|
|
31
|
+
export class MeasureContext<TContext, TInput> {
|
|
32
|
+
public pre?: StepFn<TContext, TInput>;
|
|
33
|
+
public post?: StepFn<TContext, TInput>;
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
public title: string,
|
|
37
|
+
public run: StepFn<TContext, TInput>,
|
|
38
|
+
) {}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class Measure<TContext, TInput> {
|
|
42
|
+
#ctx: MeasureContext<TContext, TInput>;
|
|
43
|
+
|
|
44
|
+
constructor(ctx: MeasureContext<TContext, TInput>) {
|
|
45
|
+
this.#ctx = ctx;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
pre(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {
|
|
49
|
+
this.#ctx.pre = fn;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
post(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {
|
|
53
|
+
this.#ctx.post = fn;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class TargetContext<TContext, TInput> {
|
|
59
|
+
public teardown?: TeardownFn<TContext>;
|
|
60
|
+
public measures: MeasureContext<TContext, TInput>[] = [];
|
|
61
|
+
|
|
62
|
+
constructor(
|
|
63
|
+
readonly title: string,
|
|
64
|
+
readonly setup?: SetupFn<MaybePromise<TContext>>,
|
|
65
|
+
) {}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class Target<TContext, TInput> {
|
|
69
|
+
#ctx: TargetContext<TContext, TInput>;
|
|
70
|
+
|
|
71
|
+
constructor(ctx: TargetContext<TContext, TInput>) {
|
|
72
|
+
this.#ctx = ctx;
|
|
73
|
+
}
|
|
74
|
+
teardown(fn: TeardownFn<TContext>): Target<TContext, TInput> {
|
|
75
|
+
this.#ctx.teardown = fn;
|
|
76
|
+
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
measure(title: string, run: StepFn<TContext, TInput>): Measure<TContext, TInput> {
|
|
80
|
+
const measure = new MeasureContext(title, run);
|
|
81
|
+
this.#ctx.measures.push(measure);
|
|
82
|
+
|
|
83
|
+
return new Measure(measure);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class FeedContext<TInput> {
|
|
88
|
+
constructor(
|
|
89
|
+
readonly title: string,
|
|
90
|
+
readonly fn?: FeedFn<TInput>,
|
|
91
|
+
) {}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class Benchmark<TInput> {
|
|
95
|
+
#targets: TargetContext<unknown, TInput>[] = [];
|
|
96
|
+
#feeds: FeedContext<TInput>[] = [];
|
|
97
|
+
#executed = false;
|
|
98
|
+
|
|
99
|
+
static create(title: string): Benchmark<void>;
|
|
100
|
+
static create<I>(title: string, fn: FeedFn<I>): Benchmark<I>;
|
|
101
|
+
static create<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<I> {
|
|
102
|
+
if (fn) {
|
|
103
|
+
return new Benchmark(title, fn);
|
|
104
|
+
} else {
|
|
105
|
+
return new Benchmark(title);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
constructor(title: string);
|
|
110
|
+
constructor(title: string, fn: FeedFn<TInput>);
|
|
111
|
+
constructor(title: string, fn?: FeedFn<TInput> | undefined) {
|
|
112
|
+
if (fn) {
|
|
113
|
+
this.feed(title, fn);
|
|
114
|
+
} else {
|
|
115
|
+
this.feed(title);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
feed(title: string): Benchmark<TInput | void>;
|
|
120
|
+
feed<I>(title: string, fn: FeedFn<I>): Benchmark<TInput | I>;
|
|
121
|
+
feed<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<TInput | I> {
|
|
122
|
+
const self = this as Benchmark<TInput | I>;
|
|
123
|
+
self.#feeds.push(fn ? new FeedContext(title, fn) : new FeedContext(title));
|
|
124
|
+
|
|
125
|
+
return self;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
target<TContext>(title: string): Target<void, TInput>;
|
|
129
|
+
target<TContext>(title: string, setup: SetupFn<Awaited<TContext>>): Target<TContext, TInput>;
|
|
130
|
+
target<TContext>(title: string, setup?: SetupFn<Awaited<TContext>> | undefined): Target<TContext, TInput> {
|
|
131
|
+
const target = new TargetContext<TContext, TInput>(title, setup);
|
|
132
|
+
this.#targets.push(target as TargetContext<unknown, TInput>);
|
|
133
|
+
|
|
134
|
+
return new Target<TContext, TInput>(target);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async execute<R extends readonly ReportType[] = typeof DEFAULT_REPORT_TYPES>({
|
|
138
|
+
workers = DEFAULT_WORKERS,
|
|
139
|
+
warmupCycles = 20,
|
|
140
|
+
maxCycles = DEFAULT_CYCLES,
|
|
141
|
+
minCycles = 50,
|
|
142
|
+
absThreshold = 1_000,
|
|
143
|
+
relThreshold = 0.02,
|
|
144
|
+
reportTypes = DEFAULT_REPORT_TYPES as unknown as R,
|
|
145
|
+
}: ExecutorOptions<R>): Promise<TargetReport<R>[]> {
|
|
146
|
+
if (this.#executed) {
|
|
147
|
+
throw new Error("Benchmark is executed and can't be reused");
|
|
148
|
+
}
|
|
149
|
+
this.#executed = true;
|
|
150
|
+
|
|
151
|
+
const executor = createExecutor<unknown, TInput, R>({
|
|
152
|
+
workers,
|
|
153
|
+
warmupCycles,
|
|
154
|
+
maxCycles,
|
|
155
|
+
minCycles,
|
|
156
|
+
absThreshold,
|
|
157
|
+
relThreshold,
|
|
158
|
+
reportTypes,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const reports: TargetReport<R>[] = [];
|
|
162
|
+
for (const target of this.#targets) {
|
|
163
|
+
const targetReport: TargetReport<R> = { target: target.title, measures: [] };
|
|
164
|
+
for (const measure of target.measures) {
|
|
165
|
+
const measureReport: MeasureReport<R> = { measure: measure.title, feeds: [] };
|
|
166
|
+
for (const feed of this.#feeds) {
|
|
167
|
+
const data = await feed.fn?.();
|
|
168
|
+
executor
|
|
169
|
+
.push<ExecutorReport<R>>({
|
|
170
|
+
setup: target.setup,
|
|
171
|
+
teardown: target.teardown,
|
|
172
|
+
pre: measure.pre,
|
|
173
|
+
run: measure.run,
|
|
174
|
+
post: measure.post,
|
|
175
|
+
data,
|
|
176
|
+
})
|
|
177
|
+
.then((data) => {
|
|
178
|
+
measureReport.feeds.push({
|
|
179
|
+
feed: feed.title,
|
|
180
|
+
data,
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
targetReport.measures.push(measureReport);
|
|
185
|
+
}
|
|
186
|
+
reports.push(targetReport);
|
|
187
|
+
}
|
|
188
|
+
await executor.drain();
|
|
189
|
+
executor.kill();
|
|
190
|
+
|
|
191
|
+
return reports;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export const printSimpleReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {
|
|
196
|
+
for (const report of reports) {
|
|
197
|
+
for (const { measure, feeds } of report.measures) {
|
|
198
|
+
console.group('\n', report.target, measure);
|
|
199
|
+
for (const { feed, data } of feeds) {
|
|
200
|
+
const output = Object.entries(data)
|
|
201
|
+
.map(([key, report]) => `${key}: ${report.toString()}`)
|
|
202
|
+
.join('; ');
|
|
203
|
+
console.log(feed, output);
|
|
204
|
+
}
|
|
205
|
+
console.groupEnd();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
export const printTableReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {
|
|
211
|
+
for (const report of reports) {
|
|
212
|
+
for (const { measure, feeds } of report.measures) {
|
|
213
|
+
console.log('\n', report.target, measure);
|
|
214
|
+
const table: Record<string, unknown> = {};
|
|
215
|
+
for (const { feed, data } of feeds) {
|
|
216
|
+
table[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));
|
|
217
|
+
}
|
|
218
|
+
console.table(table);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const printJSONReports = <R extends ReportTypeList>(reports: TargetReport<R>[], padding?: number) => {
|
|
224
|
+
const output = {} as Record<string, Record<string, Record<string, string>>>;
|
|
225
|
+
for (const report of reports) {
|
|
226
|
+
for (const { measure, feeds } of report.measures) {
|
|
227
|
+
const row = {} as Record<string, Record<string, string>>;
|
|
228
|
+
for (const { feed, data } of feeds) {
|
|
229
|
+
row[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));
|
|
230
|
+
}
|
|
231
|
+
output[`${report.target} ${measure}`] = row;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
console.log(JSON.stringify(output, null, padding));
|
|
235
|
+
};
|
package/src/reporter.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|