|
| 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 | +// Get the revisions and make things happen! |
| 21 | +const {benchmarkPatterns, revisions} = getArguments(process.argv.slice(2)); |
| 22 | +prepareAndRunBenchmarks(benchmarkPatterns, revisions); |
| 23 | + |
| 24 | +// Returns the complete git hash for a given git revision reference. |
| 25 | +function hashForRevision(revision) { |
| 26 | + if (revision === LOCAL) { |
| 27 | + return revision; |
| 28 | + } |
| 29 | + const out = execSync(`git rev-parse "${revision}"`, {encoding: 'utf8'}); |
| 30 | + const match = /[0-9a-f]{8,40}/.exec(out); |
| 31 | + if (!match) { |
| 32 | + throw new Error(`Bad results for revision ${revision}: ${out}`); |
| 33 | + } |
| 34 | + return match[0]; |
| 35 | +} |
| 36 | + |
| 37 | +// Returns the temporary directory which hosts the files for this git hash. |
| 38 | +function dirForHash(hash) { |
| 39 | + if (hash === LOCAL) { |
| 40 | + return path.join(__dirname, '../'); |
| 41 | + } |
| 42 | + return path.join(TEMP_DIR, 'graphql-js-benchmark', hash); |
| 43 | +} |
| 44 | + |
| 45 | +// Build a benchmarkable environment for the given revision. |
| 46 | +function prepareRevision(revision) { |
| 47 | + const hash = hashForRevision(revision); |
| 48 | + const dir = dirForHash(hash); |
| 49 | + if (hash === LOCAL) { |
| 50 | + execSync(`(cd "${dir}" && yarn run ${BUILD_CMD})`); |
| 51 | + } else { |
| 52 | + execSync(` |
| 53 | + if [ ! -d "${dir}" ]; then |
| 54 | + mkdir -p "${dir}" && |
| 55 | + git archive "${hash}" | tar -xC "${dir}" && |
| 56 | + (cd "${dir}" && yarn install); |
| 57 | + fi && |
| 58 | + # Copy in local tests so the same logic applies to each revision. |
| 59 | + for file in $(cd "${LOCAL_DIR}src"; find . -path '*/__tests__/*.js'); |
| 60 | + do cp "${LOCAL_DIR}src/$file" "${dir}/src/$file"; |
| 61 | + done && |
| 62 | + (cd "${dir}" && yarn run ${BUILD_CMD}) |
| 63 | + `); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Find all benchmark tests to be run. |
| 68 | +function findBenchmarks() { |
| 69 | + const out = execSync( |
| 70 | + `(cd ${LOCAL_DIR}src; find . -path '*/__tests__/*-benchmark.js')`, |
| 71 | + {encoding: 'utf8'} |
| 72 | + ); |
| 73 | + return out.split('\n').filter(Boolean); |
| 74 | +} |
| 75 | + |
| 76 | +// Run a given benchmark test with the provided revisions. |
| 77 | +function runBenchmark(benchmark, revisions) { |
| 78 | + const modules = revisions.map(revision => |
| 79 | + require(path.join(dirForHash(hashForRevision(revision)), 'dist', benchmark)) |
| 80 | + ); |
| 81 | + const suite = new Suite(modules[0].name, { |
| 82 | + onStart(event) { |
| 83 | + console.log('⏱️ ' + event.currentTarget.name); |
| 84 | + }, |
| 85 | + onCycle(event) { |
| 86 | + beautifyBenchmark.add(event.target); |
| 87 | + }, |
| 88 | + onComplete() { |
| 89 | + beautifyBenchmark.log(); |
| 90 | + }, |
| 91 | + }); |
| 92 | + for (let i = 0; i < revisions.length; i++) { |
| 93 | + suite.add(revisions[i], modules[i].measure); |
| 94 | + } |
| 95 | + suite.run(); |
| 96 | +} |
| 97 | + |
| 98 | +// Prepare all revisions and run benchmarks matching a pattern against them. |
| 99 | +function prepareAndRunBenchmarks(benchmarkPatterns, revisions) { |
| 100 | + const benchmarks = findBenchmarks().filter(benchmark => |
| 101 | + benchmarkPatterns.length === 0 || |
| 102 | + benchmarkPatterns.some(pattern => benchmark.indexOf(pattern) !== -1) |
| 103 | + ); |
| 104 | + if (benchmarks.length === 0) { |
| 105 | + console.warn(`No benchmarks matching: \u001b[1m${benchmarkPatterns.join('\u001b[0m or \u001b[1m')}\u001b[0m`); |
| 106 | + return; |
| 107 | + } |
| 108 | + revisions.forEach(revision => { |
| 109 | + console.log(`🍳 Preparing ${revision}...`); |
| 110 | + prepareRevision(revision); |
| 111 | + }); |
| 112 | + benchmarks.forEach(benchmark => |
| 113 | + runBenchmark(benchmark, revisions) |
| 114 | + ); |
| 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(`Assuming you meant: \u001b[1mbenchmark ${assumeArgs.join(' ')}\u001b[0m`); |
| 138 | + } |
| 139 | + return {benchmarkPatterns, revisions}; |
| 140 | +} |
0 commit comments