benchmark-backends.mjs (2169B)
1 // Run through a Nix tooling shell with Node, Chromium and Firefox available. 2 import {spawn} from 'node:child_process'; 3 import {mkdir,readFile,writeFile} from 'node:fs/promises'; 4 import {resolve} from 'node:path'; 5 import {createHash} from 'node:crypto'; 6 import {cpus,release} from 'node:os'; 7 const [site,output]=process.argv.slice(2); 8 if(!site||!output)throw Error('Usage: benchmark-backends.mjs PACKAGED_SITE OUTPUT_DIRECTORY'); 9 await mkdir(output,{recursive:true}); 10 const reports=[],rounds=Number(process.env.BENCHMARK_ROUNDS??3); 11 if(!Number.isInteger(rounds)||rounds<1||rounds>10)throw Error('BENCHMARK_ROUNDS must be 1–10'); 12 for(const browser of ['chromium','firefox'])for(let round=0;round<rounds;round++)for(const backend of round%2?['haskell-wasm','zig-wasm']:['zig-wasm','haskell-wasm']){ 13 const file=resolve(output,`${browser}-${backend}-${round}.json`); 14 console.log('Benchmark',browser,backend,round); 15 const child=spawn(process.execPath,[`ext/demo-site/test/backend-${browser==='chromium'?'browser':'firefox'}.mjs`,resolve(site,'runtime/haskell'),browser],{ 16 env:{...process.env,ARBORICX_SITE_ROOT:resolve(site),TEST_BACKEND:backend,TEST_BENCHMARK:'1',BENCHMARK_REPORT:file},stdio:'inherit', 17 }); 18 const code=await new Promise((r,j)=>{child.on('error',j);child.on('exit',r);});if(code!==0)throw Error('Benchmark failed'); 19 reports.push({browser,round,...JSON.parse(await readFile(file,'utf8'))}); 20 } 21 const artifacts={}; 22 for(const [backend,name] of [['zig','arboricx'],['haskell','reactor']]){ 23 const bytes=await readFile(resolve(site,`runtime/${backend}/${name}.wasm`));artifacts[backend]={bytes:bytes.length,sha256:createHash('sha256').update(bytes).digest('hex')}; 24 } 25 const fixtures={}; 26 for(const name of ['demos.json','catalogue.json','compiler.json','environments.json']){const bytes=await readFile(resolve(name==='demos.json'?'ext/demo-site':site,name));fixtures[name]=createHash('sha256').update(bytes).digest('hex');} 27 await writeFile(resolve(output,'report.json'),JSON.stringify({rounds,memoryOnly:!!process.env.BENCHMARK_PSS,fixtures,site:resolve(site),platform:release(),cpu:cpus()[0]?.model,node:process.version,artifacts,reports},null,2)+'\n');