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