-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbench.mjs
More file actions
executable file
·342 lines (291 loc) · 10 KB
/
bench.mjs
File metadata and controls
executable file
·342 lines (291 loc) · 10 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
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
338
339
340
341
342
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import { existsSync, readdirSync, statSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
// Parse command line arguments
const { values } = parseArgs({
options: {
app: {
type: 'string',
default: 'apps/10000',
},
json: {
type: 'string',
},
help: {
type: 'boolean',
short: 'h',
},
},
});
// Show help
if (values.help) {
console.log(`Usage: node bench.mjs [OPTIONS]
Options:
--app APP App directory to benchmark (default: apps/10000)
Available: apps/1000, apps/3000, apps/5000, apps/10000, apps/rome, apps/three10x
--json FILE Export results to JSON file
-h, --help Show this help message
Example:
node bench.mjs --app apps/5000
node bench.mjs --json results.json`);
process.exit(0);
}
const app = values.app;
const jsonOutput = values.json;
// Determine hyperfine binary name based on platform
const hyperfineBin = process.platform === 'win32' ? 'hyperfine.exe' : 'hyperfine';
// Check if hyperfine is installed
try {
execSync(`${hyperfineBin} --version`, { stdio: 'ignore' });
} catch (error) {
console.error('Error: hyperfine is not installed.');
console.error('Please install it from: https://github.com/sharkdp/hyperfine');
console.error(' macOS: brew install hyperfine');
console.error(' Linux: apt install hyperfine / pacman -S hyperfine');
console.error(' Windows: choco install hyperfine');
process.exit(1);
}
// Check if app directory exists
if (!existsSync(app)) {
console.error(`Error: App directory '${app}' does not exist`);
process.exit(1);
}
// Helper function to escape shell arguments cross-platform
function escapeShellArg(arg) {
if (process.platform === 'win32') {
// Windows: wrap in double quotes and escape inner double quotes
return `"${arg.replace(/"/g, '\\"')}"`;
} else {
// Unix: wrap in single quotes and escape single quotes
return `'${arg.replace(/'/g, "'\\''")}'`;
}
}
// Run benchmark for all tools
const tools = ['vite', 'rsbuild', 'rspack', 'rolldown', 'esbuild', 'bun'];
const toolDisplayNames = {
'vite': 'vite',
'rsbuild': 'rsbuild',
'rspack': 'rspack',
'rolldown': 'rolldown',
'esbuild': 'esbuild',
'bun': 'bun'
};
const tempJsonFile = '.bench-temp.json';
// Build hyperfine command with proper quoting
let cmd = `${hyperfineBin} --export-json ${escapeShellArg(tempJsonFile)}`;
for (const tool of tools) {
const displayName = toolDisplayNames[tool] || tool;
cmd += ` -n ${escapeShellArg(displayName)} ${escapeShellArg(`node --run build:${tool}`)}`;
}
console.log(`Running benchmarks for: ${tools.join(', ')}`);
console.log(`App: ${app}`);
console.log('');
execSync(cmd, { stdio: 'inherit', shell: true, cwd: app });
// Parse benchmark results
const benchmarkJson = JSON.parse(readFileSync(join(app, tempJsonFile), 'utf-8'));
// Get file sizes
const fileSizes = getFileSizes(app, tools);
// Get tool versions from package.json
const toolVersions = getToolVersions();
// Create reverse mapping from display names to internal tool names
const displayNameToTool = Object.entries(toolDisplayNames).reduce((acc, [tool, displayName]) => {
acc[displayName] = tool;
return acc;
}, {});
// Combine results
const results = benchmarkJson.results.map((result) => {
// Extract tool name from the command or use the command itself (which might be the display name)
let toolName = result.command.match(/node --run build:(\w+)/)?.[1];
// If we couldn't extract from command, result.command might be the display name
// Try to map it back to the internal tool name
if (!toolName) {
toolName = displayNameToTool[result.command] || result.command;
}
const sizeData = fileSizes.find(s => s.tool === toolName);
// Get version using the internal tool name (before display name mapping)
const version = toolVersions[toolName] || 'unknown';
// Get display name for the tool
const displayName = toolDisplayNames[toolName] || toolName;
return {
tool: displayName,
version: version,
mean: result.mean,
stddev: result.stddev,
jsSize: sizeData?.jsSize || 0,
cssSize: sizeData?.cssSize || 0,
mapSize: sizeData?.mapSize || 0,
totalSize: (sizeData?.jsSize || 0) + (sizeData?.cssSize || 0),
};
});
// Sort by build time
results.sort((a, b) => a.mean - b.mean);
// Display combined results
console.log('');
console.log('Benchmark Results (sorted by time):');
displayResults(results);
// Clean up temp file
unlinkSync(join(app, tempJsonFile));
// Save to user's JSON file if specified
if (jsonOutput) {
const output = {
results: results.map(r => ({
tool: r.tool,
version: r.version,
time_ms: (r.mean * 1000).toFixed(2),
stddev_ms: (r.stddev * 1000).toFixed(2),
js_size: formatSize(r.jsSize),
css_size: formatSize(r.cssSize),
sourcemaps_size: formatSize(r.mapSize),
})),
};
writeFileSync(jsonOutput, JSON.stringify(output, null, 2));
}
function getToolVersions() {
// Map tool names to their version commands
const toolCommandMap = {
'vite': 'pnpm vite --version',
'rsbuild': 'pnpm rsbuild --version',
'rspack': 'pnpm rspack --version',
'rolldown': 'pnpm rolldown --version',
'esbuild': 'pnpm esbuild --version',
'bun': 'bun --version',
};
const versions = {};
for (const [tool, command] of Object.entries(toolCommandMap)) {
try {
const output = execSync(command, {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
shell: true
}).trim();
// Extract version number from various formats:
// - "0.27.0" (esbuild, bun)
// - "vite/7.2.2 darwin-arm64 node-v24.11.0" (vite)
// - "rspack/1.6.1 darwin-arm64 node-v24.11.0" (rspack)
// - "Rsbuild v1.6.3\n\nrsbuild/1.6.3 ..." (rsbuild)
// - "rolldown v1.0.0-beta.48" (rolldown)
const match = output.match(/(?:^|[/\s]v?)(\d+\.\d+\.\d+(?:-[^\s]+)?)/);
versions[tool] = match ? match[1] : 'unknown';
} catch (error) {
versions[tool] = 'unknown';
}
}
return versions;
}
function walkDirectory(dir) {
const files = [];
const entries = readdirSync(dir);
for (const entry of entries) {
const fullPath = join(dir, entry);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
files.push(...walkDirectory(fullPath));
} else if (stats.isFile()) {
files.push(fullPath);
}
}
return files;
}
function getFileSizes(appDir, tools) {
const results = [];
for (const tool of tools) {
const distDir = join(appDir, `dist-${tool}`);
if (!existsSync(distDir)) {
results.push({ tool, jsSize: 0, cssSize: 0, mapSize: 0 });
continue;
}
let jsSize = 0;
let cssSize = 0;
let mapSize = 0;
try {
const files = walkDirectory(distDir);
for (const filePath of files) {
const stats = statSync(filePath);
if (filePath.endsWith('.js.map') || filePath.endsWith('.css.map')) {
mapSize += stats.size;
} else if (filePath.endsWith('.js')) {
jsSize += stats.size;
} else if (filePath.endsWith('.css')) {
cssSize += stats.size;
}
}
results.push({ tool, jsSize, cssSize, mapSize });
} catch (error) {
results.push({ tool, jsSize: 0, cssSize: 0, mapSize: 0 });
}
}
return results;
}
function displayResults(results) {
// Find the minimum mean time for comparison
const minMean = Math.min(...results.map(r => r.mean));
// Calculate column widths
const data = results.map(result => {
const meanMs = (result.mean * 1000).toFixed(2);
const stddevMs = (result.stddev * 1000).toFixed(2);
// Pad to format: "XXXX.XX ± XXX.XX ms"
const meanPadded = meanMs.padStart(7); // Max: "9999.99"
const stddevPadded = stddevMs.padStart(6); // Max: "999.99"
const comparison = (result.mean / minMean).toFixed(1) + 'x';
return {
tool: result.tool,
version: result.version,
time: `${meanPadded} ± ${stddevPadded} ms`,
comparison: comparison,
js: result.jsSize > 0 ? formatSize(result.jsSize) : 'not found',
css: result.cssSize > 0 ? formatSize(result.cssSize) : 'not found',
maps: result.mapSize > 0 ? formatSize(result.mapSize) : 'not found',
};
});
const colWidths = {
tool: Math.max(4, ...data.map(d => d.tool.length)),
version: Math.max(7, ...data.map(d => d.version.length)),
time: 25,
comparison: Math.max(10, ...data.map(d => d.comparison.length)),
js: Math.max(2, ...data.map(d => d.js.length)),
css: Math.max(3, ...data.map(d => d.css.length)),
maps: Math.max(10, ...data.map(d => d.maps.length)),
};
// Print markdown table header
console.log(
'| ' + 'Tool'.padEnd(colWidths.tool) +
' | ' + 'Version'.padEnd(colWidths.version) +
' | ' + 'Time (mean ± σ)'.padEnd(colWidths.time) +
' | ' + 'Comparison'.padEnd(colWidths.comparison) +
' | ' + 'JS'.padEnd(colWidths.js) +
' | ' + 'CSS'.padEnd(colWidths.css) +
' | ' + 'Sourcemaps'.padEnd(colWidths.maps) +
' |'
);
console.log(
'| ' + '-'.repeat(colWidths.tool) +
' | ' + '-'.repeat(colWidths.version) +
' | ' + '-'.repeat(colWidths.time - 1) + ':' +
' | ' + '-'.repeat(colWidths.comparison) +
' | ' + '-'.repeat(colWidths.js) +
' | ' + '-'.repeat(colWidths.css) +
' | ' + '-'.repeat(colWidths.maps) +
' |'
);
// Print rows
for (const row of data) {
console.log(
'| ' + row.tool.padEnd(colWidths.tool) +
' | ' + row.version.padEnd(colWidths.version) +
' | ' + row.time.padStart(colWidths.time) +
' | ' + row.comparison.padEnd(colWidths.comparison) +
' | ' + row.js.padEnd(colWidths.js) +
' | ' + row.css.padEnd(colWidths.css) +
' | ' + row.maps.padEnd(colWidths.maps) +
' |'
);
}
}
function formatSize(bytes) {
if (bytes === 0) return '0 B';
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}