// Runs the wasm benchmark modules off the main thread. The threaded (mt) // variant spawns its pthread workers from here (nested workers). 'use strict'; const instances = {}; // variant -> { module, run } async function getInstance(variant) { if (!instances[variant]) { importScripts(`dist/bench_${variant}.js`); const factory = variant === 'mt' ? createBenchMT : createBenchST; const module = await factory({ // Loaded via importScripts, the module would otherwise resolve the // .wasm (and the script pthread workers re-load) relative to THIS // worker's URL — point both back into dist/. locateFile: (path) => 'dist/' + path, mainScriptUrlOrBlob: `dist/bench_${variant}.js`, print: (line) => postMessage({ type: 'log', line }), printErr: (line) => postMessage({ type: 'log', line: '[stderr] ' + line }), }); instances[variant] = { module, run: module.cwrap('run_bench', 'number', ['number', 'number', 'number']), }; } return instances[variant]; } onmessage = async (e) => { const { id, variant, routine, n, threads } = e.data; try { const inst = await getInstance(variant); const t0 = performance.now(); const gflops = inst.run(routine, n, threads); const seconds = (performance.now() - t0) / 1000; postMessage({ type: 'result', id, gflops, seconds }); } catch (err) { postMessage({ type: 'error', id, message: String(err && err.message || err) }); } };