Repository navigation
Expand file tree
/
Copy pathbenchmark.ts
More file actions
532 lines (469 loc) · 14 KB
/
Copy pathbenchmark.ts
File metadata and controls
532 lines (469 loc) · 14 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/**
* Performance Benchmark: Native C++ File I/O vs Standard TypeScript
*
* This benchmark compares:
* 1. Native C++ addon (mmap + SIMD)
* 2. Standard fs.promises (Node.js built-in)
* 3. Standard fs.readFileSync (blocking)
*/
import * as fs from "fs";
import * as path from "path";
import * as crypto from "crypto";
import {
fastReadFile,
simdChecksum,
getFileStats,
isNativeCppLoaded,
} from "./index";
// ANSI color codes for terminal output
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
red: "\x1b[31m",
};
interface BenchmarkResult {
operation: string;
method: string;
iterations: number;
totalTimeMs: number;
avgTimeMs: number;
throughputMBps?: number;
}
/**
* Create test files of various sizes
*/
async function createTestFiles(): Promise<void> {
const testDir = path.join(__dirname, "benchmark_data");
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
const sizes = [
{ name: "small", size: 1024 * 100 }, // 100 KB
{ name: "medium", size: 1024 * 1024 * 10 }, // 10 MB
{ name: "large", size: 1024 * 1024 * 100 }, // 100 MB
{ name: "xlarge", size: 1024 * 1024 * 1024 }, // 1 GB
];
console.log(`${colors.cyan}Creating test files...${colors.reset}`);
for (const { name, size } of sizes) {
const filePath = path.join(testDir, `test_${name}.bin`);
if (!fs.existsSync(filePath)) {
// Format file size for display
const sizeMB = size / 1024 / 1024;
const sizeDisplay =
sizeMB >= 1024
? `${(sizeMB / 1024).toFixed(2)} GB`
: `${sizeMB.toFixed(2)} MB`;
// For very large files (>100MB), write in chunks to avoid memory issues
if (size > 100 * 1024 * 1024) {
console.log(
` Creating ${name}: ${sizeDisplay} (this may take a while)...`
);
const chunkSize = 10 * 1024 * 1024; // 10 MB chunks
const fd = fs.openSync(filePath, "w");
let written = 0;
while (written < size) {
const chunk = crypto.randomBytes(Math.min(chunkSize, size - written));
fs.writeSync(fd, chunk, 0, chunk.length, written);
written += chunk.length;
}
fs.closeSync(fd);
console.log(` Created ${name}: ${sizeDisplay}`);
} else {
const buffer = crypto.randomBytes(size);
fs.writeFileSync(filePath, buffer);
console.log(` Created ${name}: ${sizeDisplay}`);
}
}
}
console.log();
}
/**
* Benchmark: File reading operations
*/
async function benchmarkFileRead(
filePath: string,
fileSize: number
): Promise<BenchmarkResult[]> {
const results: BenchmarkResult[] = [];
// Adjust iterations based on file size
let iterations: number;
if (fileSize > 500 * 1024 * 1024) {
// > 500 MB
iterations = 5;
} else if (fileSize > 50 * 1024 * 1024) {
// > 50 MB
iterations = 20;
} else {
iterations = 100;
}
// Method 1: Native C++ addon
if (isNativeCppLoaded()) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
await fastReadFile(filePath, 0, fileSize);
}
const elapsed = performance.now() - start;
results.push({
operation: "Read File",
method: "Native C++ (mmap)",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 2: fs.promises.readFile
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
await fs.promises.readFile(filePath);
}
const elapsed = performance.now() - start;
results.push({
operation: "Read File",
method: "fs.promises.readFile",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 3: fs.readFileSync
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fs.readFileSync(filePath);
}
const elapsed = performance.now() - start;
results.push({
operation: "Read File",
method: "fs.readFileSync",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 4: fs.promises with file descriptor (optimized)
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
const fd = await fs.promises.open(filePath, "r");
try {
const buffer = Buffer.allocUnsafe(fileSize);
await fd.read(buffer, 0, fileSize, 0);
} finally {
await fd.close();
}
}
const elapsed = performance.now() - start;
results.push({
operation: "Read File",
method: "fs.promises (fd)",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
return results;
}
/**
* Benchmark: Checksum calculation
*/
async function benchmarkChecksum(
filePath: string,
fileSize: number
): Promise<BenchmarkResult[]> {
const results: BenchmarkResult[] = [];
// Adjust iterations based on file size
let iterations: number;
if (fileSize > 500 * 1024 * 1024) {
// > 500 MB
iterations = 3;
} else if (fileSize > 50 * 1024 * 1024) {
// > 50 MB
iterations = 10;
} else {
iterations = 50;
}
// Read file once for all checksum tests
const buffer = fs.readFileSync(filePath);
// Method 1: Native SIMD checksum
if (isNativeCppLoaded()) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
simdChecksum(buffer);
}
const elapsed = performance.now() - start;
results.push({
operation: "Checksum",
method: "Native SIMD",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 2: crypto.createHash (SHA-256)
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
crypto.createHash("sha256").update(buffer).digest("hex");
}
const elapsed = performance.now() - start;
results.push({
operation: "Checksum",
method: "crypto SHA-256",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 3: crypto.createHash (MD5)
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
crypto.createHash("md5").update(buffer).digest("hex");
}
const elapsed = performance.now() - start;
results.push({
operation: "Checksum",
method: "crypto MD5",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
// Method 4: Simple JavaScript hash
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
let hash = 0;
for (let j = 0; j < buffer.length; j++) {
hash = (hash * 31 + buffer[j]) | 0;
}
hash.toString(16);
}
const elapsed = performance.now() - start;
results.push({
operation: "Checksum",
method: "JS Simple Hash",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
throughputMBps: fileSize / 1024 / 1024 / (elapsed / 1000 / iterations),
});
}
return results;
}
/**
* Benchmark: File stats
*/
async function benchmarkStats(filePath: string): Promise<BenchmarkResult[]> {
const results: BenchmarkResult[] = [];
const iterations = 10000;
// Method 1: Native C++ stats
if (isNativeCppLoaded()) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
await getFileStats(filePath);
}
const elapsed = performance.now() - start;
results.push({
operation: "File Stats",
method: "Native C++",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
});
}
// Method 2: fs.promises.stat
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
await fs.promises.stat(filePath);
}
const elapsed = performance.now() - start;
results.push({
operation: "File Stats",
method: "fs.promises.stat",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
});
}
// Method 3: fs.statSync
{
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fs.statSync(filePath);
}
const elapsed = performance.now() - start;
results.push({
operation: "File Stats",
method: "fs.statSync",
iterations,
totalTimeMs: elapsed,
avgTimeMs: elapsed / iterations,
});
}
return results;
}
/**
* Display results in a formatted table
*/
function displayResults(results: BenchmarkResult[], fileSize?: number): void {
// Use the slowest method as baseline for speedup calculation
const baselineAvg = Math.max(...results.map((r) => r.avgTimeMs));
console.log(
` ${"Method".padEnd(25)} | ${"Avg Time".padEnd(
12
)} | ${"Total Time".padEnd(12)} | ${"Speedup".padEnd(
10
)} | ${"Throughput".padEnd(12)}`
);
console.log(
` ${"-".repeat(25)} | ${"-".repeat(12)} | ${"-".repeat(12)} | ${"-".repeat(
10
)} | ${"-".repeat(12)}`
);
results.forEach((result, index) => {
const speedup = baselineAvg / result.avgTimeMs;
const speedupStr = `${speedup.toFixed(2)}x`;
const avgTimeStr = `${result.avgTimeMs.toFixed(3)} ms`;
const totalTimeStr = `${result.totalTimeMs.toFixed(0)} ms`;
const throughputStr = result.throughputMBps
? `${result.throughputMBps.toFixed(2)} MB/s`
: "N/A";
// Highlight fastest method
const isFastest = index === 0 && isNativeCppLoaded();
const methodStr = result.method.padEnd(25);
const color = isFastest ? colors.green : colors.reset;
console.log(
` ${color}${methodStr}${colors.reset} | ${avgTimeStr.padEnd(
12
)} | ${totalTimeStr.padEnd(12)} | ${speedupStr.padEnd(
10
)} | ${throughputStr.padEnd(12)}`
);
});
console.log();
}
/**
* Calculate and display speedup summary
*/
function displaySpeedupSummary(allResults: BenchmarkResult[]): void {
const nativeResults = allResults.filter((r) => r.method.includes("Native"));
const standardResults = allResults.filter(
(r) =>
r.method.includes("fs.promises.readFile") ||
r.method.includes("crypto SHA-256") ||
r.method.includes("fs.promises.stat")
);
if (nativeResults.length === 0 || standardResults.length === 0) {
return;
}
const avgNative =
nativeResults.reduce((sum, r) => sum + r.avgTimeMs, 0) /
nativeResults.length;
const avgStandard =
standardResults.reduce((sum, r) => sum + r.avgTimeMs, 0) /
standardResults.length;
const overallSpeedup = avgStandard / avgNative;
console.log(`${colors.bright}${colors.cyan}OVERALL SPEEDUP${colors.reset}`);
console.log(
`${colors.green}Native C++ is ${overallSpeedup.toFixed(
2
)}x faster on average${colors.reset}`
);
console.log();
}
/**
* Main benchmark runner
*/
async function runBenchmarks(): Promise<void> {
console.log(
`${colors.bright}${colors.blue}Native C++ File I/O Performance Benchmark${colors.reset}\n`
);
if (!isNativeCppLoaded()) {
console.log(
`${colors.yellow}WARNING: Native C++ addon not loaded!${colors.reset}`
);
console.log(` Build it with: npm run build:native\n`);
} else {
console.log(`${colors.green}Native C++ addon loaded${colors.reset}\n`);
}
await createTestFiles();
const testDir = path.join(__dirname, "benchmark_data");
const testFiles = [
{
name: "small (100 KB)",
path: path.join(testDir, "test_small.bin"),
size: 1024 * 100,
},
{
name: "medium (10 MB)",
path: path.join(testDir, "test_medium.bin"),
size: 1024 * 1024 * 10,
},
{
name: "large (100 MB)",
path: path.join(testDir, "test_large.bin"),
size: 1024 * 1024 * 100,
},
{
name: "xlarge (1 GB)",
path: path.join(testDir, "test_xlarge.bin"),
size: 1024 * 1024 * 1024,
},
];
const allResults: BenchmarkResult[] = [];
// Run benchmarks for each file size
for (const testFile of testFiles) {
console.log(
`${colors.bright}${colors.yellow}Testing: ${testFile.name}${colors.reset}\n`
);
// File read benchmark
console.log(`${colors.cyan}1. File Read Performance${colors.reset}`);
const readResults = await benchmarkFileRead(testFile.path, testFile.size);
displayResults(readResults, testFile.size);
allResults.push(...readResults);
// Checksum benchmark
console.log(
`${colors.cyan}2. Checksum Calculation Performance${colors.reset}`
);
const checksumResults = await benchmarkChecksum(
testFile.path,
testFile.size
);
displayResults(checksumResults, testFile.size);
allResults.push(...checksumResults);
// Stats benchmark (only for first file to avoid repetition)
if (testFile.name.includes("small")) {
console.log(`${colors.cyan}3. File Stats Performance${colors.reset}`);
const statsResults = await benchmarkStats(testFile.path);
displayResults(statsResults);
allResults.push(...statsResults);
}
console.log(`${"-".repeat(80)}\n`);
}
// Display overall summary
displaySpeedupSummary(allResults);
console.log(
`${colors.bright}${colors.green}Benchmark completed!${colors.reset}`
);
console.log(`\n${colors.cyan}Key Takeaways:${colors.reset}`);
console.log(` • Native C++ uses mmap for zero-copy file access`);
console.log(` • SIMD instructions accelerate checksum calculations`);
console.log(` • Direct system calls reduce overhead vs Node.js wrappers`);
console.log(` • Performance gains increase with file size\n`);
}
// Run benchmarks
runBenchmarks().catch(console.error);