-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimulation.ts
More file actions
101 lines (85 loc) · 2.82 KB
/
simulation.ts
File metadata and controls
101 lines (85 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
InstrumentHooks,
logDebug,
mongoMeasurement,
optimizeFunction,
setupCore,
teardownCore,
} from "@codspeed/core";
import { Benchmark, type RunnerTestSuite } from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { getBenchFn } from "vitest/suite";
import {
callSuiteHook,
isVitestTaskBenchmark,
patchRootSuiteWithFullFilePath,
} from "./common";
const currentFileName =
typeof __filename === "string"
? __filename
: new URL("simulation.mjs", import.meta.url).pathname;
/**
* @deprecated
* TODO: try to use something like `updateTask` from `@vitest/runner` instead to use the output
* of vitest instead console.log but at the moment, `updateTask` is not exposed
*/
function logCodSpeed(message: string) {
console.log(`[CodSpeed] ${message}`);
}
async function runSimulationBench(
benchmark: Benchmark,
suite: RunnerTestSuite,
currentSuiteName: string
) {
const uri = `${currentSuiteName}::${benchmark.name}`;
const fn = getBenchFn(benchmark);
await optimizeFunction(async () => {
await callSuiteHook(suite, benchmark, "beforeEach");
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
await callSuiteHook(suite, benchmark, "afterEach");
});
await callSuiteHook(suite, benchmark, "beforeEach");
await mongoMeasurement.start(uri);
global.gc?.();
await (async function __codspeed_root_frame__() {
InstrumentHooks.startBenchmark();
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
InstrumentHooks.stopBenchmark();
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
})();
await mongoMeasurement.stop(uri);
await callSuiteHook(suite, benchmark, "afterEach");
logCodSpeed(`${uri} done`);
}
async function runSimulationBenchmarkSuite(
suite: RunnerTestSuite,
parentSuiteName?: string
) {
const currentSuiteName = parentSuiteName
? parentSuiteName + "::" + suite.name
: suite.name;
await callSuiteHook(suite, suite, "beforeAll");
for (const task of suite.tasks) {
if (task.mode !== "run") continue;
if (isVitestTaskBenchmark(task)) {
await runSimulationBench(task, suite, currentSuiteName);
} else if (task.type === "suite") {
await runSimulationBenchmarkSuite(task, currentSuiteName);
}
}
await callSuiteHook(suite, suite, "afterAll");
}
export class SimulationRunner extends NodeBenchmarkRunner {
async runSuite(suite: RunnerTestSuite): Promise<void> {
logDebug(`PROCESS PID: ${process.pid} in ${currentFileName}`);
setupCore();
patchRootSuiteWithFullFilePath(suite);
logCodSpeed(`running suite ${suite.name}`);
await runSimulationBenchmarkSuite(suite);
logCodSpeed(`running suite ${suite.name} done`);
teardownCore();
}
}
export default SimulationRunner;