Skip to content

Commit ad2afb2

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

3 files changed

Lines changed: 42 additions & 58 deletions

File tree

packages/tinybench-plugin/src/analysis.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ 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+
try {
46+
await rootFrame();
47+
} finally {
48+
InstrumentHooks.stopBenchmark();
49+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
50+
}
4451

4552
await mongoMeasurement.stop(uri);
4653
await fnOpts?.afterEach?.call(task, "run");
@@ -55,7 +62,14 @@ class AnalysisBenchRunner extends BaseBenchRunner {
5562
fnOpts?.beforeAll?.call(task, "run");
5663
fnOpts?.beforeEach?.call(task, "run");
5764

58-
this.wrapWithInstrumentHooks(wrapWithRootFrameSync(fn), uri);
65+
const rootFrame = wrapWithRootFrameSync(fn);
66+
InstrumentHooks.startBenchmark();
67+
try {
68+
rootFrame();
69+
} finally {
70+
InstrumentHooks.stopBenchmark();
71+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
72+
}
5973

6074
fnOpts?.afterEach?.call(task, "run");
6175
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)