(bench)
| 85 | * @returns {Bench} |
| 86 | */ |
| 87 | export function withCodSpeed(bench) { |
| 88 | const mode = getCodspeedRunnerMode(); |
| 89 | if (mode === "disabled" || mode === "walltime") return bench; |
| 90 | |
| 91 | // --- simulation mode --- |
| 92 | |
| 93 | const meta = getOrCreateMeta(bench); |
| 94 | const rawAdd = bench.add.bind(bench); |
| 95 | |
| 96 | bench.add = (name, fn, opts) => { |
| 97 | const callingFile = getCallingFile(); |
| 98 | const uri = `${callingFile}::${name}`; |
| 99 | meta.set(name, { uri, fn, opts }); |
| 100 | return rawAdd(name, fn, opts); |
| 101 | }; |
| 102 | |
| 103 | const setup = () => { |
| 104 | setupCore(); |
| 105 | console.log("[CodSpeed] running in simulation mode"); |
| 106 | }; |
| 107 | |
| 108 | const teardown = () => { |
| 109 | teardownCore(); |
| 110 | console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`); |
| 111 | return bench.tasks; |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * @param {Fn} fn |
| 116 | * @param {boolean} isAsync |
| 117 | * @returns {Fn} |
| 118 | */ |
| 119 | const wrapFrame = (fn, isAsync) => { |
| 120 | if (isAsync) { |
| 121 | // eslint-disable-next-line camelcase |
| 122 | return async function __codspeed_root_frame__() { |
| 123 | await fn(); |
| 124 | }; |
| 125 | } |
| 126 | // eslint-disable-next-line camelcase |
| 127 | return function __codspeed_root_frame__() { |
| 128 | fn(); |
| 129 | }; |
| 130 | }; |
| 131 | |
| 132 | bench.run = async () => { |
| 133 | setup(); |
| 134 | for (const task of bench.tasks) { |
| 135 | const m = /** @type {TaskMeta} */ (meta.get(task.name)); |
| 136 | |
| 137 | // Warm-up: run the body a few times to stabilise caches / JIT. |
| 138 | for (let i = 0; i < bench.iterations - 1; i++) { |
| 139 | await m.fn(); |
| 140 | } |
| 141 | |
| 142 | // Instrumented run. |
| 143 | global.gc?.(); |
| 144 | InstrumentHooks.startBenchmark(); |
no test coverage detected
searching dependent graphs…