|
1 | | -import type { |
2 | | - BenchmarkFn, |
3 | | - BenchmarkResult, |
4 | | - Challenger, |
5 | | - ImportedBenchmark, |
6 | | - SuiteState, |
7 | | -} from '../types/benchmarks'; |
8 | | -import { calculateTimes } from './utils'; |
| 1 | +import type { Bench } from 'tinybench'; |
| 2 | +import type { BenchFn, BenchmarkResult, SuiteState } from '../types/benchmarks'; |
9 | 3 |
|
10 | 4 | export class BenchmarkSuite { |
11 | 5 | name: string; |
12 | 6 | enabled: boolean; |
13 | | - benchmarks: Benchmark[]; |
| 7 | + benchmarks: BenchFn[]; |
14 | 8 | state: SuiteState; |
15 | 9 | results: BenchmarkResult[] = []; |
| 10 | + notes?: Record<string, string>; |
16 | 11 |
|
17 | | - constructor(name: string) { |
| 12 | + constructor( |
| 13 | + name: string, |
| 14 | + benchmarks: BenchFn[], |
| 15 | + notes?: Record<string, string>, |
| 16 | + ) { |
18 | 17 | this.name = name; |
19 | 18 | this.enabled = false; |
20 | 19 | this.state = 'idle'; |
21 | | - this.benchmarks = []; |
| 20 | + this.benchmarks = benchmarks; |
22 | 21 | this.results = []; |
23 | | - } |
24 | | - |
25 | | - addBenchmark(imported: ImportedBenchmark) { |
26 | | - this.benchmarks.push(new Benchmark(imported)); |
| 22 | + this.notes = notes; |
27 | 23 | } |
28 | 24 |
|
29 | 25 | addResult(result: BenchmarkResult) { |
30 | 26 | this.results.push(result); |
31 | 27 | } |
32 | 28 |
|
33 | | - run(multiplier: number = 1) { |
| 29 | + async run() { |
34 | 30 | this.results = []; |
35 | | - this.benchmarks.forEach(benchmark => { |
36 | | - benchmark.run(this, multiplier); |
| 31 | + const promises = this.benchmarks.map(async benchFn => { |
| 32 | + const b = await benchFn(); |
| 33 | + await b.run(); |
| 34 | + this.processResults(b); |
| 35 | + this.state = 'done'; |
37 | 36 | }); |
| 37 | + await Promise.all(promises); |
38 | 38 | } |
39 | | -} |
40 | 39 |
|
41 | | -export class Benchmark { |
42 | | - name: string; // function name |
43 | | - runCount: number; |
44 | | - us?: BenchmarkFn; |
45 | | - them: Challenger[]; |
46 | | - |
47 | | - constructor(benchmark: ImportedBenchmark) { |
48 | | - this.name = benchmark.name; |
49 | | - this.runCount = benchmark.runCount; |
50 | | - this.us = benchmark.us; |
51 | | - this.them = benchmark.them; |
52 | | - } |
| 40 | + processResults = (b: Bench): void => { |
| 41 | + const tasks = b.tasks; |
| 42 | + const us = tasks.find(t => t.name === 'rnqc'); |
| 43 | + const themTasks = tasks.filter(t => t.name !== 'rnqc'); |
53 | 44 |
|
54 | | - run(suite: BenchmarkSuite, multiplier: number = 1) { |
55 | | - const usTime = this.timeFn(this.us!, multiplier); |
56 | | - this.them.forEach(them => { |
57 | | - const themTime = this.timeFn(them.fn, multiplier); |
58 | | - const type = usTime < themTime ? 'faster' : 'slower'; |
59 | | - const times = calculateTimes(usTime, themTime); |
60 | | - const result: BenchmarkResult = { |
| 45 | + themTasks.map(them => { |
| 46 | + const notes = this.notes?.[them.name] ?? ''; |
| 47 | + this.addResult({ |
61 | 48 | errorMsg: undefined, |
62 | 49 | challenger: them.name, |
63 | | - notes: them.notes, |
64 | | - runCount: this.runCount * multiplier, |
65 | | - fnName: this.name, |
66 | | - time: themTime, |
67 | | - us: usTime, |
68 | | - type, |
69 | | - times, |
70 | | - }; |
71 | | - suite.addResult(result); |
| 50 | + notes, |
| 51 | + benchName: b.name, |
| 52 | + them: them.result, |
| 53 | + us: us?.result, |
| 54 | + }); |
72 | 55 | }); |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * @returns time in ms |
77 | | - */ |
78 | | - timeFn = (fn: BenchmarkFn, multiplier: number = 1): number => { |
79 | | - // warm up imports, etc. |
80 | | - fn(); |
81 | | - |
82 | | - const totalRunCount = this.runCount * multiplier; |
83 | | - |
84 | | - // do the actual benchmark |
85 | | - const start = performance.now(); |
86 | | - for (let i = 0; i < totalRunCount; i++) { |
87 | | - fn(); |
88 | | - } |
89 | | - const end = performance.now(); |
90 | | - return end - start; |
91 | 56 | }; |
92 | 57 | } |
0 commit comments