Skip to content

Commit 762c0d8

Browse files
committed
chore(tinybench): emit benchmark markers
1 parent 374a563 commit 762c0d8

2 files changed

Lines changed: 28 additions & 1 deletion

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()

0 commit comments

Comments
 (0)