|
1 | | -import { performance, PerformanceObserver } from "node:perf_hooks"; |
| 1 | +import { performance } from "node:perf_hooks"; |
2 | 2 | import { lru } from "../dist/tiny-lru.js"; |
3 | 3 |
|
4 | | -// Performance observer for function timing |
5 | | -class LRUPerformanceProfiler { |
6 | | - constructor () { |
7 | | - this.entries = []; |
8 | | - this.observer = new PerformanceObserver(items => { |
9 | | - const batch = items.getEntries(); |
10 | | - |
11 | | - // Debug log of raw PerformanceObserver function entries |
12 | | - console.log("PerformanceObserver function batch size:", batch.length); |
13 | | - if (batch.length > 0) { |
14 | | - console.log("PerformanceObserver function entries:", batch); |
15 | | - } |
16 | | - |
17 | | - batch.forEach(entry => { |
18 | | - this.entries.push(entry); |
19 | | - }); |
20 | | - }); |
21 | | - this.observer.observe({ entryTypes: ["function"] }); |
22 | | - } |
23 | | - |
24 | | - timerify (fn, name) { |
25 | | - const wrappedFn = performance.timerify(fn); |
26 | | - // Override the name for better reporting (safely handle non-configurable name property) |
27 | | - try { |
28 | | - Object.defineProperty(wrappedFn, "name", { value: name, configurable: true }); |
29 | | - } catch (error) { // eslint-disable-line no-unused-vars |
30 | | - // If we can't redefine the name property, create a wrapper with the desired name |
31 | | - const namedWrapper = { |
32 | | - [name]: (...args) => wrappedFn(...args) |
33 | | - }[name]; |
34 | | - |
35 | | - return namedWrapper; |
36 | | - } |
37 | | - |
38 | | - return wrappedFn; |
39 | | - } |
40 | | - |
41 | | - getResults () { |
42 | | - const results = new Map(); |
43 | | - |
44 | | - this.entries.forEach(entry => { |
45 | | - if (!results.has(entry.name)) { |
46 | | - results.set(entry.name, { |
47 | | - name: entry.name, |
48 | | - calls: 0, |
49 | | - totalTime: 0, |
50 | | - minTime: Infinity, |
51 | | - maxTime: 0, |
52 | | - times: [] |
53 | | - }); |
54 | | - } |
55 | | - |
56 | | - const result = results.get(entry.name); |
57 | | - result.calls++; |
58 | | - result.totalTime += entry.duration; |
59 | | - result.minTime = Math.min(result.minTime, entry.duration); |
60 | | - result.maxTime = Math.max(result.maxTime, entry.duration); |
61 | | - result.times.push(entry.duration); |
62 | | - }); |
63 | | - |
64 | | - // Calculate statistics |
65 | | - results.forEach(result => { |
66 | | - result.avgTime = result.totalTime / result.calls; |
67 | | - |
68 | | - // Calculate standard deviation |
69 | | - const variance = result.times.reduce((acc, time) => { |
70 | | - return acc + Math.pow(time - result.avgTime, 2); |
71 | | - }, 0) / result.calls; |
72 | | - result.stdDev = Math.sqrt(variance); |
73 | | - |
74 | | - // Calculate median |
75 | | - const sorted = [...result.times].sort((a, b) => a - b); |
76 | | - const mid = Math.floor(sorted.length / 2); |
77 | | - result.median = sorted.length % 2 === 0 ? |
78 | | - (sorted[mid - 1] + sorted[mid]) / 2 : |
79 | | - sorted[mid]; |
80 | | - |
81 | | - // Operations per second (rough estimate) |
82 | | - result.opsPerSec = result.calls / (result.totalTime / 1000); // duration is in ms |
83 | | - }); |
84 | | - |
85 | | - return Array.from(results.values()); |
86 | | - } |
87 | | - |
88 | | - printResults () { |
89 | | - const results = this.getResults(); |
90 | | - console.log("\n📊 Performance Observer Results"); |
91 | | - console.log("================================"); |
92 | | - |
93 | | - if (results.length === 0) { |
94 | | - console.log("No function performance entries were recorded by the Performance Observer."); |
95 | | - console.log("Raw PerformanceObserver entries:", this.entries); |
96 | | - |
97 | | - return; |
98 | | - } |
99 | | - |
100 | | - console.table(results.map(r => ({ |
101 | | - "Function": r.name, |
102 | | - "Calls": r.calls, |
103 | | - "Avg (ms)": r.avgTime.toFixed(4), |
104 | | - "Min (ms)": r.minTime.toFixed(4), |
105 | | - "Max (ms)": r.maxTime.toFixed(4), |
106 | | - "Median (ms)": r.median.toFixed(4), |
107 | | - "Std Dev": r.stdDev.toFixed(4), |
108 | | - "Ops/sec": Math.round(r.opsPerSec) |
109 | | - }))); |
110 | | - } |
111 | | - |
112 | | - disconnect () { |
113 | | - this.observer.disconnect(); |
114 | | - } |
115 | | - |
116 | | - reset () { |
117 | | - this.entries = []; |
118 | | - } |
119 | | -} |
120 | | - |
121 | 4 | // Test data generation |
122 | 5 | function generateTestData (size) { |
123 | 6 | const out = new Array(size); |
@@ -479,6 +362,5 @@ export { |
479 | 362 | runPerformanceObserverBenchmarks, |
480 | 363 | runCustomTimerBenchmarks, |
481 | 364 | runScalabilityTest, |
482 | | - LRUPerformanceProfiler, |
483 | 365 | CustomTimer |
484 | 366 | }; |
0 commit comments