MCPcopy
hub / github.com/webpack/tapable / withCodSpeed

Function withCodSpeed

benchmark/with-codspeed.mjs:87–180  ·  view source on GitHub ↗
(bench)

Source from the content-addressed store, hash-verified

85 * @returns {Bench}
86 */
87export 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();

Callers 1

run.mjsFile · 0.90

Calls 6

getOrCreateMetaFunction · 0.85
getCallingFileFunction · 0.85
setupFunction · 0.85
wrapFrameFunction · 0.85
teardownFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…