Skip to content

Commit 7ee6021

Browse files
chore: restore the refactored impl
1 parent f5ce409 commit 7ee6021

File tree

1 file changed

+99
-186
lines changed

1 file changed

+99
-186
lines changed

packages/tinybench-plugin/src/walltime.ts

Lines changed: 99 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,221 +1,134 @@
11
import {
2-
Benchmark,
32
calculateQuantiles,
4-
InstrumentHooks,
53
mongoMeasurement,
64
msToNs,
75
msToS,
86
writeWalltimeResults,
7+
type Benchmark as CodspeedBenchmark,
98
type BenchmarkStats,
109
} from "@codspeed/core";
11-
import { Bench, TaskResult } from "tinybench";
12-
import { getTaskUri } from "./uri";
10+
import { Bench, Fn, Task, TaskResult } from "tinybench";
11+
import { BaseBenchRunner } from "./shared";
1312

1413
export function setupCodspeedWalltimeBench(
1514
bench: Bench,
1615
rootCallingFile: string
1716
): void {
18-
// const runner = new WalltimeBenchRunner(bench, rootCallingFile);
19-
// runner.setupBenchMethods();
17+
const runner = new WalltimeBenchRunner(bench, rootCallingFile);
18+
runner.setupBenchMethods();
19+
}
2020

21-
bench.run = async () => {
22-
console.log(
23-
`[CodSpeed] running with @codspeed/tinybench v (walltime mode)`
24-
);
21+
class WalltimeBenchRunner extends BaseBenchRunner {
22+
private codspeedBenchmarks: CodspeedBenchmark[] = [];
2523

26-
// Store the original run method before we override it
27-
const originalRun = bench.run;
24+
protected getModeName(): string {
25+
return "walltime mode";
26+
}
2827

29-
// Temporarily restore the original run to get actual benchmark results
30-
const benchProto = Object.getPrototypeOf(bench);
31-
const prototypeRun = benchProto.run;
32-
bench.run = prototypeRun;
28+
protected async runTaskAsync(task: Task, uri: string): Promise<void> {
29+
// Override the function under test to add a static frame
30+
this.wrapTaskFunction(task, true);
31+
32+
// run the warmup of the task right before its actual run
33+
if (this.bench.opts.warmup) {
34+
await task.warmup();
35+
}
3336

34-
const benchmarks: Benchmark[] = [];
37+
await mongoMeasurement.start(uri);
38+
await this.wrapWithInstrumentHooksAsync(() => task.run(), uri);
39+
await mongoMeasurement.stop(uri);
40+
41+
this.registerCodspeedBenchmarkFromTask(task);
42+
}
43+
44+
protected runTaskSync(task: Task, uri: string): void {
45+
// Override the function under test to add a static frame
46+
this.wrapTaskFunction(task, false);
47+
48+
if (this.bench.opts.warmup) {
49+
task.warmup();
50+
}
3551

36-
// Run the bench naturally to collect TaskResult data
37-
const results = [];
52+
this.wrapWithInstrumentHooks(() => task.runSync(), uri);
3853

39-
// Collect and report walltime data
40-
for (const task of bench.tasks) {
41-
const uri = getTaskUri(bench, task.name, rootCallingFile);
54+
this.registerCodspeedBenchmarkFromTask(task);
55+
}
4256

43-
// const { fn } = task as unknown as { fn: Fn };
44-
// // eslint-disable-next-line no-inner-declarations
45-
// async function __codspeed_root_frame__() {
46-
// await fn();
47-
// }
48-
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
49-
// (task as any).fn = __codspeed_root_frame__;
57+
protected finalizeAsyncRun(): Task[] {
58+
return this.finalizeWalltimeRun(true);
59+
}
5060

51-
// run the warmup of the task right before its actual run
52-
if (bench.opts.warmup) {
53-
await task.warmup();
61+
protected finalizeSyncRun(): Task[] {
62+
return this.finalizeWalltimeRun(false);
63+
}
64+
65+
private wrapTaskFunction(task: Task, isAsync: boolean): void {
66+
const { fn } = task as unknown as { fn: Fn };
67+
if (isAsync) {
68+
// eslint-disable-next-line no-inner-declarations
69+
async function __codspeed_root_frame__() {
70+
await fn();
5471
}
55-
await mongoMeasurement.start(uri);
56-
InstrumentHooks.startBenchmark();
57-
const taskResult = await task.run();
58-
InstrumentHooks.stopBenchmark();
59-
await mongoMeasurement.stop(uri);
60-
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
61-
results.push(taskResult);
62-
63-
if (task.result) {
64-
// Convert tinybench result to BenchmarkStats format
65-
const stats = convertTinybenchResultToBenchmarkStats(
66-
task.result,
67-
bench.opts.warmup ? bench.opts.warmupIterations ?? 0 : 0
68-
);
69-
70-
const benchmark: Benchmark = {
71-
name: task.name,
72-
uri,
73-
config: {
74-
max_rounds: bench.opts.iterations ?? null,
75-
max_time_ns: bench.opts.time ? msToNs(bench.opts.time) : null,
76-
min_round_time_ns: null, // tinybench does not have an option for this
77-
warmup_time_ns:
78-
bench.opts.warmup && bench.opts.warmupTime
79-
? msToNs(bench.opts.warmupTime)
80-
: null,
81-
},
82-
stats,
83-
};
84-
85-
benchmarks.push(benchmark);
86-
87-
console.log(` ✔ Collected walltime data for ${uri}`);
88-
} else {
89-
console.warn(` ⚠ No result data available for ${uri}`);
72+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73+
(task as any).fn = __codspeed_root_frame__;
74+
} else {
75+
// eslint-disable-next-line no-inner-declarations
76+
function __codspeed_root_frame__() {
77+
fn();
9078
}
79+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80+
(task as any).fn = __codspeed_root_frame__;
81+
}
82+
}
83+
84+
private registerCodspeedBenchmarkFromTask(task: Task): void {
85+
const uri = this.getTaskUri(task);
86+
87+
if (!task.result) {
88+
console.warn(` ⚠ No result data available for ${uri}`);
89+
return;
9190
}
9291

92+
const warmupIterations = this.bench.opts.warmup
93+
? this.bench.opts.warmupIterations ?? TINYBENCH_WARMUP_DEFAULT
94+
: 0;
95+
const stats = convertTinybenchResultToBenchmarkStats(
96+
task.result,
97+
warmupIterations
98+
);
99+
100+
this.codspeedBenchmarks.push({
101+
name: task.name,
102+
uri,
103+
config: {
104+
max_rounds: this.bench.opts.iterations ?? null,
105+
max_time_ns: this.bench.opts.time ? msToNs(this.bench.opts.time) : null,
106+
min_round_time_ns: null, // tinybench does not have an option for this
107+
warmup_time_ns:
108+
this.bench.opts.warmup && this.bench.opts.warmupTime
109+
? msToNs(this.bench.opts.warmupTime)
110+
: null,
111+
},
112+
stats,
113+
});
114+
115+
this.logTaskCompletion(uri, "Collected walltime data for");
116+
}
117+
118+
private finalizeWalltimeRun(isAsync: boolean): Task[] {
93119
// Write results to JSON file using core function
94-
if (benchmarks.length > 0) {
95-
writeWalltimeResults(benchmarks);
120+
if (this.codspeedBenchmarks.length > 0) {
121+
writeWalltimeResults(this.codspeedBenchmarks, isAsync);
96122
}
97123

98124
console.log(
99-
`[CodSpeed] Done collecting walltime data for ${bench.tasks.length} benches.`
125+
`[CodSpeed] Done collecting walltime data for ${this.bench.tasks.length} benches.`
100126
);
101-
// Restore our custom run method
102-
bench.run = originalRun;
103-
104-
return results;
105-
};
127+
return this.bench.tasks;
128+
}
106129
}
107130

108-
// class WalltimeBenchRunner extends BaseBenchRunner {
109-
// private codspeedBenchmarks: CodspeedBenchmark[] = [];
110-
//
111-
// protected getModeName(): string {
112-
// return "walltime mode";
113-
// }
114-
//
115-
// protected async runTaskAsync(task: Task, uri: string): Promise<void> {
116-
// // Override the function under test to add a static frame
117-
// this.wrapTaskFunction(task, true);
118-
//
119-
// // run the warmup of the task right before its actual run
120-
// if (this.bench.opts.warmup) {
121-
// await task.warmup();
122-
// }
123-
//
124-
// await mongoMeasurement.start(uri);
125-
// await this.wrapWithInstrumentHooksAsync(() => task.run(), uri);
126-
// await mongoMeasurement.stop(uri);
127-
//
128-
// this.registerCodspeedBenchmarkFromTask(task);
129-
// }
130-
//
131-
// protected runTaskSync(task: Task, uri: string): void {
132-
// // Override the function under test to add a static frame
133-
// this.wrapTaskFunction(task, false);
134-
//
135-
// if (this.bench.opts.warmup) {
136-
// task.warmup();
137-
// }
138-
//
139-
// this.wrapWithInstrumentHooks(() => task.runSync(), uri);
140-
//
141-
// this.registerCodspeedBenchmarkFromTask(task);
142-
// }
143-
//
144-
// protected finalizeAsyncRun(): Task[] {
145-
// return this.finalizeWalltimeRun(true);
146-
// }
147-
//
148-
// protected finalizeSyncRun(): Task[] {
149-
// return this.finalizeWalltimeRun(false);
150-
// }
151-
//
152-
// private wrapTaskFunction(task: Task, isAsync: boolean): void {
153-
// const { fn } = task as unknown as { fn: Fn };
154-
// if (isAsync) {
155-
// // eslint-disable-next-line no-inner-declarations
156-
// async function __codspeed_root_frame__() {
157-
// await fn();
158-
// }
159-
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
160-
// (task as any).fn = __codspeed_root_frame__;
161-
// } else {
162-
// // eslint-disable-next-line no-inner-declarations
163-
// function __codspeed_root_frame__() {
164-
// fn();
165-
// }
166-
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
167-
// (task as any).fn = __codspeed_root_frame__;
168-
// }
169-
// }
170-
//
171-
// private registerCodspeedBenchmarkFromTask(task: Task): void {
172-
// const uri = this.getTaskUri(task);
173-
//
174-
// if (!task.result) {
175-
// console.warn(` ⚠ No result data available for ${uri}`);
176-
// return;
177-
// }
178-
//
179-
// const warmupIterations = this.bench.opts.warmup
180-
// ? this.bench.opts.warmupIterations ?? TINYBENCH_WARMUP_DEFAULT
181-
// : 0;
182-
// const stats = convertTinybenchResultToBenchmarkStats(
183-
// task.result,
184-
// warmupIterations
185-
// );
186-
//
187-
// this.codspeedBenchmarks.push({
188-
// name: task.name,
189-
// uri,
190-
// config: {
191-
// max_rounds: this.bench.opts.iterations ?? null,
192-
// max_time_ns: this.bench.opts.time ? msToNs(this.bench.opts.time) : null,
193-
// min_round_time_ns: null, // tinybench does not have an option for this
194-
// warmup_time_ns:
195-
// this.bench.opts.warmup && this.bench.opts.warmupTime
196-
// ? msToNs(this.bench.opts.warmupTime)
197-
// : null,
198-
// },
199-
// stats,
200-
// });
201-
//
202-
// this.logTaskCompletion(uri, "Collected walltime data for");
203-
// }
204-
//
205-
// private finalizeWalltimeRun(isAsync: boolean): Task[] {
206-
// // Write results to JSON file using core function
207-
// if (this.codspeedBenchmarks.length > 0) {
208-
// writeWalltimeResults(this.codspeedBenchmarks, isAsync);
209-
// }
210-
//
211-
// console.log(
212-
// `[CodSpeed] Done collecting walltime data for ${this.bench.tasks.length} benches.`
213-
// );
214-
// return this.bench.tasks;
215-
// }
216-
// }
217-
218-
// const TINYBENCH_WARMUP_DEFAULT = 16;
131+
const TINYBENCH_WARMUP_DEFAULT = 16;
219132

220133
function convertTinybenchResultToBenchmarkStats(
221134
result: TaskResult,

0 commit comments

Comments
 (0)