|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { execSync, fork } from 'node:child_process'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { benchmarks } from '../benchmarks.js'; |
| 6 | + |
| 7 | +// if (execSync('git status --porcelain').toString().trim()) { |
| 8 | +// console.error('Working directory is not clean'); |
| 9 | +// process.exit(1); |
| 10 | +// } |
| 11 | + |
| 12 | +const filename = fileURLToPath(import.meta.url); |
| 13 | +const runner = path.resolve(filename, '../runner.js'); |
| 14 | +const outdir = path.resolve(filename, '../.results'); |
| 15 | + |
| 16 | +if (fs.existsSync(outdir)) fs.rmSync(outdir, { recursive: true }); |
| 17 | +fs.mkdirSync(outdir); |
| 18 | + |
| 19 | +const branches = []; |
| 20 | + |
| 21 | +for (const arg of process.argv.slice(2)) { |
| 22 | + if (arg.startsWith('--')) continue; |
| 23 | + if (arg === filename) continue; |
| 24 | + |
| 25 | + branches.push(arg); |
| 26 | +} |
| 27 | + |
| 28 | +if (branches.length === 0) { |
| 29 | + branches.push( |
| 30 | + execSync('git symbolic-ref --short -q HEAD || git rev-parse --short HEAD').toString().trim() |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +if (branches.length === 1) { |
| 35 | + branches.push('main'); |
| 36 | +} |
| 37 | + |
| 38 | +process.on('exit', () => { |
| 39 | + execSync(`git checkout ${branches[0]}`); |
| 40 | +}); |
| 41 | + |
| 42 | +for (const branch of branches) { |
| 43 | + console.group(`Benchmarking ${branch}`); |
| 44 | + |
| 45 | + execSync(`git checkout ${branch}`); |
| 46 | + |
| 47 | + await new Promise((fulfil, reject) => { |
| 48 | + const child = fork(runner); |
| 49 | + |
| 50 | + child.on('message', (results) => { |
| 51 | + fs.writeFileSync(`${outdir}/${branch}.json`, JSON.stringify(results, null, ' ')); |
| 52 | + fulfil(); |
| 53 | + }); |
| 54 | + |
| 55 | + child.on('error', reject); |
| 56 | + }); |
| 57 | + |
| 58 | + console.groupEnd(); |
| 59 | +} |
| 60 | + |
| 61 | +const results = branches.map((branch) => { |
| 62 | + return JSON.parse(fs.readFileSync(`${outdir}/${branch}.json`, 'utf-8')); |
| 63 | +}); |
| 64 | + |
| 65 | +for (let i = 0; i < results[0].length; i += 1) { |
| 66 | + console.group(`${results[0][i].benchmark}`); |
| 67 | + |
| 68 | + for (const metric of ['time', 'gc_time']) { |
| 69 | + const times = results.map((result) => +result[i][metric]); |
| 70 | + let min = Infinity; |
| 71 | + let min_index = -1; |
| 72 | + |
| 73 | + for (let b = 0; b < times.length; b += 1) { |
| 74 | + if (times[b] < min) { |
| 75 | + min = times[b]; |
| 76 | + min_index = b; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (min !== 0) { |
| 81 | + console.group(`${metric}: fastest is ${branches[min_index]}`); |
| 82 | + times.forEach((time, b) => { |
| 83 | + console.log(`${branches[b]}: ${time.toFixed(2)}ms (${((time / min) * 100).toFixed(2)}%)`); |
| 84 | + }); |
| 85 | + console.groupEnd(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + console.groupEnd(); |
| 90 | +} |
0 commit comments