|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +const { Suite } = require('benchmark'); |
| 9 | +const beautifyBenchmark = require('beautify-benchmark'); |
| 10 | +const { execSync } = require('child_process'); |
| 11 | +const os = require('os'); |
| 12 | +const path = require('path'); |
| 13 | + |
| 14 | +// Like build:cjs, but includes __tests__ and copies other files. |
| 15 | +const BUILD_CMD = 'babel src --optional runtime --copy-files --out-dir dist/'; |
| 16 | +const LOCAL = 'local'; |
| 17 | +const LOCAL_DIR = path.join(__dirname, '../'); |
| 18 | +const TEMP_DIR = os.tmpdir(); |
| 19 | + |
| 20 | +// Returns the complete git hash for a given git revision reference. |
| 21 | +function hashForRevision(revision) { |
| 22 | + if (revision === LOCAL) { |
| 23 | + return revision; |
| 24 | + } |
| 25 | + const out = execSync(`git rev-parse "${revision}"`, { encoding: 'utf8' }); |
| 26 | + const match = /[0-9a-f]{8,40}/.exec(out); |
| 27 | + if (!match) { |
| 28 | + throw new Error(`Bad results for revision ${revision}: ${out}`); |
| 29 | + } |
| 30 | + return match[0]; |
| 31 | +} |
| 32 | + |
| 33 | +// Returns the temporary directory which hosts the files for this git hash. |
| 34 | +function dirForHash(hash) { |
| 35 | + if (hash === LOCAL) { |
| 36 | + return path.join(__dirname, '../'); |
| 37 | + } |
| 38 | + return path.join(TEMP_DIR, 'graphql-js-benchmark', hash); |
| 39 | +} |
| 40 | + |
| 41 | +// Build a benchmarkable environment for the given revision. |
| 42 | +function prepareRevision(revision) { |
| 43 | + console.log(`🍳 Preparing ${revision}...`); |
| 44 | + const hash = hashForRevision(revision); |
| 45 | + const dir = dirForHash(hash); |
| 46 | + if (hash === LOCAL) { |
| 47 | + execSync(`(cd "${dir}" && yarn run ${BUILD_CMD})`); |
| 48 | + } else { |
| 49 | + execSync(` |
| 50 | + if [ ! -d "${dir}" ]; then |
| 51 | + mkdir -p "${dir}" && |
| 52 | + git archive "${hash}" | tar -xC "${dir}" && |
| 53 | + (cd "${dir}" && yarn install); |
| 54 | + fi && |
| 55 | + # Copy in local tests so the same logic applies to each revision. |
| 56 | + for file in $(cd "${LOCAL_DIR}src"; find . -path '*/__tests__/*.js'); |
| 57 | + do cp "${LOCAL_DIR}src/$file" "${dir}/src/$file"; |
| 58 | + done && |
| 59 | + (cd "${dir}" && yarn run ${BUILD_CMD}) |
| 60 | + `); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Find all benchmark tests to be run. |
| 65 | +function findBenchmarks() { |
| 66 | + const out = execSync( |
| 67 | + `(cd ${LOCAL_DIR}src; find . -path '*/__tests__/*-benchmark.js')`, |
| 68 | + { encoding: 'utf8' }, |
| 69 | + ); |
| 70 | + return out.split('\n').filter(Boolean); |
| 71 | +} |
| 72 | + |
| 73 | +// Run a given benchmark test with the provided revisions. |
| 74 | +function runBenchmark(benchmark, revisions) { |
| 75 | + const modules = revisions.map(revision => |
| 76 | + require(path.join( |
| 77 | + dirForHash(hashForRevision(revision)), |
| 78 | + 'dist', |
| 79 | + benchmark, |
| 80 | + )), |
| 81 | + ); |
| 82 | + const suite = new Suite(modules[0].name, { |
| 83 | + onStart(event) { |
| 84 | + console.log('⏱️ ' + event.currentTarget.name); |
| 85 | + }, |
| 86 | + onCycle(event) { |
| 87 | + beautifyBenchmark.add(event.target); |
| 88 | + }, |
| 89 | + onComplete() { |
| 90 | + beautifyBenchmark.log(); |
| 91 | + }, |
| 92 | + }); |
| 93 | + for (let i = 0; i < revisions.length; i++) { |
| 94 | + suite.add(revisions[i], modules[i].measure); |
| 95 | + } |
| 96 | + suite.run(); |
| 97 | +} |
| 98 | + |
| 99 | +// Prepare all revisions and run benchmarks matching a pattern against them. |
| 100 | +function prepareAndRunBenchmarks(benchmarkPatterns, revisions) { |
| 101 | + const benchmarks = findBenchmarks().filter( |
| 102 | + benchmark => |
| 103 | + benchmarkPatterns.length === 0 || |
| 104 | + benchmarkPatterns.some(pattern => benchmark.indexOf(pattern) !== -1), |
| 105 | + ); |
| 106 | + if (benchmarks.length === 0) { |
| 107 | + console.warn( |
| 108 | + 'No benchmarks matching: ' + |
| 109 | + `\u001b[1m${benchmarkPatterns.join('\u001b[0m or \u001b[1m')}\u001b[0m`, |
| 110 | + ); |
| 111 | + return; |
| 112 | + } |
| 113 | + revisions.forEach(revision => prepareRevision(revision)); |
| 114 | + benchmarks.forEach(benchmark => runBenchmark(benchmark, revisions)); |
| 115 | +} |
| 116 | + |
| 117 | +function getArguments(argv) { |
| 118 | + const revsIdx = argv.indexOf('--revs'); |
| 119 | + const revsArgs = revsIdx === -1 ? [] : argv.slice(revsIdx + 1); |
| 120 | + const benchmarkPatterns = revsIdx === -1 ? argv : argv.slice(0, revsIdx); |
| 121 | + let assumeArgs; |
| 122 | + let revisions; |
| 123 | + switch (revsArgs.length) { |
| 124 | + case 0: |
| 125 | + assumeArgs = [...benchmarkPatterns, '--revs', 'local', 'HEAD']; |
| 126 | + revisions = [LOCAL, 'HEAD']; |
| 127 | + break; |
| 128 | + case 1: |
| 129 | + assumeArgs = [...benchmarkPatterns, '--revs', 'local', revsArgs[0]]; |
| 130 | + revisions = [LOCAL, revsArgs[0]]; |
| 131 | + break; |
| 132 | + default: |
| 133 | + revisions = revsArgs; |
| 134 | + break; |
| 135 | + } |
| 136 | + if (assumeArgs) { |
| 137 | + console.warn( |
| 138 | + `Assuming you meant: \u001b[1mbenchmark ${assumeArgs.join(' ')}\u001b[0m`, |
| 139 | + ); |
| 140 | + } |
| 141 | + return { benchmarkPatterns, revisions }; |
| 142 | +} |
| 143 | + |
| 144 | +// Get the revisions and make things happen! |
| 145 | +if (require.main === module) { |
| 146 | + const { benchmarkPatterns, revisions } = getArguments(process.argv.slice(2)); |
| 147 | + prepareAndRunBenchmarks(benchmarkPatterns, revisions); |
| 148 | +} |
0 commit comments