import WasmModule from './binaries/wasm_add_pthread.js'; async function main() { const loadStart = Date.now(); const wasmModule = await WasmModule(); const loadDuration = Date.now() - loadStart; console.log(`WASM module load time: ${loadDuration} ms`); if (!wasmModule.wasmMemory || !wasmModule.wasmMemory.buffer) { console.error("wasmMemory or wasmMemory.buffer not found!"); process.exit(1); } const ARRAY_SIZE = 15000576; const allocStart = Date.now(); const aPtr = wasmModule._alloc_float_array(ARRAY_SIZE); const bPtr = wasmModule._alloc_float_array(ARRAY_SIZE); const cPtr = wasmModule._alloc_float_array(ARRAY_SIZE); const allocDuration = Date.now() - allocStart; console.log(`Memory allocation time: ${allocDuration} ms`); const initStart = Date.now(); wasmModule._init_arrays(aPtr, bPtr); const initDuration = Date.now() - initStart; console.log(`Array initialization time: ${initDuration} ms`); const memory = new Float32Array(wasmModule.wasmMemory.buffer); const calcStart = Date.now(); for (let i = 0; i < 100; i++) { wasmModule._calculate_range(aPtr, bPtr, cPtr, 0, ARRAY_SIZE); } const calcDuration = Date.now() - calcStart; console.log(`Average calc time: ${(calcDuration / 100).toFixed(2)} ms`); for (let i = 0; i < 10; i++) { console.log(`c[${i}] = ${memory[cPtr / 4 + i]}`); } var testSize = 1000000; // Benchmark the copy process const copyStart = Date.now(); const jsArray = new Float32Array(testSize); jsArray.set(memory.subarray(aPtr / 4, aPtr / 4 + testSize)); const copyDuration = Date.now() - copyStart; console.log(`Copy time from WASM to JS ${testSize} items: ${copyDuration} ms`); // the jsArray can be used inside WebGpu. const deallocStart = Date.now(); wasmModule._dealloc_float_array(aPtr); wasmModule._dealloc_float_array(bPtr); wasmModule._dealloc_float_array(cPtr); const deallocDuration = Date.now() - deallocStart; console.log(`Memory deallocation time: ${deallocDuration} ms`); } main();