| 1 | 'use strict'; |
| 2 | |
| 3 | const {readdirSync, statSync} = require('fs'); |
| 4 | const {join} = require('path'); |
| 5 | const runBenchmark = require('./benchmark'); |
| 6 | const { |
| 7 | buildReactBundles, |
| 8 | buildBenchmark, |
| 9 | buildBenchmarkBundlesFromGitRepo, |
| 10 | getMergeBaseFromLocalGitRepo, |
| 11 | } = require('./build'); |
| 12 | const argv = require('minimist')(process.argv.slice(2)); |
| 13 | const chalk = require('chalk'); |
| 14 | const printResults = require('./stats'); |
| 15 | const serveBenchmark = require('./server'); |
| 16 | |
| 17 | function getBenchmarkNames() { |
| 18 | return readdirSync(join(__dirname, 'benchmarks')).filter(file => |
| 19 | statSync(join(__dirname, 'benchmarks', file)).isDirectory() |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | function wait(val) { |
| 24 | return new Promise(resolve => setTimeout(resolve, val)); |
| 25 | } |
| 26 | |
| 27 | const runRemote = argv.remote; |
| 28 | const runLocal = argv.local; |
| 29 | const benchmarkFilter = argv.benchmark; |
| 30 | const headless = argv.headless; |
| 31 | const skipBuild = argv['skip-build']; |
| 32 | |
| 33 | async function runBenchmarks(reactPath) { |
| 34 | const benchmarkNames = getBenchmarkNames(); |
| 35 | const results = {}; |
| 36 | const server = serveBenchmark(); |
| 37 | await wait(1000); |
| 38 | |
| 39 | for (let i = 0; i < benchmarkNames.length; i++) { |
| 40 | const benchmarkName = benchmarkNames[i]; |
| 41 | |
| 42 | if ( |
| 43 | !benchmarkFilter || |
| 44 | (benchmarkFilter && benchmarkName.indexOf(benchmarkFilter) !== -1) |
| 45 | ) { |
| 46 | console.log( |
| 47 | chalk.gray(`- Building benchmark "${chalk.white(benchmarkName)}"...`) |
| 48 | ); |
| 49 | await buildBenchmark(reactPath, benchmarkName); |
| 50 | console.log( |
| 51 | chalk.gray(`- Running benchmark "${chalk.white(benchmarkName)}"...`) |
| 52 | ); |
| 53 | results[benchmarkName] = await runBenchmark(benchmarkName, headless); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | server.close(); |
| 58 | // http-server.close() is async but they don't provide a callback.. |
| 59 | await wait(500); |
| 60 | return results; |
| 61 | } |
| 62 | |
| 63 | // get the performance benchmark results |
| 64 | // from remote main (default React repo) |
| 65 | async function benchmarkRemoteMaster() { |
| 66 | console.log(chalk.gray(`- Building React bundles...`)); |
| 67 | let commit = argv.remote; |
| 68 | |
| 69 | if (!commit || typeof commit !== 'string') { |
| 70 | commit = await getMergeBaseFromLocalGitRepo(join(__dirname, '..', '..')); |
| 71 | console.log( |
| 72 | chalk.gray(`- Merge base commit ${chalk.white(commit.tostrS())}`) |
| 73 | ); |
| 74 | } |
| 75 | await buildBenchmarkBundlesFromGitRepo(commit, skipBuild); |
| 76 | return { |
| 77 | benchmarks: await runBenchmarks(), |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | // get the performance benchmark results |
| 82 | // of the local react repo |
| 83 | async function benchmarkLocal(reactPath) { |
| 84 | console.log(chalk.gray(`- Building React bundles...`)); |
| 85 | await buildReactBundles(reactPath, skipBuild); |
| 86 | return { |
| 87 | benchmarks: await runBenchmarks(reactPath), |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | async function runLocalBenchmarks(showResults) { |
| 92 | console.log( |
| 93 | chalk.white.bold('Running benchmarks for ') + |
| 94 | chalk.green.bold('Local (Current Branch)') |
| 95 | ); |
| 96 | const localResults = await benchmarkLocal(join(__dirname, '..', '..')); |
| 97 | |
| 98 | if (showResults) { |
| 99 | printResults(localResults, null); |
| 100 | } |
| 101 | return localResults; |
| 102 | } |
| 103 | |
| 104 | async function runRemoteBenchmarks(showResults) { |
| 105 | console.log( |
| 106 | chalk.white.bold('Running benchmarks for ') + |
| 107 | chalk.yellow.bold('Remote (Merge Base)') |
| 108 | ); |
| 109 | const remoteMasterResults = await benchmarkRemoteMaster(); |
| 110 | |
| 111 | if (showResults) { |
| 112 | printResults(null, remoteMasterResults); |
| 113 | } |
| 114 | return remoteMasterResults; |
| 115 | } |
| 116 | |
| 117 | async function compareLocalToMaster() { |
| 118 | console.log( |
| 119 | chalk.white.bold('Comparing ') + |
| 120 | chalk.green.bold('Local (Current Branch)') + |
| 121 | chalk.white.bold(' to ') + |
| 122 | chalk.yellow.bold('Remote (Merge Base)') |
| 123 | ); |
| 124 | const localResults = await runLocalBenchmarks(false); |
| 125 | const remoteMasterResults = await runRemoteBenchmarks(false); |
| 126 | printResults(localResults, remoteMasterResults); |
| 127 | } |
| 128 | |
| 129 | if ((runLocal && runRemote) || (!runLocal && !runRemote)) { |
| 130 | compareLocalToMaster().then(() => process.exit(0)); |
| 131 | } else if (runLocal) { |
| 132 | runLocalBenchmarks(true).then(() => process.exit(0)); |
| 133 | } else if (runRemote) { |
| 134 | runRemoteBenchmarks(true).then(() => process.exit(0)); |
| 135 | } |