-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark-bao.js
More file actions
271 lines (223 loc) · 8.98 KB
/
benchmark-bao.js
File metadata and controls
271 lines (223 loc) · 8.98 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
/**
* Bao Performance Benchmarks
*/
const bao = require('./bao.js');
// Generate test data
function generateInput(length) {
const input = new Uint8Array(length);
for (let i = 0; i < length; i++) {
input[i] = i % 251;
}
return input;
}
// Format bytes as human-readable
function formatBytes(bytes) {
if (bytes >= 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
if (bytes >= 1024) return (bytes / 1024).toFixed(2) + ' KB';
return bytes + ' B';
}
// Format throughput
function formatThroughput(bytesPerMs) {
const mbPerSec = (bytesPerMs * 1000) / (1024 * 1024);
return mbPerSec.toFixed(2) + ' MB/s';
}
// Benchmark helper
function benchmark(name, fn, iterations = 10) {
// Warmup
for (let i = 0; i < 3; i++) {
fn();
}
// Timed runs
const times = [];
for (let i = 0; i < iterations; i++) {
const start = performance.now();
fn();
const end = performance.now();
times.push(end - start);
}
const avg = times.reduce((a, b) => a + b, 0) / times.length;
const min = Math.min(...times);
const max = Math.max(...times);
return { avg, min, max };
}
console.log('Bao Performance Benchmarks');
console.log('==========================\n');
// Test sizes
const sizes = [
{ name: '1 KB', bytes: 1024 },
{ name: '10 KB', bytes: 10 * 1024 },
{ name: '100 KB', bytes: 100 * 1024 },
{ name: '1 MB', bytes: 1024 * 1024 },
{ name: '10 MB', bytes: 10 * 1024 * 1024 },
];
// Pre-generate test data
const testData = {};
for (const size of sizes) {
testData[size.bytes] = generateInput(size.bytes);
}
// ============================
// Encoding Benchmarks
// ============================
console.log('--- Encoding Throughput (Combined Mode) ---\n');
console.log('Size | Time (ms) | Throughput');
console.log('-----------|-----------|------------');
const encodedResults = {};
for (const size of sizes) {
const data = testData[size.bytes];
let result;
const { avg } = benchmark(`encode_${size.name}`, () => {
result = bao.baoEncode(data);
}, size.bytes >= 1024 * 1024 ? 5 : 10);
encodedResults[size.bytes] = result;
const throughput = formatThroughput(size.bytes / avg);
console.log(`${size.name.padEnd(10)} | ${avg.toFixed(2).padStart(9)} | ${throughput}`);
}
console.log('\n--- Encoding Throughput (Outboard Mode) ---\n');
console.log('Size | Time (ms) | Throughput');
console.log('-----------|-----------|------------');
const outboardResults = {};
for (const size of sizes) {
const data = testData[size.bytes];
let result;
const { avg } = benchmark(`encode_outboard_${size.name}`, () => {
result = bao.baoEncode(data, true);
}, size.bytes >= 1024 * 1024 ? 5 : 10);
outboardResults[size.bytes] = result;
const throughput = formatThroughput(size.bytes / avg);
console.log(`${size.name.padEnd(10)} | ${avg.toFixed(2).padStart(9)} | ${throughput}`);
}
// ============================
// Decoding Benchmarks
// ============================
console.log('\n--- Decoding Throughput (Combined Mode) ---\n');
console.log('Size | Time (ms) | Throughput');
console.log('-----------|-----------|------------');
for (const size of sizes) {
const { encoded, hash } = encodedResults[size.bytes];
const { avg } = benchmark(`decode_${size.name}`, () => {
bao.baoDecode(encoded, hash);
}, size.bytes >= 1024 * 1024 ? 5 : 10);
const throughput = formatThroughput(size.bytes / avg);
console.log(`${size.name.padEnd(10)} | ${avg.toFixed(2).padStart(9)} | ${throughput}`);
}
console.log('\n--- Decoding Throughput (Outboard Mode) ---\n');
console.log('Size | Time (ms) | Throughput');
console.log('-----------|-----------|------------');
for (const size of sizes) {
const data = testData[size.bytes];
const { encoded, hash } = outboardResults[size.bytes];
const { avg } = benchmark(`decode_outboard_${size.name}`, () => {
bao.baoDecode(encoded, hash, data);
}, size.bytes >= 1024 * 1024 ? 5 : 10);
const throughput = formatThroughput(size.bytes / avg);
console.log(`${size.name.padEnd(10)} | ${avg.toFixed(2).padStart(9)} | ${throughput}`);
}
// ============================
// Slice Extraction Benchmarks
// ============================
console.log('\n--- Slice Extraction Performance ---\n');
// Test extracting 1KB from various file sizes
const sliceSize = 1024;
const sliceSizes = [
{ name: '100 KB', bytes: 100 * 1024 },
{ name: '1 MB', bytes: 1024 * 1024 },
{ name: '10 MB', bytes: 10 * 1024 * 1024 },
];
console.log('Extracting 1KB slice from middle of file:\n');
console.log('File Size | Slice Size | Reduction | Extract (ms) | Decode (ms)');
console.log('-----------|------------|-----------|--------------|------------');
for (const size of sliceSizes) {
const { encoded, hash } = encodedResults[size.bytes];
const sliceStart = Math.floor(size.bytes / 2);
let slice;
const extractTime = benchmark(`slice_extract_${size.name}`, () => {
slice = bao.baoSlice(encoded, sliceStart, sliceSize);
}, 10);
const decodeTime = benchmark(`slice_decode_${size.name}`, () => {
bao.baoDecodeSlice(slice, hash, sliceStart, sliceSize);
}, 10);
const reduction = ((1 - slice.length / encoded.length) * 100).toFixed(1);
console.log(
`${size.name.padEnd(10)} | ${formatBytes(slice.length).padEnd(10)} | ${(reduction + '%').padStart(9)} | ${extractTime.avg.toFixed(3).padStart(12)} | ${decodeTime.avg.toFixed(3).padStart(10)}`
);
}
// ============================
// Streaming vs Batch Comparison
// ============================
console.log('\n--- Streaming vs Batch Encoding ---\n');
console.log('Size | Batch (ms) | Stream (ms) | Ratio');
console.log('-----------|------------|-------------|-------');
for (const size of sizes.slice(0, 4)) { // Skip 10MB for streaming (too slow)
const data = testData[size.bytes];
const batchTime = benchmark(`batch_encode_${size.name}`, () => {
bao.baoEncode(data);
}, 10);
// Streaming encode with 1KB chunks
const streamTime = benchmark(`stream_encode_${size.name}`, () => {
const encoder = new bao.BaoEncoder();
for (let i = 0; i < data.length; i += 1024) {
encoder.write(data.subarray(i, Math.min(i + 1024, data.length)));
}
encoder.finalize();
}, 10);
const ratio = (streamTime.avg / batchTime.avg).toFixed(2);
console.log(
`${size.name.padEnd(10)} | ${batchTime.avg.toFixed(2).padStart(10)} | ${streamTime.avg.toFixed(2).padStart(11)} | ${ratio}x`
);
}
console.log('\n--- Streaming vs Batch Decoding ---\n');
console.log('Size | Batch (ms) | Stream (ms) | Ratio');
console.log('-----------|------------|-------------|-------');
for (const size of sizes.slice(0, 4)) {
const { encoded, hash } = encodedResults[size.bytes];
const content = encoded.subarray(bao.HEADER_SIZE);
const batchTime = benchmark(`batch_decode_${size.name}`, () => {
bao.baoDecode(encoded, hash);
}, 10);
// Streaming decode with 1KB chunks
const streamTime = benchmark(`stream_decode_${size.name}`, () => {
const decoder = new bao.BaoDecoder(hash, size.bytes);
for (let i = 0; i < content.length; i += 1024) {
decoder.write(content.subarray(i, Math.min(i + 1024, content.length)));
}
decoder.read();
}, 10);
const ratio = (streamTime.avg / batchTime.avg).toFixed(2);
console.log(
`${size.name.padEnd(10)} | ${batchTime.avg.toFixed(2).padStart(10)} | ${streamTime.avg.toFixed(2).padStart(11)} | ${ratio}x`
);
}
// ============================
// Encoding Size Overhead
// ============================
console.log('\n--- Encoding Size Overhead ---\n');
console.log('Input Size | Combined | Outboard | Combined OH | Outboard OH');
console.log('-----------|------------|-----------|-------------|------------');
for (const size of sizes) {
const combined = encodedResults[size.bytes].encoded.length;
const outboard = outboardResults[size.bytes].encoded.length;
const combinedOH = ((combined - size.bytes) / size.bytes * 100).toFixed(2);
const outboardOH = (outboard / size.bytes * 100).toFixed(2);
console.log(
`${size.name.padEnd(10)} | ${formatBytes(combined).padEnd(10)} | ${formatBytes(outboard).padEnd(9)} | ${(combinedOH + '%').padStart(11)} | ${(outboardOH + '%').padStart(10)}`
);
}
// ============================
// Summary
// ============================
console.log('\n==========================');
console.log('Benchmark complete.');
// Quick summary of peak performance
const peakEncodeSize = sizes[sizes.length - 1];
const peakEncodeTime = benchmark('peak_encode', () => {
bao.baoEncode(testData[peakEncodeSize.bytes]);
}, 3);
const peakEncodeThroughput = formatThroughput(peakEncodeSize.bytes / peakEncodeTime.avg);
console.log(`\nPeak encoding throughput (${peakEncodeSize.name}): ${peakEncodeThroughput}`);
const peakDecodeSize = sizes[sizes.length - 1];
const { encoded: peakEncoded, hash: peakHash } = encodedResults[peakDecodeSize.bytes];
const peakDecodeTime = benchmark('peak_decode', () => {
bao.baoDecode(peakEncoded, peakHash);
}, 3);
const peakDecodeThroughput = formatThroughput(peakDecodeSize.bytes / peakDecodeTime.avg);
console.log(`Peak decoding throughput (${peakDecodeSize.name}): ${peakDecodeThroughput}`);