Skip to content

Commit c52eabe

Browse files
feat: to fixup tinybench marker clean up
1 parent 89b8a9a commit c52eabe

3 files changed

Lines changed: 45 additions & 51 deletions

File tree

packages/tinybench-plugin/src/analysis.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ class AnalysisBenchRunner extends BaseBenchRunner {
4040
await mongoMeasurement.start(uri);
4141

4242
global.gc?.();
43-
await this.wrapWithInstrumentHooksAsync(wrapWithRootFrame(fn), uri);
43+
const rootFrame = wrapWithRootFrame(fn);
44+
InstrumentHooks.startBenchmark();
45+
const runStart = InstrumentHooks.currentTimestamp();
46+
try {
47+
await rootFrame();
48+
} finally {
49+
const runEnd = InstrumentHooks.currentTimestamp();
50+
InstrumentHooks.stopBenchmark();
51+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
52+
this.sendBenchmarkMarkers(runStart, runEnd);
53+
}
4454

4555
await mongoMeasurement.stop(uri);
4656
await fnOpts?.afterEach?.call(task, "run");
@@ -55,7 +65,17 @@ class AnalysisBenchRunner extends BaseBenchRunner {
5565
fnOpts?.beforeAll?.call(task, "run");
5666
fnOpts?.beforeEach?.call(task, "run");
5767

58-
this.wrapWithInstrumentHooks(wrapWithRootFrameSync(fn), uri);
68+
const rootFrame = wrapWithRootFrameSync(fn);
69+
InstrumentHooks.startBenchmark();
70+
const runStart = InstrumentHooks.currentTimestamp();
71+
try {
72+
rootFrame();
73+
} finally {
74+
const runEnd = InstrumentHooks.currentTimestamp();
75+
InstrumentHooks.stopBenchmark();
76+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
77+
this.sendBenchmarkMarkers(runStart, runEnd);
78+
}
5979

6080
fnOpts?.afterEach?.call(task, "run");
6181
fnOpts?.afterAll?.call(task, "run");

packages/tinybench-plugin/src/shared.ts

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
2-
getInstrumentMode,
32
InstrumentHooks,
43
MARKER_TYPE_BENCHMARK_END,
54
MARKER_TYPE_BENCHMARK_START,
65
setupCore,
76
teardownCore,
87
} from "@codspeed/core";
9-
import { Bench, Fn, Task } from "tinybench";
8+
import { Bench, Task } from "tinybench";
109
import { CapturedTaskData, getTaskData } from "./taskData";
1110
import { getTaskUri } from "./uri";
1211

@@ -56,57 +55,12 @@ export abstract class BaseBenchRunner {
5655
return this.bench.tasks;
5756
}
5857

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-
9258
protected abstract getModeName(): string;
9359
protected abstract runTaskAsync(task: Task, uri: string): Promise<void>;
9460
protected abstract runTaskSync(task: Task, uri: string): void;
9561
protected abstract finalizeAsyncRun(): Task[];
9662
protected abstract finalizeSyncRun(): Task[];
9763

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-
11064
public setupBenchMethods(): void {
11165
this.bench.run = async () => {
11266
this.setupBenchRun();
@@ -130,4 +84,13 @@ export abstract class BaseBenchRunner {
13084
return this.finalizeSyncRun();
13185
};
13286
}
87+
88+
protected sendBenchmarkMarkers(runStart: bigint, runEnd: bigint): void {
89+
InstrumentHooks.addMarker(
90+
process.pid,
91+
MARKER_TYPE_BENCHMARK_START,
92+
runStart,
93+
);
94+
InstrumentHooks.addMarker(process.pid, MARKER_TYPE_BENCHMARK_END, runEnd);
95+
}
13396
}

packages/tinybench-plugin/src/walltime.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
calculateQuantiles,
3+
InstrumentHooks,
34
mongoMeasurement,
45
msToNs,
56
msToS,
@@ -54,14 +55,24 @@ class WalltimeBenchRunner extends BaseBenchRunner {
5455
opts.setup = (task, mode) => {
5556
const setupResult = userSetup(task, mode);
5657
if (mode === "run") {
57-
this.runStart = this.openInstrumentWindow();
58+
InstrumentHooks.startBenchmark();
59+
this.runStart = InstrumentHooks.currentTimestamp();
5860
}
5961
return setupResult;
6062
};
6163

6264
opts.teardown = (task, mode) => {
6365
if (mode === "run" && task) {
64-
this.closeInstrumentWindow(this.getTaskUri(task), this.runStart!);
66+
const runEnd = InstrumentHooks.currentTimestamp();
67+
InstrumentHooks.stopBenchmark();
68+
InstrumentHooks.setExecutedBenchmark(
69+
process.pid,
70+
this.getTaskUri(task),
71+
);
72+
if (this.runStart !== null) {
73+
this.sendBenchmarkMarkers(this.runStart, runEnd);
74+
}
75+
6576
this.runStart = null;
6677
}
6778
return userTeardown(task, mode);

0 commit comments

Comments
 (0)