-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathcalibrate-n.js
More file actions
292 lines (248 loc) Β· 9.65 KB
/
calibrate-n.js
File metadata and controls
292 lines (248 loc) Β· 9.65 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
const path = require('node:path');
const { fork } = require('node:child_process');
const fs = require('node:fs');
const { styleText } = require('node:util');
const DEFAULT_RUNS = 30; // Number of runs for each n value
const CV_THRESHOLD = 0.05; // 5% coefficient of variation threshold
const MAX_N_INCREASE = 6; // Maximum number of times to increase n (10**6)
const INCREASE_FACTOR = 10; // Factor by which to increase n
const args = process.argv.slice(2);
if (args.length === 0) {
console.log(`
Usage: node calibrate-n.js [options] <benchmark_path>
Options:
--runs=N Number of runs for each n value (default: ${DEFAULT_RUNS})
--cv-threshold=N Target coefficient of variation threshold (default: ${CV_THRESHOLD})
--max-increases=N Maximum number of n increases to try (default: ${MAX_N_INCREASE})
--start-n=N Initial n value to start with (default: autodetect)
--increase=N Factor by which to increase n (default: ${INCREASE_FACTOR})
Example:
node calibrate-n.js buffers/buffer-compare.js
node calibrate-n.js --runs=10 --cv-threshold=0.02 buffers/buffer-compare.js
`);
process.exit(1);
}
// Extract options
let benchmarkPath;
let runs = DEFAULT_RUNS;
let cvThreshold = CV_THRESHOLD;
let maxIncreases = MAX_N_INCREASE;
let startN = 10;
let increaseFactor = INCREASE_FACTOR;
for (const arg of args) {
if (arg.startsWith('--runs=')) {
runs = parseInt(arg.substring(7), 10);
} else if (arg.startsWith('--cv-threshold=')) {
cvThreshold = parseFloat(arg.substring(14));
} else if (arg.startsWith('--max-increases=')) {
maxIncreases = parseInt(arg.substring(15), 10);
if (isNaN(maxIncreases)) {
console.error(`Error: Invalid value for --max-increases. Using default: ${MAX_N_INCREASE}`);
maxIncreases = MAX_N_INCREASE;
}
} else if (arg.startsWith('--start-n=')) {
startN = parseInt(arg.substring(10), 10);
if (isNaN(startN)) {
console.error(`Error: Invalid value for --start-n. Using default: 10`);
startN = 10;
}
} else if (arg.startsWith('--increase=')) {
increaseFactor = parseInt(arg.substring(11), 10);
if (isNaN(increaseFactor)) {
console.error(`Error: Invalid value for --increase. Using default: ${INCREASE_FACTOR}`);
increaseFactor = INCREASE_FACTOR;
}
} else {
benchmarkPath = arg;
}
}
if (!benchmarkPath) {
console.error('Error: No benchmark path specified');
process.exit(1);
}
const fullBenchmarkPath = path.resolve(benchmarkPath);
if (!fs.existsSync(fullBenchmarkPath)) {
console.error(`Error: Benchmark file not found: ${fullBenchmarkPath}`);
process.exit(1);
}
function calculateStats(values) {
const mean = values.reduce((sum, val) => sum + val, 0) / values.length;
const squaredDiffs = values.map((val) => {
const diff = val - mean;
const squared = diff ** 2;
return squared;
});
const variance = squaredDiffs.reduce((sum, val) => sum + val, 0) / values.length;
const stdDev = Math.sqrt(variance);
const cv = stdDev / mean;
return { mean, stdDev, cv, variance };
}
function runBenchmark(n) {
return new Promise((resolve, reject) => {
const child = fork(
fullBenchmarkPath,
[`n=${n}`],
{ stdio: ['inherit', 'pipe', 'inherit', 'ipc'] },
);
const results = [];
child.on('message', (data) => {
if (data.type === 'report' && data.rate && data.conf) {
results.push({
rate: data.rate,
conf: data.conf,
});
}
});
child.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Benchmark exited with code ${code}`));
} else {
resolve(results);
}
});
});
}
async function main(n = startN) {
let increaseCount = 0;
let bestN = n;
let bestCV = Infinity;
let bestGroupStats = null;
console.log(`
--------------------------------------------------------
Benchmark: ${benchmarkPath}
--------------------------------------------------------
What we are trying to find: The optimal number of iterations (n)
that produces consistent benchmark results without wasting time.
How it works:
1. Run the benchmark multiple times with a specific n value
2. Group results by configuration
3. If overall CV is above 5% or any configuration has CV above 10%, increase n and try again
Configuration:
- Starting n: ${n.toLocaleString()} iterations
- Runs per n value: ${runs}
- Target CV threshold: ${cvThreshold * 100}% (lower CV = more stable results)
- Max increases: ${maxIncreases}
- Increase factor: ${increaseFactor}x`);
while (increaseCount < maxIncreases) {
console.log(`\nTesting with n=${n}:`);
const resultsData = [];
for (let i = 0; i < runs; i++) {
const results = await runBenchmark(n);
// Each run might return multiple results (one per configuration)
if (Array.isArray(results) && results.length > 0) {
resultsData.push(...results);
} else if (results) {
resultsData.push(results);
}
process.stdout.write('.');
}
process.stdout.write('\n');
const groupedResults = {};
resultsData.forEach((result) => {
if (!result || !result.conf) return;
const confKey = JSON.stringify(result.conf);
groupedResults[confKey] ||= {
conf: result.conf,
rates: [],
};
groupedResults[confKey].rates.push(result.rate);
});
const groupStats = [];
for (const [confKey, group] of Object.entries(groupedResults)) {
console.log(`\nConfiguration: ${JSON.stringify(group.conf)}`);
const stats = calculateStats(group.rates);
console.log(` CV: ${(stats.cv * 100).toFixed(2)}% (lower values mean more stable results)`);
const isStable = stats.cv <= cvThreshold;
console.log(` Stability: ${isStable ?
styleText(['bold', 'green'], 'β Stable') :
styleText(['bold', 'red'], 'β Unstable')}`);
groupStats.push({
confKey,
stats,
isStable,
});
}
if (groupStats.length > 0) {
// Check if any configuration has CV > 10% (too unstable)
const tooUnstableConfigs = groupStats.filter((g) => g.stats.cv > 0.10);
const avgCV = groupStats.reduce((sum, g) => sum + g.stats.cv, 0) / groupStats.length;
console.log(`\nOverall average CV: ${(avgCV * 100).toFixed(2)}%`);
const isOverallStable = avgCV < CV_THRESHOLD;
const hasVeryUnstableConfigs = tooUnstableConfigs.length > 0;
// Check if overall CV is below CV_THRESHOLD and no configuration has CV > 10%
if (isOverallStable && !hasVeryUnstableConfigs) {
console.log(styleText(['bold', 'green'], ` β Overall CV is below 5% and no configuration has CV above 10%`));
} else {
if (!isOverallStable) {
console.log(styleText(['bold', 'red'], ` β Overall CV (${(avgCV * 100).toFixed(2)}%) is above 5%`));
}
if (hasVeryUnstableConfigs) {
console.log(styleText(['bold', 'red'], ` β ${tooUnstableConfigs.length} configuration(s) have CV above 10%`));
}
}
if (avgCV < bestCV || !bestGroupStats) {
bestN = n;
bestCV = avgCV;
bestGroupStats = [];
for (const group of Object.values(groupedResults)) {
if (group.rates.length >= 3) {
const stats = calculateStats(group.rates);
bestGroupStats.push({
conf: group.conf,
stats: stats,
isStable: stats.cv <= 0.10,
});
}
}
console.log(` β New best n: ${n} with average CV: ${(avgCV * 100).toFixed(2)}%`);
} else {
console.log(` β Current best n remains: ${bestN} with average CV: ${(bestCV * 100).toFixed(2)}%`);
}
}
// Check if we've reached acceptable stability based on new criteria
// 1. Overall CV should be below CV_THRESHOLD
// 2. No configuration should have a CV greater than 10%
const avgCV = groupStats.length > 0 ?
groupStats.reduce((sum, g) => sum + g.stats.cv, 0) / groupStats.length : Infinity;
const hasUnstableConfig = groupStats.some((g) => g.stats.cv > 0.10);
const isOverallStable = avgCV < CV_THRESHOLD;
if (isOverallStable && !hasUnstableConfig) {
console.log(`\nβ Found optimal n=${n} (Overall CV=${(avgCV * 100).toFixed(2)}% < 5% and no configuration has CV > 10%)`);
console.log('\nFinal CV for each configuration:');
groupStats.forEach((g) => {
console.log(` ${JSON.stringify(groupedResults[g.confKey].conf)}: ${(g.stats.cv * 100).toFixed(2)}%`);
});
return n;
}
increaseCount++;
n *= increaseFactor;
}
if (increaseCount >= maxIncreases) {
const finalAvgCV = bestGroupStats && bestGroupStats.length > 0 ?
bestGroupStats.reduce((sum, g) => sum + g.stats.cv, 0) / bestGroupStats.length : Infinity;
console.log(`Maximum number of increases (${maxIncreases}) reached without achieving target stability`);
console.log(`Best n found: ${bestN} with average CV=${(finalAvgCV * 100).toFixed(2)}%`);
console.log(`\nCV by configuration at best n:`);
if (bestGroupStats) {
bestGroupStats.forEach((g) => {
if (g.conf) {
console.log(` ${JSON.stringify(g.conf)}: ${(g.stats.cv * 100).toFixed(2)}%`);
if (g.stats.cv > cvThreshold) {
console.log(` β οΈ This configuration is above the target threshold of ${cvThreshold * 100}%`);
}
}
});
}
}
console.log(`
Recommendation: You might want to try increasing --max-increases to
continue testing with larger n values, or adjust --cv-threshold to
accept the current best result, or investigate if specific configurations
are contributing to instability.`);
return bestN;
}
main().catch((err) => {
console.error('Error:', err);
process.exit(1);
});