|
| 1 | +const benchmark = require('benchmark'); |
| 2 | +const beautifyBenchmark = require('beautify-benchmark'); |
| 3 | +const sh = require('shelljs'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const pathJoin = require('path').join; |
| 6 | + |
| 7 | +const args = process.argv.slice(2); |
| 8 | +args[0] = args[0] || 'HEAD'; |
| 9 | +args[1] = args[1] || 'local'; |
| 10 | + |
| 11 | +console.log('Benchmarking revisions: ' + args.join(', ')); |
| 12 | + |
| 13 | +const localDistDir = './benchmark/local'; |
| 14 | +sh.rm('-rf', localDistDir); |
| 15 | +console.log(`Building local dist: ${localDistDir}`); |
| 16 | +sh.mkdir('-p', localDistDir); |
| 17 | +exec(`babel src --optional runtime --copy-files --out-dir ${localDistDir}`); |
| 18 | + |
| 19 | +const revisions = {}; |
| 20 | +for (const arg of args) { |
| 21 | + const distPath = buildRevisionDist(arg); |
| 22 | + const distRequire = (path) => reqireFromCWD(pathJoin(distPath, path)); |
| 23 | + revisions[arg] = distRequire; |
| 24 | +} |
| 25 | + |
| 26 | +const suites = {}; |
| 27 | +global.suite = suite; |
| 28 | +const testFiles = sh.ls(`${localDistDir}/**/__tests__/**/*-benchmark.js`); |
| 29 | +for (const file of testFiles) { |
| 30 | + reqireFromCWD(file); |
| 31 | +} |
| 32 | + |
| 33 | +dummyRun(); |
| 34 | +for (const [name, measures] of Object.entries(suites)) { |
| 35 | + console.log(chalk.green(name) + '\n'); |
| 36 | + benchmark.invoke(measures, 'run'); |
| 37 | +} |
| 38 | + |
| 39 | +function reqireFromCWD(path) { |
| 40 | + return require(pathJoin(process.cwd(), path)) |
| 41 | +} |
| 42 | + |
| 43 | +function dummyRun() { |
| 44 | + (new benchmark.Suite('dummy')) |
| 45 | + .add('dummy', () => { Math.pow(2, 256); }) |
| 46 | + .run(); |
| 47 | +} |
| 48 | + |
| 49 | +function newMeasurement(name) { |
| 50 | + return new benchmark.Suite(name, { |
| 51 | + onStart(event) { |
| 52 | + console.log(' ⏱️ ', event.currentTarget.name); |
| 53 | + }, |
| 54 | + onCycle(event) { |
| 55 | + beautifyBenchmark.add(event.target); |
| 56 | + }, |
| 57 | + onComplete() { |
| 58 | + beautifyBenchmark.log(); |
| 59 | + }, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function suite(name, fn) { |
| 64 | + const measures = {}; |
| 65 | + for (const [revision, distRequire] of Object.entries(revisions)) { |
| 66 | + currentRevision = revision; |
| 67 | + global.measure = (name, fn) => { |
| 68 | + measures[name] = measures[name] || newMeasurement(name); |
| 69 | + measures[name].add(revision, fn); |
| 70 | + }; |
| 71 | + try { |
| 72 | + fn(distRequire); |
| 73 | + } catch (e) { |
| 74 | + console.error(e.stack); |
| 75 | + } |
| 76 | + } |
| 77 | + global.measure = undefined; |
| 78 | + suites[name] = Object.values(measures); |
| 79 | +} |
| 80 | + |
| 81 | +function exec(command) { |
| 82 | + const {code, stdout, stderr} = sh.exec(command, {silent: true}); |
| 83 | + if (code !== 0) { |
| 84 | + console.error(stdout); |
| 85 | + console.error(stderr); |
| 86 | + sh.exit(code); |
| 87 | + } |
| 88 | + return stdout.trim(); |
| 89 | +} |
| 90 | + |
| 91 | +function buildRevisionDist(revision) { |
| 92 | + if (revision === 'local') { |
| 93 | + return localDistDir; |
| 94 | + } |
| 95 | + |
| 96 | + const hash = exec(`git log -1 --format=%h "${revision}"`); |
| 97 | + const buildDir = './benchmark/' + hash; |
| 98 | + const distDir = buildDir + '/dist' |
| 99 | + |
| 100 | + if (sh.test('-d', buildDir)) { |
| 101 | + return distDir; |
| 102 | + } |
| 103 | + console.log(`Building "${revision}"(${hash}) revision: ${buildDir}`); |
| 104 | + sh.mkdir('-p', buildDir); |
| 105 | + exec(`git archive "${hash}" | tar -xC "${buildDir}"`); |
| 106 | + |
| 107 | + const pwd = sh.pwd(); |
| 108 | + sh.cd(buildDir); |
| 109 | + exec('yarn && npm run build'); |
| 110 | + sh.cd(pwd); |
| 111 | + return buildDir + '/dist'; |
| 112 | +} |
0 commit comments