-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanalysis.ts
More file actions
86 lines (70 loc) · 2.31 KB
/
analysis.ts
File metadata and controls
86 lines (70 loc) · 2.31 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
import {
getCodspeedRunnerMode,
InstrumentHooks,
mongoMeasurement,
optimizeFunction,
} from "@codspeed/core";
import { Bench, Fn, FnOptions, Task } from "tinybench";
import { BaseBenchRunner } from "./shared";
export function setupCodspeedAnalysisBench(
bench: Bench,
rootCallingFile: string
): void {
const runner = new AnalysisBenchRunner(bench, rootCallingFile);
runner.setupBenchMethods();
}
class AnalysisBenchRunner extends BaseBenchRunner {
protected getModeName(): string {
const runnerMode = getCodspeedRunnerMode();
return `${runnerMode} mode`;
}
private taskCompletionMessage() {
return InstrumentHooks.isInstrumented() ? "Measured" : "Checked";
}
private wrapFunctionWithFrame(fn: Fn, isAsync: boolean): Fn {
if (isAsync) {
return async function __codspeed_root_frame__() {
await fn();
};
} else {
return function __codspeed_root_frame__() {
fn();
};
}
}
protected async runTaskAsync(task: Task, uri: string): Promise<void> {
const { fnOpts, fn } = task as unknown as { fnOpts?: FnOptions; fn: Fn };
await fnOpts?.beforeAll?.call(task, "run");
await optimizeFunction(async () => {
await fnOpts?.beforeEach?.call(task, "run");
await fn();
await fnOpts?.afterEach?.call(task, "run");
});
await fnOpts?.beforeEach?.call(task, "run");
await mongoMeasurement.start(uri);
global.gc?.();
await this.wrapWithInstrumentHooksAsync(
this.wrapFunctionWithFrame(fn, true),
uri
);
await mongoMeasurement.stop(uri);
await fnOpts?.afterEach?.call(task, "run");
await fnOpts?.afterAll?.call(task, "run");
this.logTaskCompletion(uri, this.taskCompletionMessage());
}
protected runTaskSync(task: Task, uri: string): void {
const { fnOpts, fn } = task as unknown as { fnOpts?: FnOptions; fn: Fn };
fnOpts?.beforeAll?.call(task, "run");
fnOpts?.beforeEach?.call(task, "run");
this.wrapWithInstrumentHooks(this.wrapFunctionWithFrame(fn, false), uri);
fnOpts?.afterEach?.call(task, "run");
fnOpts?.afterAll?.call(task, "run");
this.logTaskCompletion(uri, this.taskCompletionMessage());
}
protected finalizeAsyncRun(): Task[] {
return this.finalizeBenchRun();
}
protected finalizeSyncRun(): Task[] {
return this.finalizeBenchRun();
}
}