-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.mjs
More file actions
169 lines (145 loc) · 4.03 KB
/
benchmark.mjs
File metadata and controls
169 lines (145 loc) · 4.03 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Simple benchmark for rescript-signals
import { Signal, Computed, Effect } from './src/Signals.res.mjs';
import { writeFileSync, mkdirSync, existsSync } from 'fs';
const results = [];
function benchmark(name, fn, iterations = 10000) {
// Warmup
for (let i = 0; i < 10; i++) fn();
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fn();
}
const end = performance.now();
const total = end - start;
const perOp = total / iterations;
const opsPerSec = 1000 / perOp;
console.log(`${name}: ${total.toFixed(2)}ms total, ${perOp.toFixed(4)}ms/op`);
results.push({
name,
iterations,
totalMs: Number(total.toFixed(2)),
msPerOp: Number(perOp.toFixed(4)),
opsPerSec: Math.round(opsPerSec),
});
return total;
}
console.log('\n=== ReScript Signals Benchmark ===\n');
// Test 1: Create Signals
console.log('--- Signal Creation ---');
benchmark('Create 10000 signals', () => {
const signals = [];
for (let i = 0; i < 10000; i++) {
signals.push(Signal.make(i));
}
}, 100);
// Test 2: Create Computeds
console.log('\n--- Computed Creation ---');
benchmark('Create 10000 computeds (simple)', () => {
const signals = [];
const computeds = [];
for (let i = 0; i < 10000; i++) {
const s = Signal.make(i);
signals.push(s);
computeds.push(Computed.make(() => Signal.get(s) * 2));
}
}, 100);
// Test 3: Deep computed chain
console.log('\n--- Deep Computed Chain ---');
benchmark('Create chain of 100 computeds', () => {
const source = Signal.make(1);
let prev = source;
for (let i = 0; i < 100; i++) {
const current = prev;
prev = Computed.make(() => Signal.get(current) + 1);
}
// Read the final value to ensure chain is evaluated
Signal.get(prev);
}, 100);
// Test 4: Signal updates
console.log('\n--- Signal Updates ---');
{
const source = Signal.make(0);
const computeds = [];
for (let i = 0; i < 100; i++) {
computeds.push(Computed.make(() => Signal.get(source) * 2));
}
benchmark('Update signal with 100 computed observers', () => {
Signal.set(source, Signal.get(source) + 1);
// Read all computeds to force evaluation
for (const c of computeds) {
Signal.get(c);
}
}, 10000);
}
// Test 5: Wide dependency tree
console.log('\n--- Wide Dependency Tree ---');
{
const sources = [];
for (let i = 0; i < 100; i++) {
sources.push(Signal.make(i));
}
const computed = Computed.make(() => {
let sum = 0;
for (const s of sources) {
sum += Signal.get(s);
}
return sum;
});
benchmark('Update 1 of 100 source signals', () => {
Signal.set(sources[0], Signal.get(sources[0]) + 1);
Signal.get(computed);
}, 10000);
}
// Test 6: Batched updates
console.log('\n--- Batched Updates ---');
{
const signals = [];
for (let i = 0; i < 100; i++) {
signals.push(Signal.make(i));
}
const computed = Computed.make(() => {
let sum = 0;
for (const s of signals) {
sum += Signal.get(s);
}
return sum;
});
benchmark('Batch update 100 signals', () => {
Signal.batch(() => {
for (let i = 0; i < 100; i++) {
Signal.set(signals[i], Signal.get(signals[i]) + 1);
}
});
Signal.get(computed);
}, 10000);
}
// Test 7: Effect with updates
console.log('\n--- Effects ---');
{
let effectCount = 0;
const source = Signal.make(0);
const effect = Effect.run(() => {
Signal.get(source);
effectCount++;
return undefined;
});
benchmark('Signal update triggering effect', () => {
Signal.set(source, Signal.get(source) + 1);
}, 10000);
effect.dispose();
}
console.log('\n=== Benchmark Complete ===\n');
// Save results to file
const resultsDir = './benchmark-results';
if (!existsSync(resultsDir)) {
mkdirSync(resultsDir);
}
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${resultsDir}/benchmark-${timestamp}.json`;
const output = {
timestamp: new Date().toISOString(),
nodeVersion: process.version,
results,
};
writeFileSync(filename, JSON.stringify(output, null, 2));
console.log(`Results saved to ${filename}`);