Skip to content

Commit e08c11f

Browse files
committed
chore(tinybench): emit benchmark markers
1 parent 59f3bb4 commit e08c11f

3 files changed

Lines changed: 34 additions & 27 deletions

File tree

packages/tinybench-plugin/src/shared.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { InstrumentHooks, setupCore, teardownCore } from "@codspeed/core";
1+
import {
2+
getInstrumentMode,
3+
InstrumentHooks,
4+
MARKER_TYPE_BENCHMARK_END,
5+
MARKER_TYPE_BENCHMARK_START,
6+
setupCore,
7+
teardownCore,
8+
} from "@codspeed/core";
29
import { Bench, Fn, Task } from "tinybench";
310
import { getTaskUri } from "./uri";
411

@@ -63,25 +70,43 @@ export abstract class BaseBenchRunner {
6370
protected abstract finalizeAsyncRun(): Task[];
6471
protected abstract finalizeSyncRun(): Task[];
6572

73+
private sendRunMarkers(runStart: bigint, runEnd: bigint): void {
74+
if (getInstrumentMode() !== "walltime") {
75+
return;
76+
}
77+
InstrumentHooks.addMarker(
78+
process.pid,
79+
MARKER_TYPE_BENCHMARK_START,
80+
runStart,
81+
);
82+
InstrumentHooks.addMarker(process.pid, MARKER_TYPE_BENCHMARK_END, runEnd);
83+
}
84+
6685
public setupBenchMethods(): void {
6786
this.bench.run = async () => {
6887
this.setupBenchRun();
6988

89+
const runStart = InstrumentHooks.currentTimestamp();
7090
for (const task of this.bench.tasks) {
7191
const uri = this.getTaskUri(task);
7292
await this.runTaskAsync(task, uri);
7393
}
94+
const runEnd = InstrumentHooks.currentTimestamp();
95+
this.sendRunMarkers(runStart, runEnd);
7496

7597
return this.finalizeAsyncRun();
7698
};
7799

78100
this.bench.runSync = () => {
79101
this.setupBenchRun();
80102

103+
const runStart = InstrumentHooks.currentTimestamp();
81104
for (const task of this.bench.tasks) {
82105
const uri = this.getTaskUri(task);
83106
this.runTaskSync(task, uri);
84107
}
108+
const runEnd = InstrumentHooks.currentTimestamp();
109+
this.sendRunMarkers(runStart, runEnd);
85110

86111
return this.finalizeSyncRun();
87112
};

packages/tinybench-plugin/tests/index.integ.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const mockCore = vi.hoisted(() => {
1616
startBenchmark: vi.fn(),
1717
stopBenchmark: vi.fn(),
1818
setExecutedBenchmark: vi.fn(),
19+
currentTimestamp: vi.fn().mockReturnValue(0n),
20+
addMarker: vi.fn(),
1921
},
2022
optimizeFunction: vi
2123
.fn()

packages/vitest-plugin/src/walltime/index.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
setupCore,
66
writeWalltimeResults,
77
} from "@codspeed/core";
8-
import { Fn } from "tinybench";
98
import {
109
RunnerTaskEventPack,
1110
RunnerTaskResultPack,
@@ -78,37 +77,18 @@ export class WalltimeRunner extends NodeBenchmarkRunner {
7877
};
7978

8079
tinybench.Task.prototype.run = async function () {
81-
const { fn } = this as { fn: Fn };
8280
const suiteUri = getSuiteUri();
8381

84-
const finishRound = (roundStart: bigint): void => {
85-
const roundEnd = InstrumentHooks.currentTimestamp();
86-
InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_START, roundStart);
87-
InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_END, roundEnd);
88-
};
89-
90-
function __codspeed_root_frame__(): unknown {
91-
const roundStart = InstrumentHooks.currentTimestamp();
92-
const result = fn();
93-
if (
94-
result !== null &&
95-
typeof result === "object" &&
96-
typeof (result as PromiseLike<unknown>).then === "function"
97-
) {
98-
return (result as PromiseLike<unknown>).then((value) => {
99-
finishRound(roundStart);
100-
return value;
101-
});
102-
}
103-
finishRound(roundStart);
104-
return result;
105-
}
106-
(this as { fn: Fn }).fn = __codspeed_root_frame__ as Fn;
107-
10882
InstrumentHooks.startBenchmark();
83+
const runStart = InstrumentHooks.currentTimestamp();
10984
await originalRun.call(this);
85+
const runEnd = InstrumentHooks.currentTimestamp();
11086
InstrumentHooks.stopBenchmark();
11187

88+
// Emit a single marker pair covering the whole measurement run
89+
InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_START, runStart);
90+
InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_END, runEnd);
91+
11292
// Look up the URI by task name
11393
const uri = `${suiteUri}::${this.name}`;
11494
InstrumentHooks.setExecutedBenchmark(pid, uri);

0 commit comments

Comments
 (0)