-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-rust-wasm.js
More file actions
239 lines (208 loc) · 7.15 KB
/
test-rust-wasm.js
File metadata and controls
239 lines (208 loc) · 7.15 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
/**
* Test Rust WASM Bao operations.
*/
'use strict';
const rustWasm = require('./bao-rust-wasm.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('=== Rust WASM Bao Tests ===\n');
console.log('Initializing Rust WASM...');
const ok = await rustWasm.initWasm();
if (!ok) {
console.error('Rust WASM initialization failed!');
process.exit(1);
}
console.log('Rust WASM enabled:', rustWasm.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 rustEmpty = rustWasm.chunkCV(new Uint8Array(0), 0, true);
if (toHex(jsEmpty) === toHex(rustEmpty)) {
console.log('PASS');
console.log(' Hash:', toHex(jsEmpty).substring(0, 32) + '...');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(jsEmpty));
console.log(' Rust:', toHex(rustEmpty));
failed++;
}
// Test 2: Single byte chunk CV
console.log('--- Test 2: Single byte chunk CV ---');
const js1 = baoJs.chunkCV(new Uint8Array([42]), 0, false);
const rust1 = rustWasm.chunkCV(new Uint8Array([42]), 0, false);
if (toHex(js1) === toHex(rust1)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(js1));
console.log(' Rust:', toHex(rust1));
failed++;
}
// Test 3: Full chunk CV (1024 bytes)
console.log('--- Test 3: Full chunk CV (1024 bytes) ---');
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 rustFull = rustWasm.chunkCV(fullChunk, 5, false);
if (toHex(jsFull) === toHex(rustFull)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(jsFull));
console.log(' Rust:', toHex(rustFull));
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 rustParent = rustWasm.parentCV(left, right, false);
if (toHex(jsParent) === toHex(rustParent)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(jsParent));
console.log(' Rust:', toHex(rustParent));
failed++;
}
// Test 5: Parent CV with isRoot
console.log('--- Test 5: Parent CV (root) ---');
const jsParentRoot = baoJs.parentCV(left, right, true);
const rustParentRoot = rustWasm.parentCV(left, right, true);
if (toHex(jsParentRoot) === toHex(rustParentRoot)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' JS: ', toHex(jsParentRoot));
console.log(' Rust:', toHex(rustParentRoot));
failed++;
}
// Test 6: Batch chunk CVs
console.log('--- Test 6: 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 rustBatch = rustWasm.batchChunkCVs(batchData, 0, 4);
let batchOk = true;
for (let i = 0; i < 4; i++) {
if (toHex(jsBatch[i]) !== toHex(rustBatch[i])) {
batchOk = false;
console.log(` Chunk ${i} mismatch:`);
console.log(` JS: `, toHex(jsBatch[i]));
console.log(` Rust:`, toHex(rustBatch[i]));
}
}
if (batchOk) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
failed++;
}
// Test 7: Zero-copy direct API
console.log('--- Test 7: Zero-copy direct API ---');
const input = rustWasm.getInputBuffer();
input.set(fullChunk, 0);
const directCV = rustWasm.chunkCVDirect(1024, 5, false);
if (toHex(directCV) === toHex(jsFull)) {
console.log('PASS');
passed++;
} else {
console.log('FAIL');
console.log(' Expected:', toHex(jsFull));
console.log(' Got: ', toHex(directCV));
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);
rustWasm.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++) {
rustWasm.chunkCV(fullChunk, i, false);
}
const rustTime = Date.now() - start;
// Zero-copy benchmark
input.set(fullChunk, 0);
start = Date.now();
for (let i = 0; i < iterations; i++) {
rustWasm.chunkCVDirect(1024, i, false);
}
const zcTime = Date.now() - start;
console.log(` JS: ${jsTime}ms (${formatThroughput(iterations * 1024, jsTime)})`);
console.log(` Rust: ${rustTime}ms (${formatThroughput(iterations * 1024, rustTime)}) - ${(jsTime/rustTime).toFixed(2)}x`);
console.log(` Zero-copy: ${zcTime}ms (${formatThroughput(iterations * 1024, zcTime)}) - ${(jsTime/zcTime).toFixed(2)}x`);
// Benchmark 2: Batch chunk CVs
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++) {
rustWasm.batchChunkCVs(batch64, 0, 64);
}
const rustBatchTime = Date.now() - start;
console.log(` JS Sequential: ${jsSeqTime}ms (${formatThroughput(batchIterations * 64 * 1024, jsSeqTime)})`);
console.log(` Rust Batch: ${rustBatchTime}ms (${formatThroughput(batchIterations * 64 * 1024, rustBatchTime)}) - ${(jsSeqTime/rustBatchTime).toFixed(2)}x`);
// Benchmark 3: Parent CV
console.log('');
console.log('--- parentCV (10,000 iterations) ---');
start = Date.now();
for (let i = 0; i < iterations; i++) {
baoJs.parentCV(left, right, false);
}
const jsParentTime = Date.now() - start;
start = Date.now();
for (let i = 0; i < iterations; i++) {
rustWasm.parentCV(left, right, false);
}
const rustParentTime = Date.now() - start;
console.log(` JS: ${jsParentTime}ms`);
console.log(` Rust: ${rustParentTime}ms - ${(jsParentTime/rustParentTime).toFixed(2)}x`);
}
runTests().catch(console.error);