|
3 | 3 | MARKER_TYPE_BENCHMARK_END, |
4 | 4 | MARKER_TYPE_BENCHMARK_START, |
5 | 5 | setupCore, |
| 6 | + wrapWithRootFrame, |
6 | 7 | writeWalltimeResults, |
7 | 8 | } from "@codspeed/core"; |
8 | | -import { Fn } from "tinybench"; |
9 | 9 | import { |
10 | 10 | RunnerTaskEventPack, |
11 | 11 | RunnerTaskResultPack, |
@@ -78,37 +78,27 @@ export class WalltimeRunner extends NodeBenchmarkRunner { |
78 | 78 | }; |
79 | 79 |
|
80 | 80 | tinybench.Task.prototype.run = async function () { |
81 | | - const { fn } = this as { fn: Fn }; |
82 | 81 | const suiteUri = getSuiteUri(); |
83 | 82 |
|
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; |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 84 | + const task = this as any; |
| 85 | + const originalFn = task.fn; |
| 86 | + task.fn = wrapWithRootFrame(() => originalFn.call(task)); |
107 | 87 |
|
108 | 88 | InstrumentHooks.startBenchmark(); |
109 | | - await originalRun.call(this); |
| 89 | + const runStart = InstrumentHooks.currentTimestamp(); |
| 90 | + try { |
| 91 | + await originalRun.call(this); |
| 92 | + } finally { |
| 93 | + task.fn = originalFn; |
| 94 | + } |
| 95 | + const runEnd = InstrumentHooks.currentTimestamp(); |
110 | 96 | InstrumentHooks.stopBenchmark(); |
111 | 97 |
|
| 98 | + // Emit a single marker pair covering the whole measurement run |
| 99 | + InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_START, runStart); |
| 100 | + InstrumentHooks.addMarker(pid, MARKER_TYPE_BENCHMARK_END, runEnd); |
| 101 | + |
112 | 102 | // Look up the URI by task name |
113 | 103 | const uri = `${suiteUri}::${this.name}`; |
114 | 104 | InstrumentHooks.setExecutedBenchmark(pid, uri); |
|
0 commit comments