-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathshared.ts
More file actions
118 lines (100 loc) · 3.12 KB
/
Copy pathshared.ts
File metadata and controls
118 lines (100 loc) · 3.12 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
import {
getInstrumentMode,
InstrumentHooks,
MARKER_TYPE_BENCHMARK_END,
MARKER_TYPE_BENCHMARK_START,
setupCore,
teardownCore,
} from "@codspeed/core";
import { Bench, Fn, Task } from "tinybench";
import { getTaskUri } from "./uri";
declare const __VERSION__: string;
export abstract class BaseBenchRunner {
protected bench: Bench;
protected rootCallingFile: string;
constructor(bench: Bench, rootCallingFile: string) {
this.bench = bench;
this.rootCallingFile = rootCallingFile;
}
private setupBenchRun(): void {
setupCore();
this.logStart();
}
private logStart(): void {
console.log(
`[CodSpeed] running with @codspeed/tinybench v${__VERSION__} (${this.getModeName()})`,
);
}
protected getTaskUri(task: Task): string {
return getTaskUri(this.bench, task.name, this.rootCallingFile);
}
protected logTaskCompletion(uri: string, status: string): void {
console.log(`[CodSpeed] ${status} ${uri}`);
}
protected finalizeBenchRun(): Task[] {
teardownCore();
console.log(`[CodSpeed] Done running ${this.bench.tasks.length} benches.`);
return this.bench.tasks;
}
protected wrapWithInstrumentHooks<T>(fn: () => T, uri: string): T {
InstrumentHooks.startBenchmark();
const runStart = InstrumentHooks.currentTimestamp();
try {
return fn();
} finally {
const runEnd = InstrumentHooks.currentTimestamp();
InstrumentHooks.stopBenchmark();
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
this.sendBenchmarkMarkers(runStart, runEnd);
}
}
protected async wrapWithInstrumentHooksAsync(
fn: Fn,
uri: string,
): Promise<unknown> {
InstrumentHooks.startBenchmark();
const runStart = InstrumentHooks.currentTimestamp();
try {
return await fn();
} finally {
const runEnd = InstrumentHooks.currentTimestamp();
InstrumentHooks.stopBenchmark();
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
this.sendBenchmarkMarkers(runStart, runEnd);
}
}
protected abstract getModeName(): string;
protected abstract runTaskAsync(task: Task, uri: string): Promise<void>;
protected abstract runTaskSync(task: Task, uri: string): void;
protected abstract finalizeAsyncRun(): Task[];
protected abstract finalizeSyncRun(): Task[];
private sendBenchmarkMarkers(runStart: bigint, runEnd: bigint): void {
if (getInstrumentMode() !== "walltime") {
return;
}
InstrumentHooks.addMarker(
process.pid,
MARKER_TYPE_BENCHMARK_START,
runStart,
);
InstrumentHooks.addMarker(process.pid, MARKER_TYPE_BENCHMARK_END, runEnd);
}
public setupBenchMethods(): void {
this.bench.run = async () => {
this.setupBenchRun();
for (const task of this.bench.tasks) {
const uri = this.getTaskUri(task);
await this.runTaskAsync(task, uri);
}
return this.finalizeAsyncRun();
};
this.bench.runSync = () => {
this.setupBenchRun();
for (const task of this.bench.tasks) {
const uri = this.getTaskUri(task);
this.runTaskSync(task, uri);
}
return this.finalizeSyncRun();
};
}
}