Skip to content

Commit 62e169f

Browse files
--wip-- [skip ci]
1 parent 6e81853 commit 62e169f

2 files changed

Lines changed: 10 additions & 139 deletions

File tree

packages/tinybench-plugin/.codspeed/790993.json

Lines changed: 0 additions & 87 deletions
This file was deleted.

packages/tinybench-plugin/src/walltime.ts

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,21 @@ import {
88
type Benchmark,
99
type BenchmarkStats,
1010
} from "@codspeed/core";
11-
import { Bench, TaskResult } from "tinybench";
11+
import { Bench, Task, TaskResult } from "tinybench";
1212
import { getTaskUri } from "./uri";
1313

1414
declare const __VERSION__: string;
1515

16-
class CodespeedFrame {
17-
public readonly suffix: string;
18-
public readonly id: number;
19-
public readonly timestamp: number;
20-
public readonly methodName: string;
21-
22-
constructor(suffix: string) {
23-
this.suffix = this.sanitizeIdentifier(suffix);
24-
this.id = Math.random();
25-
this.timestamp = Date.now();
26-
27-
const methodName = `__codspeed_root_frame__${this.suffix}`;
28-
this.methodName = methodName;
29-
30-
// Create a named function dynamically using eval
31-
const functionBody = `
32-
async function ${methodName}() {
33-
InstrumentHooks.startBenchmark();
34-
const result = await task.run();
35-
InstrumentHooks.stopBenchmark();
36-
return result;
37-
}
38-
return ${methodName};
39-
`;
40-
41-
// Type assertion to tell TypeScript about the dynamic method
42-
(this as any)[methodName] = eval(`(${functionBody})`);
43-
}
44-
45-
private sanitizeIdentifier(input: string): string {
46-
return (
47-
input
48-
// Replace invalid characters with underscores
49-
.replace(/[^a-zA-Z0-9_$]/g, "_")
50-
// Ensure it doesn't start with a number
51-
.replace(/^[0-9]/, "_$&")
52-
// Collapse multiple underscores
53-
.replace(/_+/g, "_")
54-
// Remove trailing underscores
55-
.replace(/_+$/, "") ||
56-
// Ensure it's not empty
57-
"_default"
58-
);
59-
}
16+
class CodspeedFrame {
17+
private task: Task;
6018

61-
async call(): Promise<any> {
62-
return await (this as any)[this.methodName]();
19+
constructor(task: Task) {
20+
this.task = task;
6321
}
6422

65-
// Get the actual function for direct calling
66-
getFunction(): () => Promise<any> {
67-
return (this as any)[this.methodName];
23+
// Make the instance callable by delegating to the dynamic method
24+
call() {
25+
return this.task.run();
6826
}
6927
}
7028

@@ -96,8 +54,8 @@ export function runWalltimeBench(bench: Bench, rootCallingFile: string): void {
9654
await task.warmup();
9755
}
9856
await mongoMeasurement.start(uri);
99-
const frame = new CodespeedFrame(uri);
100-
const taskResult = await frame.call();
57+
const __cosdspeed_root_frame__ = new CodspeedFrame(task);
58+
const taskResult = await __cosdspeed_root_frame__.call();
10159
await mongoMeasurement.stop(uri);
10260
results.push(taskResult);
10361

0 commit comments

Comments
 (0)