-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-wasm-zerocopy.js
More file actions
264 lines (230 loc) · 7.91 KB
/
Copy pathtest-wasm-zerocopy.js
File metadata and controls
264 lines (230 loc) · 7.91 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
/**
* Test and benchmark zero-copy WASM operations.
*/
'use strict';
const wasmZC = require('./bao-wasm-zerocopy.js');
const baoJs = require('./bao.js');
function toHex(arr) {
return Array.from(arr).map(b => b.toString(16).padStart(2, '0')).join('');
}
function formatThroughput(bytes, ms) {
if (ms === 0) return 'Inf MB/s';
return ((bytes / (1024 * 1024)) / (ms / 1000)).toFixed(2) + ' MB/s';
}
async function runTests() {
console.log('Initializing WASM...');
const ok = await wasmZC.initWasm();
if (!ok) {
console.error('WASM init failed');
process.exit(1);
}
console.log('WASM available:', wasmZC.isWasmEnabled());
console.log('');
let passed = 0;
let failed = 0;
// Test 1: Empty chunk CV
console.log('--- Test 1: Empty chunk CV ---');
const jsEmpty = baoJs.chunkCV(new Uint8Array(0), 0, true);
const wasmEmpty = wasmZC.chunkCV(new Uint8Array(0), 0, true);
if (toHex(jsEmpty) === toHex(wasmEmpty)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(jsEmpty));
console.log(' WASM:', toHex(wasmEmpty));
failed++;
}
// Test 2: Single byte
console.log('--- Test 2: Single byte chunk CV ---');
const js1 = baoJs.chunkCV(new Uint8Array([42]), 0, false);
const wasm1 = wasmZC.chunkCV(new Uint8Array([42]), 0, false);
if (toHex(js1) === toHex(wasm1)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 3: Full chunk
console.log('--- Test 3: Full chunk CV ---');
const fullChunk = new Uint8Array(1024);
for (let i = 0; i < 1024; i++) fullChunk[i] = (i * 13) & 0xff;
const jsFull = baoJs.chunkCV(fullChunk, 5, false);
const wasmFull = wasmZC.chunkCV(fullChunk, 5, false);
if (toHex(jsFull) === toHex(wasmFull)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 4: Parent CV
console.log('--- Test 4: Parent CV ---');
const left = new Uint8Array(32).fill(0x11);
const right = new Uint8Array(32).fill(0x22);
const jsParent = baoJs.parentCV(left, right, false);
const wasmParent = wasmZC.parentCV(left, right, false);
if (toHex(jsParent) === toHex(wasmParent)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 5: Batch chunk CVs
console.log('--- Test 5: Batch chunk CVs (4 chunks) ---');
const batchData = new Uint8Array(4 * 1024);
for (let i = 0; i < batchData.length; i++) batchData[i] = (i * 7) & 0xff;
const jsBatch = [];
for (let i = 0; i < 4; i++) {
jsBatch.push(baoJs.chunkCV(batchData.subarray(i * 1024, (i + 1) * 1024), i, false));
}
const wasmBatch = wasmZC.batchChunkCVs(batchData, 0, 4);
let batchOk = true;
for (let i = 0; i < 4; i++) {
if (toHex(jsBatch[i]) !== toHex(wasmBatch[i])) {
batchOk = false;
console.log(` Chunk ${i} mismatch`);
}
}
if (batchOk) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 6: Zero-copy API
console.log('--- Test 6: Zero-copy direct API ---');
const input = wasmZC.getInputBuffer();
input.set(fullChunk, 0);
const directCV = wasmZC.chunkCVDirect(1024, 5, false);
if (toHex(directCV) === toHex(jsFull)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 7: baoEncodeWasm vs baoEncode
console.log('--- Test 7: baoEncodeWasm (4KB) ---');
const data4k = new Uint8Array(4096);
for (let i = 0; i < data4k.length; i++) data4k[i] = (i * 11) & 0xff;
const jsEnc = baoJs.baoEncode(data4k, true);
const wasmEnc = wasmZC.baoEncodeWasm(data4k, true);
if (toHex(jsEnc.hash) === toHex(wasmEnc.hash) &&
toHex(jsEnc.encoded) === toHex(wasmEnc.encoded)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS hash: ', toHex(jsEnc.hash));
console.log(' WASM hash:', toHex(wasmEnc.hash));
failed++;
}
// Test 8: baoEncodeWasm combined mode
console.log('--- Test 8: baoEncodeWasm combined (4KB) ---');
const jsEncComb = baoJs.baoEncode(data4k, false);
const wasmEncComb = wasmZC.baoEncodeWasm(data4k, false);
if (toHex(jsEncComb.hash) === toHex(wasmEncComb.hash) &&
toHex(jsEncComb.encoded) === toHex(wasmEncComb.encoded)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
console.log('');
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) {
process.exit(1);
}
// Benchmarks
console.log('');
console.log('=== BENCHMARKS ===');
console.log('');
// Warmup
for (let i = 0; i < 100; i++) {
baoJs.chunkCV(fullChunk, i, false);
wasmZC.chunkCV(fullChunk, i, false);
}
// Benchmark 1: Single chunkCV
console.log('--- chunkCV (10,000 iterations) ---');
const iterations = 10000;
let start = Date.now();
for (let i = 0; i < iterations; i++) {
baoJs.chunkCV(fullChunk, i, false);
}
const jsTime = Date.now() - start;
start = Date.now();
for (let i = 0; i < iterations; i++) {
wasmZC.chunkCV(fullChunk, i, false);
}
const wasmTime = Date.now() - start;
// Zero-copy: write once, call many times with same data
input.set(fullChunk, 0);
start = Date.now();
for (let i = 0; i < iterations; i++) {
wasmZC.chunkCVDirect(1024, i, false);
}
const zcTime = Date.now() - start;
console.log(` JS: ${jsTime}ms (${formatThroughput(iterations * 1024, jsTime)})`);
console.log(` WASM: ${wasmTime}ms (${formatThroughput(iterations * 1024, wasmTime)}) - ${(jsTime/wasmTime).toFixed(2)}x`);
console.log(` Zero-copy: ${zcTime}ms (${formatThroughput(iterations * 1024, zcTime)}) - ${(jsTime/zcTime).toFixed(2)}x`);
// Benchmark 2: Batch vs sequential
console.log('');
console.log('--- Batch 64 chunks (500 iterations) ---');
const batchIterations = 500;
const batch64 = new Uint8Array(64 * 1024);
for (let i = 0; i < batch64.length; i++) batch64[i] = (i * 17) & 0xff;
start = Date.now();
for (let iter = 0; iter < batchIterations; iter++) {
for (let i = 0; i < 64; i++) {
baoJs.chunkCV(batch64.subarray(i * 1024, (i + 1) * 1024), i, false);
}
}
const jsSeqTime = Date.now() - start;
start = Date.now();
for (let iter = 0; iter < batchIterations; iter++) {
wasmZC.batchChunkCVs(batch64, 0, 64);
}
const wasmBatchTime = Date.now() - start;
// Zero-copy batch
start = Date.now();
for (let iter = 0; iter < batchIterations; iter++) {
input.set(batch64, 0);
wasmZC.batchChunkCVsDirect(64, 0);
}
const zcBatchTime = Date.now() - start;
console.log(` JS Sequential: ${jsSeqTime}ms (${formatThroughput(batchIterations * 64 * 1024, jsSeqTime)})`);
console.log(` WASM Batch: ${wasmBatchTime}ms (${formatThroughput(batchIterations * 64 * 1024, wasmBatchTime)}) - ${(jsSeqTime/wasmBatchTime).toFixed(2)}x`);
console.log(` ZC Batch: ${zcBatchTime}ms (${formatThroughput(batchIterations * 64 * 1024, zcBatchTime)}) - ${(jsSeqTime/zcBatchTime).toFixed(2)}x`);
// Benchmark 3: Full baoEncode
console.log('');
console.log('--- baoEncode outboard ---');
const sizes = [
{ name: '4 KB', bytes: 4 * 1024, iters: 5000 },
{ name: '64 KB', bytes: 64 * 1024, iters: 1000 },
{ name: '1 MB', bytes: 1024 * 1024, iters: 100 },
];
for (const { name, bytes, iters } of sizes) {
const data = new Uint8Array(bytes);
for (let i = 0; i < bytes; i++) data[i] = (i * 13) & 0xff;
start = Date.now();
for (let i = 0; i < iters; i++) {
baoJs.baoEncode(data, true);
}
const jsT = Date.now() - start;
start = Date.now();
for (let i = 0; i < iters; i++) {
wasmZC.baoEncodeWasm(data, true);
}
const wasmT = Date.now() - start;
const jsTP = formatThroughput(bytes * iters, jsT);
const wasmTP = formatThroughput(bytes * iters, wasmT);
const speedup = (jsT / wasmT).toFixed(2);
console.log(` ${name.padEnd(6)}: JS=${jsTP.padEnd(12)} WASM=${wasmTP.padEnd(12)} (${speedup}x)`);
}
}
runTests().catch(console.error);