-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunner.ts
More file actions
144 lines (118 loc) · 4.42 KB
/
runner.ts
File metadata and controls
144 lines (118 loc) · 4.42 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
getGitDir,
logDebug,
Measurement,
mongoMeasurement,
optimizeFunction,
setupCore,
teardownCore,
} from "@codspeed/core";
import path from "path";
import { Benchmark, chai, Suite, Task } from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { getBenchFn, getHooks } from "vitest/suite";
type SuiteHooks = ReturnType<typeof getHooks>;
function getSuiteHooks(suite: Suite, name: keyof SuiteHooks) {
return getHooks(suite)?.[name] ?? [];
}
export async function callSuiteHook<T extends keyof SuiteHooks>(
suite: Suite,
currentTask: Task,
name: T
): Promise<void> {
if (name === "beforeEach" && suite?.suite) {
await callSuiteHook(suite.suite, currentTask, name);
}
const hooks = getSuiteHooks(suite, name);
await Promise.all(hooks.map((fn) => fn()));
if (name === "afterEach" && suite?.suite) {
await callSuiteHook(suite.suite, currentTask, name);
}
}
const currentFileName =
typeof __filename === "string"
? __filename
: new URL("runner.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 runBench(benchmark: Benchmark, currentSuiteName: string) {
const uri = `${currentSuiteName}::${benchmark.name}`;
const fn = getBenchFn(benchmark);
await callSuiteHook(benchmark.suite, benchmark, "beforeEach");
try {
await optimizeFunction(fn);
} catch (e) {
// if the error is not an assertion error, we want to fail the run
// we allow assertion errors because we want to be able to use `expect` in the benchmark to allow for better authoring
// assertions are allowed to fail in the optimization phase since it might be linked to stateful code
if (!(e instanceof chai.AssertionError)) {
throw e;
}
}
await callSuiteHook(benchmark.suite, benchmark, "afterEach");
await callSuiteHook(benchmark.suite, benchmark, "beforeEach");
await mongoMeasurement.start(uri);
global.gc?.();
await (async function __codspeed_root_frame__() {
Measurement.startInstrumentation();
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
Measurement.stopInstrumentation(uri);
})();
await mongoMeasurement.stop(uri);
await callSuiteHook(benchmark.suite, benchmark, "afterEach");
logCodSpeed(`${uri} done`);
}
async function runBenchmarkSuite(suite: Suite, parentSuiteName?: string) {
await callSuiteHook(suite, suite, "setup");
const currentSuiteName = parentSuiteName
? parentSuiteName + "::" + suite.name
: suite.name;
// do not call `beforeAll` if we are in the root suite, since it is already called by vitest
// see https://github.com/vitest-dev/vitest/blob/1fee63f2598edc228017f18eca325f85ee54aee0/packages/runner/src/run.ts#L293
if (parentSuiteName !== undefined) {
await callSuiteHook(suite, suite, "beforeAll");
}
for (const task of suite.tasks) {
if (task.mode !== "run") continue;
if (task.meta?.benchmark) {
await runBench(task as Benchmark, currentSuiteName);
} else if (task.type === "suite") {
await runBenchmarkSuite(task, currentSuiteName);
}
}
// do not call `afterAll` if we are in the root suite, since it is already called by vitest
// see https://github.com/vitest-dev/vitest/blob/1fee63f2598edc228017f18eca325f85ee54aee0/packages/runner/src/run.ts#L324
if (parentSuiteName !== undefined) {
await callSuiteHook(suite, suite, "afterAll");
}
await callSuiteHook(suite, suite, "teardown");
}
function patchRootSuiteWithFullFilePath(suite: Suite) {
if (suite.filepath === undefined) {
throw new Error("filepath is undefined is the root suite");
}
const gitDir = getGitDir(suite.filepath);
if (gitDir === undefined) {
throw new Error("Could not find a git repository");
}
suite.name = path.relative(gitDir, suite.filepath);
}
class CodSpeedRunner extends NodeBenchmarkRunner {
async runSuite(suite: Suite): Promise<void> {
logDebug(`PROCESS PID: ${process.pid} in ${currentFileName}`);
setupCore();
patchRootSuiteWithFullFilePath(suite);
logCodSpeed(`running suite ${suite.name}`);
await runBenchmarkSuite(suite);
logCodSpeed(`running suite ${suite.name} done`);
teardownCore();
}
}
export default CodSpeedRunner;