Skip to content

Commit 6ad82ca

Browse files
committed
chore(vitest): emit benchmark markers
1 parent 079e488 commit 6ad82ca

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

  • packages/vitest-plugin/src/walltime

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
InstrumentHooks,
3+
MARKER_TYPE_BENCHMARK_END,
4+
MARKER_TYPE_BENCHMARK_START,
35
setupCore,
46
writeWalltimeResults,
57
} from "@codspeed/core";
@@ -66,6 +68,7 @@ export class WalltimeRunner extends NodeBenchmarkRunner {
6668
this.isTinybenchHookedWithCodspeed = true;
6769

6870
const originalRun = tinybench.Task.prototype.run;
71+
const pid = process.pid;
6972

7073
const getSuiteUri = (): string => {
7174
if (this.currentSuiteId === null) {
@@ -78,18 +81,37 @@ export class WalltimeRunner extends NodeBenchmarkRunner {
7881
const { fn } = this as { fn: Fn };
7982
const suiteUri = getSuiteUri();
8083

81-
function __codspeed_root_frame__() {
82-
return fn();
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;
83105
}
84-
(this as { fn: Fn }).fn = __codspeed_root_frame__;
106+
(this as { fn: Fn }).fn = __codspeed_root_frame__ as Fn;
85107

86108
InstrumentHooks.startBenchmark();
87109
await originalRun.call(this);
88110
InstrumentHooks.stopBenchmark();
89111

90112
// Look up the URI by task name
91113
const uri = `${suiteUri}::${this.name}`;
92-
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
114+
InstrumentHooks.setExecutedBenchmark(pid, uri);
93115

94116
return this;
95117
};

0 commit comments

Comments
 (0)