-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze-speed.js
337 lines (298 loc) · 10.2 KB
/
analyze-speed.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import fs from 'fs';
import path from 'path';
// Helper function to calculate mean
function calculateMean(numbers) {
const validNumbers = numbers.filter((n) => n !== null);
if (validNumbers.length === 0) return null;
return validNumbers.reduce((acc, curr) => acc + curr, 0) / validNumbers.length;
}
// Helper function to calculate standard deviation
function calculateStdDev(numbers) {
const validNumbers = numbers.filter((n) => n !== null);
if (validNumbers.length === 0) return null;
const mean = calculateMean(validNumbers);
const squareDiffs = validNumbers.map((value) => Math.pow(value - mean, 2));
const avgSquareDiff = calculateMean(squareDiffs);
return Math.sqrt(avgSquareDiff);
}
// Helper function to calculate median
function calculateMedian(numbers) {
const validNumbers = numbers.filter((n) => n !== null).sort((a, b) => a - b);
if (validNumbers.length === 0) return null;
const mid = Math.floor(validNumbers.length / 2);
if (validNumbers.length % 2 === 0) {
return (validNumbers[mid - 1] + validNumbers[mid]) / 2;
}
return validNumbers[mid];
}
// Helper function to calculate error count
function calculateErrorCount(speeds) {
return speeds.filter((speed) => speed === null).length;
}
// Read all JSON files from outputs directory
function getLatestBenchmarkData(limit) {
const outputsDir = path.join('.', 'outputs');
const files = fs
.readdirSync(outputsDir)
.filter((file) => file.endsWith('.json'))
.map((file) => ({
name: file,
time: new Date(file.replace('.json', '')).getTime(),
}))
.sort((a, b) => b.time - a.time) // Sort by timestamp descending
.slice(0, limit); // Take only the latest n files
return files.map((file) => {
const filePath = path.join(outputsDir, file.name);
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
return {
timestamp: data.timestamp,
results: data.results,
};
});
}
// Get benchmark data from JSON files
const benchmarkData = getLatestBenchmarkData(50);
// Get all speeds for each provider
const providerSpeeds = {
DeepSeek: [],
DeepInfra: [],
Fireworks: [],
Together: [],
Hyperbolic: [],
Azure: [],
Nebius: [],
Nvidia: [],
Kluster: [],
Novita: [],
};
benchmarkData.forEach((run) => {
Object.entries(run.results).forEach(([provider, speed]) => {
providerSpeeds[provider].push(speed);
});
});
// Helper function to calculate statistics for a provider's speeds
function calculateProviderStats(provider, speeds) {
if (speeds.length > 0) {
const errorCount = calculateErrorCount(speeds);
const validSpeeds = speeds.filter((s) => s !== null);
const mean = calculateMean(speeds);
const median = calculateMedian(speeds);
const min = validSpeeds.length > 0 ? Math.min(...validSpeeds) : null;
const max = validSpeeds.length > 0 ? Math.max(...validSpeeds) : null;
const stdDev = calculateStdDev(speeds);
return {
provider,
mean,
median,
min,
max,
stdDev,
runs: speeds.length,
errorCount,
};
}
return null;
}
// Helper function to format statistics line
function formatStatsLine({ provider, mean, median, min, max, stdDev, runs, errorCount }) {
const errorRate = runs > 0 ? ((errorCount / runs) * 100).toFixed(2) + '%' : '?';
const stats = [];
if (median !== null && mean !== null) {
stats.push(
`Median/Mean: ${median.toFixed(2).padStart(5)}/${mean.toFixed(2).padStart(5)}`
);
}
if (min !== null && max !== null && stdDev !== null) {
stats.push(
`Range: ${min.toFixed(2).padStart(5)}-${max.toFixed(2).padStart(5)} ±${stdDev
.toFixed(2)
.padStart(5)}`
);
}
stats.push(`Error rate: ${errorRate.padStart(6)}`);
stats.push(`Success/Error: ${runs - errorCount}/${errorCount}`);
return `${provider.padEnd(10)}: ${stats.join(', ')}`;
}
// Helper function to process daily statistics
function processDailyStats(providers) {
const totalRuns = Object.values(providers).reduce(
(acc, speeds) => acc + speeds.length,
0
);
// Skip dates with less than 5 total runs
if (totalRuns < 5) {
return null;
}
return Object.entries(providers)
.map(([provider, speeds]) => calculateProviderStats(provider, speeds))
.filter(Boolean)
.sort((a, b) => {
// Sort by median speed, but handle null values
if (a.median === null && b.median === null) return 0;
if (a.median === null) return 1;
if (b.median === null) return -1;
return b.median - a.median;
});
}
// Calculate overall statistics
const providerStats = Object.entries(providerSpeeds)
.map(([provider, speeds]) => calculateProviderStats(provider, speeds))
.filter(Boolean)
.sort((a, b) => {
// Sort by median speed, but handle null values
if (a.median === null && b.median === null) return 0;
if (a.median === null) return 1;
if (b.median === null) return -1;
return b.median - a.median;
});
// Aggregate results by date
const resultsByDate = {};
benchmarkData.forEach((run) => {
const date = new Date(run.timestamp).toLocaleDateString('en-UK', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
if (!resultsByDate[date]) {
resultsByDate[date] = {
DeepSeek: [],
DeepInfra: [],
Fireworks: [],
Together: [],
Hyperbolic: [],
Azure: [],
Nebius: [],
Nvidia: [],
Kluster: [],
Novita: [],
};
}
Object.entries(run.results).forEach(([provider, speed]) => {
resultsByDate[date][provider].push(speed);
});
});
// Process daily statistics
const dailyStats = {};
Object.entries(resultsByDate)
.sort((a, b) => {
// Parse UK formatted dates (DD/MM/YYYY)
const [aDay, aMonth, aYear] = a[0].split('/');
const [bDay, bMonth, bYear] = b[0].split('/');
const aDate = new Date(aYear, aMonth - 1, aDay);
const bDate = new Date(bYear, bMonth - 1, bDay);
return bDate - aDate;
})
.forEach(([date, providers]) => {
const stats = processDailyStats(providers);
if (stats) {
dailyStats[date] = stats;
}
});
// Function to display and update statistics
function displayAndUpdateStats(overallStats, dailyStats) {
// Generate the statistics content
let content = '';
// Overall statistics section
const overallSection =
'=== Overall Speed Statistics (tokens/second) ===\n' +
`Using latest ${benchmarkData.length} benchmark runs\n\n` +
overallStats.map(formatStatsLine).join('\n');
// Daily statistics section
const dailySection =
'\n\n=== Daily Statistics ===\n' +
Object.entries(dailyStats)
.sort((a, b) => {
// Parse UK formatted dates (DD/MM/YYYY)
const [aDay, aMonth, aYear] = a[0].split('/');
const [bDay, bMonth, bYear] = b[0].split('/');
const aDate = new Date(aYear, aMonth - 1, aDay);
const bDate = new Date(bYear, bMonth - 1, bDay);
return bDate - aDate;
})
.map(([date, stats]) => {
if (!stats || stats.length === 0) return '';
return `\nDate: ${date}\n${stats.map(formatStatsLine).join('\n')}`;
})
.filter((section) => section !== '')
.join('\n');
// Combine sections
content = overallSection + dailySection;
// Log to console
console.log(content);
// Update README
const readmePath = path.join('.', 'README.md');
let readmeContent = fs.readFileSync(readmePath, 'utf8');
const readmeSection =
'## Speed statistics\n\n' +
'Statistics of the speed of the API automatically generated by running `analyze-speed.js`.\n\n' +
'```\n' +
content +
'\n```\n';
// Replace the existing Speed statistics section
// Match from '## Speed statistics' until the closing code block marker
const regex = /## Speed statistics[\s\S]*?```[\s\S]*?```\n/;
readmeContent = readmeContent.replace(regex, readmeSection);
// Write the updated content back to README.md
fs.writeFileSync(readmePath, readmeContent, 'utf8');
}
// Function to generate time series data for visualization
function generateTimeSeriesData(dailyStats) {
const medianData = Object.entries(dailyStats)
.sort((a, b) => {
// Parse UK formatted dates (DD/MM/YYYY)
const [aDay, aMonth, aYear] = a[0].split('/');
const [bDay, bMonth, bYear] = b[0].split('/');
const aDate = new Date(aYear, aMonth - 1, aDay);
const bDate = new Date(bYear, bMonth - 1, bDay);
return aDate - bDate; // Note: ascending order for time series
})
.map(([date, stats]) => {
const dataPoint = {
date,
providers: {},
};
stats.forEach(({ provider, median }) => {
dataPoint.providers[provider] =
median !== null ? Number(median.toFixed(2)) : null;
});
return dataPoint;
});
const successRateData = Object.entries(dailyStats)
.sort((a, b) => {
// Parse UK formatted dates (DD/MM/YYYY)
const [aDay, aMonth, aYear] = a[0].split('/');
const [bDay, bMonth, bYear] = b[0].split('/');
const aDate = new Date(aYear, aMonth - 1, aDay);
const bDate = new Date(bYear, bMonth - 1, bDay);
return aDate - bDate; // Note: ascending order for time series
})
.map(([date, stats]) => {
const dataPoint = {
date,
providers: {},
};
stats.forEach(({ provider, runs, errorCount }) => {
const successCount = runs - errorCount;
dataPoint.providers[provider] =
runs > 0 ? Number(((successCount / runs) * 100).toFixed(2)) : null;
});
return dataPoint;
});
// Create summary directory if it doesn't exist
const summaryDir = path.join('.', 'summary');
if (!fs.existsSync(summaryDir)) {
fs.mkdirSync(summaryDir);
}
// Write median data to JSON file
const medianPath = path.join(summaryDir, 'median_speeds.json');
fs.writeFileSync(medianPath, JSON.stringify(medianData, null, 2), 'utf8');
console.log('\nMedian speeds data has been written to:', medianPath);
// Write success rate data to JSON file
const successPath = path.join(summaryDir, 'success_rates.json');
fs.writeFileSync(successPath, JSON.stringify(successRateData, null, 2), 'utf8');
console.log('Success rates data has been written to:', successPath);
}
// Display statistics and update README
displayAndUpdateStats(providerStats, dailyStats);
// Generate time series data
generateTimeSeriesData(dailyStats);