diff --git a/scripts/bench/benchmark.js b/scripts/bench/benchmark.js index 27af0ac43c..2e5b2e0b86 100644 --- a/scripts/bench/benchmark.js +++ b/scripts/bench/benchmark.js @@ -1,7 +1,8 @@ 'use strict'; const Lighthouse = require('lighthouse'); -const ChromeLauncher = require('lighthouse/lighthouse-cli/chrome-launcher.js').ChromeLauncher; +const ChromeLauncher = require('lighthouse/lighthouse-cli/chrome-launcher.js') + .ChromeLauncher; const stats = require('stats-analysis'); const config = require('lighthouse/lighthouse-core/config/perf.json'); const spawn = require('child_process').spawn; @@ -14,18 +15,22 @@ function wait(val) { } async function runScenario(benchmark, launcher) { - const results = await Lighthouse(`http://localhost:8080/${benchmark}/`, { - output: 'json', - disableCpuThrottling: false, - disableNetworkThrottling: false, - }, config); + const results = await Lighthouse( + `http://localhost:8080/${benchmark}/`, + { + output: 'json', + disableCpuThrottling: false, + disableNetworkThrottling: false, + }, + config + ); const perfMarkings = results.audits['user-timings'].extendedInfo.value; const entries = perfMarkings - .filter(marker => !marker.isMark) - .map(({ duration, name }) => ({ - entry: name, - time: duration, - })); + .filter(marker => !marker.isMark) + .map(({duration, name}) => ({ + entry: name, + time: duration, + })); entries.push({ entry: 'First Meaningful Paint', time: results.audits['first-meaningful-paint'].rawValue, @@ -55,7 +60,7 @@ function calculateAverages(runs) { const averages = []; runs.forEach((entries, x) => { - entries.forEach(({ entry, time }, i) => { + entries.forEach(({entry, time}, i) => { if (i >= averages.length) { data.push([time]); averages.push({ @@ -83,7 +88,7 @@ async function initChrome() { if (platform === 'linux') { process.env.XVFBARGS = '-screen 0, 1024x768x16'; process.env.LIGHTHOUSE_CHROMIUM_PATH = 'chromium-browser'; - const child = spawn('xvfb start', [{ detached: true, stdio: ['ignore'] }]); + const child = spawn('xvfb start', [{detached: true, stdio: ['ignore']}]); child.unref(); // wait for chrome to load then continue await wait(3000); @@ -128,4 +133,3 @@ async function runBenchmark(benchmark, headless) { } module.exports = runBenchmark; - diff --git a/scripts/bench/build.js b/scripts/bench/build.js index 81ec025591..7ec4021124 100644 --- a/scripts/bench/build.js +++ b/scripts/bench/build.js @@ -3,13 +3,9 @@ const Git = require('nodegit'); const rimraf = require('rimraf'); const ncp = require('ncp').ncp; -const { - existsSync, -} = require('fs'); +const {existsSync} = require('fs'); const exec = require('child_process').exec; -const { - join, -} = require('path'); +const {join} = require('path'); const reactUrl = 'https://github.com/facebook/react.git'; @@ -18,14 +14,16 @@ function cleanDir() { } function executeCommand(command) { - return new Promise(_resolve => exec(command, (error) => { - if (!error) { - _resolve(); - } else { - console.error(error); - process.exit(1); - } - })); + return new Promise(_resolve => + exec(command, error => { + if (!error) { + _resolve(); + } else { + console.error(error); + process.exit(1); + } + }) + ); } function asyncCopyTo(from, to) { @@ -51,9 +49,10 @@ async function buldAllBundles(reactPath = getDefaultReactPath()) { async function buildBenchmark(reactPath = getDefaultReactPath(), benchmark) { // get the build.js from the benchmark directory and execute it - await require( - join(__dirname, 'benchmarks', benchmark, 'build.js') - )(reactPath, asyncCopyTo); + await require(join(__dirname, 'benchmarks', benchmark, 'build.js'))( + reactPath, + asyncCopyTo + ); } function getBundleResults(reactPath = getDefaultReactPath()) { @@ -69,7 +68,12 @@ async function getMergeBaseFromLocalGitRepo(localRepo) { ); } -async function buildBenchmarkBundlesFromGitRepo(commitId, skipBuild, url = reactUrl, clean) { +async function buildBenchmarkBundlesFromGitRepo( + commitId, + skipBuild, + url = reactUrl, + clean +) { let repo; const remoteRepoDir = getDefaultReactPath(); diff --git a/scripts/bench/runner.js b/scripts/bench/runner.js index 02e3b685bc..7d057faf3c 100644 --- a/scripts/bench/runner.js +++ b/scripts/bench/runner.js @@ -1,10 +1,7 @@ 'use strict'; -const { - readdirSync, - statSync, -} = require('fs'); -const { join } = require('path'); +const {readdirSync, statSync} = require('fs'); +const {join} = require('path'); const runBenchmark = require('./benchmark'); const { buildAllBundles, @@ -18,8 +15,8 @@ const printResults = require('./stats'); const serveBenchmark = require('./server'); function getBenchmarkNames() { - return readdirSync(join(__dirname, 'benchmarks')).filter( - file => statSync(join(__dirname, 'benchmarks', file)).isDirectory() + return readdirSync(join(__dirname, 'benchmarks')).filter(file => + statSync(join(__dirname, 'benchmarks', file)).isDirectory() ); } @@ -43,13 +40,16 @@ async function runBenchmarks(reactPath) { const benchmarkName = benchmarkNames[i]; if ( - !benchmarkFilter - || + !benchmarkFilter || (benchmarkFilter && benchmarkName.indexOf(benchmarkFilter) !== -1) ) { - console.log(chalk.gray(`- Building benchmark "${chalk.white(benchmarkName)}"...`)); + console.log( + chalk.gray(`- Building benchmark "${chalk.white(benchmarkName)}"...`) + ); await buildBenchmark(reactPath, benchmarkName); - console.log(chalk.gray(`- Running benchmark "${chalk.white(benchmarkName)}"...`)); + console.log( + chalk.gray(`- Running benchmark "${chalk.white(benchmarkName)}"...`) + ); results[benchmarkName] = await runBenchmark(benchmarkName, headless); } } @@ -68,14 +68,13 @@ async function benchmarkRemoteMaster() { if (!commit || typeof commit !== 'string') { commit = await getMergeBaseFromLocalGitRepo(join(__dirname, '..', '..')); - console.log(chalk.gray(`- Merge base commit ${chalk.white(commit.tostrS())}`)); + console.log( + chalk.gray(`- Merge base commit ${chalk.white(commit.tostrS())}`) + ); } return { // we build the bundles from the React repo - bundles: await buildBenchmarkBundlesFromGitRepo( - commit, - skipBuild - ), + bundles: await buildBenchmarkBundlesFromGitRepo(commit, skipBuild), // we use these bundles to run the benchmarks benchmarks: await runBenchmarks(), }; @@ -95,8 +94,8 @@ async function benchmarkLocal(reactPath) { async function runLocalBenchmarks(showResults) { console.log( - chalk.white.bold('Running benchmarks for ') - + chalk.green.bold('Local (Current Branch)') + chalk.white.bold('Running benchmarks for ') + + chalk.green.bold('Local (Current Branch)') ); const localResults = await benchmarkLocal(join(__dirname, '..', '..')); @@ -108,8 +107,8 @@ async function runLocalBenchmarks(showResults) { async function runRemoteBenchmarks(showResults) { console.log( - chalk.white.bold('Running benchmarks for ') - + chalk.yellow.bold('Remote (Merge Base)') + chalk.white.bold('Running benchmarks for ') + + chalk.yellow.bold('Remote (Merge Base)') ); const remoteMasterResults = await benchmarkRemoteMaster(); @@ -121,10 +120,10 @@ async function runRemoteBenchmarks(showResults) { async function compareLocalToMaster() { console.log( - chalk.white.bold('Comparing ') - + chalk.green.bold('Local (Current Branch)') - + chalk.white.bold(' to ') - + chalk.yellow.bold('Remote (Merge Base)') + chalk.white.bold('Comparing ') + + chalk.green.bold('Local (Current Branch)') + + chalk.white.bold(' to ') + + chalk.yellow.bold('Remote (Merge Base)') ); const localResults = await runLocalBenchmarks(false); const remoteMasterResults = await runRemoteBenchmarks(false); diff --git a/scripts/bench/server.js b/scripts/bench/server.js index 8ea59713d3..bd20e7ca54 100644 --- a/scripts/bench/server.js +++ b/scripts/bench/server.js @@ -2,12 +2,8 @@ const http2Server = require('http2'); const httpServer = require('http-server'); -const { - existsSync, - statSync, - createReadStream, -} = require('fs'); -const { join } = require('path'); +const {existsSync, statSync, createReadStream} = require('fs'); +const {join} = require('path'); const argv = require('minimist')(process.argv.slice(2)); const mime = require('mime'); @@ -21,7 +17,12 @@ function sendFile(filename, response) { function createHTTP2Server(benchmark) { const server = http2Server.createServer({}, (request, response) => { - const filename = join(__dirname, 'benchmarks', benchmark, request.url).replace(/\?.*/g, ''); + const filename = join( + __dirname, + 'benchmarks', + benchmark, + request.url + ).replace(/\?.*/g, ''); if (existsSync(filename) && statSync(filename).isFile()) { sendFile(filename, response); diff --git a/scripts/bench/stats.js b/scripts/bench/stats.js index 7e58b719b8..2a651e1ded 100644 --- a/scripts/bench/stats.js +++ b/scripts/bench/stats.js @@ -31,20 +31,19 @@ function calculateMeanAndSdOfRatioFromDeltaMethod( semControl, semTest ) { - const mean = ( - ((meanTest - meanControl) / meanControl) - - (Math.pow(semControl, 2) * meanTest / Math.pow(meanControl, 3)) - ); - const variance = ( + const mean = + (meanTest - meanControl) / meanControl - + Math.pow(semControl, 2) * meanTest / Math.pow(meanControl, 3); + const variance = Math.pow(semTest / meanControl, 2) + - (Math.pow(semControl * meanTest, 2) / Math.pow(meanControl, 4)) - ); + Math.pow(semControl * meanTest, 2) / Math.pow(meanControl, 4); return [mean, Math.sqrt(variance)]; } function addBenchmarkResults(table, localResults, remoteMasterResults) { const benchmarks = Object.keys( - (localResults && localResults.benchmarks) || (remoteMasterResults && remoteMasterResults.benchmarks) + (localResults && localResults.benchmarks) || + (remoteMasterResults && remoteMasterResults.benchmarks) ); benchmarks.forEach(benchmark => { const rowHeader = [chalk.white.bold(benchmark)]; @@ -59,15 +58,12 @@ function addBenchmarkResults(table, localResults, remoteMasterResults) { } table.push(rowHeader); - const measurements = ( - (localResults && localResults.benchmarks[benchmark].averages) - || - (remoteMasterResults && remoteMasterResults.benchmarks[benchmark].averages) - ); + const measurements = + (localResults && localResults.benchmarks[benchmark].averages) || + (remoteMasterResults && + remoteMasterResults.benchmarks[benchmark].averages); measurements.forEach((measurement, i) => { - const row = [ - chalk.gray(measurement.entry), - ]; + const row = [chalk.gray(measurement.entry)]; let remoteMean; let remoteSem; if (remoteMasterResults) { @@ -75,7 +71,9 @@ function addBenchmarkResults(table, localResults, remoteMasterResults) { remoteSem = remoteMasterResults.benchmarks[benchmark].averages[i].sem; // https://en.wikipedia.org/wiki/1.96 gives a 99% confidence interval. const ci95 = remoteSem * 1.96; - row.push(chalk.white(+remoteMean.toFixed(2) + ' ms +- ' + ci95.toFixed(2))); + row.push( + chalk.white(+remoteMean.toFixed(2) + ' ms +- ' + ci95.toFixed(2)) + ); } let localMean; let localSem; @@ -83,7 +81,9 @@ function addBenchmarkResults(table, localResults, remoteMasterResults) { localMean = localResults.benchmarks[benchmark].averages[i].mean; localSem = localResults.benchmarks[benchmark].averages[i].sem; const ci95 = localSem * 1.96; - row.push(chalk.white(+localMean.toFixed(2) + ' ms +- ' + ci95.toFixed(2))); + row.push( + chalk.white(+localMean.toFixed(2) + ' ms +- ' + ci95.toFixed(2)) + ); } if (localResults && remoteMasterResults) { row.push(percentChange(remoteMean, localMean, remoteSem, localSem)); @@ -107,17 +107,15 @@ function addBundleSizeComparions(table, localResults, remoteMasterResults) { table.push(bundlesRowHeader); const bundles = Object.keys( - (localResults && localResults.bundles.bundleSizes) - || - (remoteMasterResults && remoteMasterResults.bundles.bundleSizes) + (localResults && localResults.bundles.bundleSizes) || + (remoteMasterResults && remoteMasterResults.bundles.bundleSizes) ); bundles.forEach(bundle => { - const row = [ - chalk.gray(bundle), - ]; + const row = [chalk.gray(bundle)]; let remoteSize = 0; if (remoteMasterResults) { - const remoteBundle = remoteSize = remoteMasterResults.bundles.bundleSizes[bundle]; + const remoteBundle = (remoteSize = + remoteMasterResults.bundles.bundleSizes[bundle]); if (remoteBundle) { remoteSize = remoteSize.size; @@ -152,7 +150,7 @@ function printResults(localResults, remoteMasterResults) { if (localResults && remoteMasterResults) { head.push(''); } - const table = new Table({ head }); + const table = new Table({head}); addBundleSizeComparions(table, localResults, remoteMasterResults); addBenchmarkResults(table, localResults, remoteMasterResults); diff --git a/scripts/prettier/index.js b/scripts/prettier/index.js index 2447445c82..728d4c8b51 100644 --- a/scripts/prettier/index.js +++ b/scripts/prettier/index.js @@ -39,7 +39,7 @@ const config = { }, scripts: { patterns: ['scripts/**/*.js'], - ignore: ['**/bench/**'], + ignore: ['scripts/bench/benchmarks/**'], options: { 'trailing-comma': 'es5', },