Skip to content

Commit 16bf967

Browse files
feat: to fixup tinybench marker clean up
1 parent 8f2de3c commit 16bf967

3 files changed

Lines changed: 51 additions & 58 deletions

File tree

packages/tinybench-plugin/src/analysis.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
InstrumentHooks,
44
mongoMeasurement,
55
optimizeFunction,
6+
optimizeFunctionSync,
67
wrapWithRootFrame,
78
wrapWithRootFrameSync,
89
} from "@codspeed/core";
@@ -40,7 +41,14 @@ class AnalysisBenchRunner extends BaseBenchRunner {
4041
await mongoMeasurement.start(uri);
4142

4243
global.gc?.();
43-
await this.wrapWithInstrumentHooksAsync(wrapWithRootFrame(fn), uri);
44+
const rootFrame = wrapWithRootFrame(fn);
45+
InstrumentHooks.startBenchmark();
46+
try {
47+
await rootFrame();
48+
} finally {
49+
InstrumentHooks.stopBenchmark();
50+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
51+
}
4452

4553
await mongoMeasurement.stop(uri);
4654
await fnOpts?.afterEach?.call(task, "run");
@@ -53,9 +61,24 @@ class AnalysisBenchRunner extends BaseBenchRunner {
5361
const { fnOpts, fn } = this.getTaskData(task);
5462

5563
fnOpts?.beforeAll?.call(task, "run");
64+
65+
optimizeFunctionSync(async () => {
66+
fnOpts?.beforeEach?.call(task, "run");
67+
await fn();
68+
fnOpts?.afterEach?.call(task, "run");
69+
});
70+
5671
fnOpts?.beforeEach?.call(task, "run");
5772

58-
this.wrapWithInstrumentHooks(wrapWithRootFrameSync(fn), uri);
73+
global.gc?.();
74+
const rootFrame = wrapWithRootFrameSync(fn);
75+
InstrumentHooks.startBenchmark();
76+
try {
77+
rootFrame();
78+
} finally {
79+
InstrumentHooks.stopBenchmark();
80+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
81+
}
5982

6083
fnOpts?.afterEach?.call(task, "run");
6184
fnOpts?.afterAll?.call(task, "run");

packages/tinybench-plugin/src/shared.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import {
2-
getInstrumentMode,
3-
InstrumentHooks,
4-
MARKER_TYPE_BENCHMARK_END,
5-
MARKER_TYPE_BENCHMARK_START,
6-
setupCore,
7-
teardownCore,
8-
} from "@codspeed/core";
9-
import { Bench, Fn, Task } from "tinybench";
1+
import { setupCore, teardownCore } from "@codspeed/core";
2+
import { Bench, Task } from "tinybench";
103
import { CapturedTaskData, getTaskData } from "./taskData";
114
import { getTaskUri } from "./uri";
125

@@ -56,57 +49,12 @@ export abstract class BaseBenchRunner {
5649
return this.bench.tasks;
5750
}
5851

59-
protected wrapWithInstrumentHooks<T>(fn: () => T, uri: string): T {
60-
const runStart = this.openInstrumentWindow();
61-
try {
62-
return fn();
63-
} finally {
64-
this.closeInstrumentWindow(uri, runStart);
65-
}
66-
}
67-
68-
protected async wrapWithInstrumentHooksAsync(
69-
fn: Fn,
70-
uri: string,
71-
): Promise<unknown> {
72-
const runStart = this.openInstrumentWindow();
73-
try {
74-
return await fn();
75-
} finally {
76-
this.closeInstrumentWindow(uri, runStart);
77-
}
78-
}
79-
80-
protected openInstrumentWindow(): bigint {
81-
InstrumentHooks.startBenchmark();
82-
return InstrumentHooks.currentTimestamp();
83-
}
84-
85-
protected closeInstrumentWindow(uri: string, runStart: bigint): void {
86-
const runEnd = InstrumentHooks.currentTimestamp();
87-
InstrumentHooks.stopBenchmark();
88-
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
89-
this.sendBenchmarkMarkers(runStart, runEnd);
90-
}
91-
9252
protected abstract getModeName(): string;
9353
protected abstract runTaskAsync(task: Task, uri: string): Promise<void>;
9454
protected abstract runTaskSync(task: Task, uri: string): void;
9555
protected abstract finalizeAsyncRun(): Task[];
9656
protected abstract finalizeSyncRun(): Task[];
9757

98-
private sendBenchmarkMarkers(runStart: bigint, runEnd: bigint): void {
99-
if (getInstrumentMode() !== "walltime") {
100-
return;
101-
}
102-
InstrumentHooks.addMarker(
103-
process.pid,
104-
MARKER_TYPE_BENCHMARK_START,
105-
runStart,
106-
);
107-
InstrumentHooks.addMarker(process.pid, MARKER_TYPE_BENCHMARK_END, runEnd);
108-
}
109-
11058
public setupBenchMethods(): void {
11159
this.bench.run = async () => {
11260
this.setupBenchRun();

packages/tinybench-plugin/src/walltime.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {
22
calculateQuantiles,
3+
InstrumentHooks,
4+
MARKER_TYPE_BENCHMARK_END,
5+
MARKER_TYPE_BENCHMARK_START,
36
mongoMeasurement,
47
msToNs,
58
msToS,
@@ -54,20 +57,39 @@ class WalltimeBenchRunner extends BaseBenchRunner {
5457
opts.setup = (task, mode) => {
5558
const setupResult = userSetup(task, mode);
5659
if (mode === "run") {
57-
this.runStart = this.openInstrumentWindow();
60+
InstrumentHooks.startBenchmark();
61+
this.runStart = InstrumentHooks.currentTimestamp();
5862
}
5963
return setupResult;
6064
};
6165

6266
opts.teardown = (task, mode) => {
6367
if (mode === "run" && task) {
64-
this.closeInstrumentWindow(this.getTaskUri(task), this.runStart!);
68+
const runEnd = InstrumentHooks.currentTimestamp();
69+
InstrumentHooks.stopBenchmark();
70+
InstrumentHooks.setExecutedBenchmark(
71+
process.pid,
72+
this.getTaskUri(task),
73+
);
74+
if (this.runStart !== null) {
75+
this.sendBenchmarkMarkers(this.runStart, runEnd);
76+
}
77+
6578
this.runStart = null;
6679
}
6780
return userTeardown(task, mode);
6881
};
6982
}
7083

84+
private sendBenchmarkMarkers(runStart: bigint, runEnd: bigint): void {
85+
InstrumentHooks.addMarker(
86+
process.pid,
87+
MARKER_TYPE_BENCHMARK_START,
88+
runStart,
89+
);
90+
InstrumentHooks.addMarker(process.pid, MARKER_TYPE_BENCHMARK_END, runEnd);
91+
}
92+
7193
protected async runTaskAsync(task: Task, uri: string): Promise<void> {
7294
// run the warmup of the task right before its actual run
7395
if (getBenchOptions(this.bench).warmup) {

0 commit comments

Comments
 (0)