Skip to content

Commit 40e1388

Browse files
committed
Add MVP of mocha-like benchmark tests
Based on graphql#1163 by @mohawk2
1 parent 358df97 commit 40e1388

File tree

8 files changed

+192
-2
lines changed

8 files changed

+192
-2
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.*/dist/.*
44
.*/coverage/.*
55
.*/resources/.*
6+
.*/benchmark/.*
67

78
[include]
89

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ node_modules
99
coverage
1010
dist
1111
npm
12+
benchmark

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ resources
1717
src
1818
dist
1919
npm
20+
benchmark

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"prettier": "prettier --write 'src/**/*.js'",
3030
"check": "flow check",
3131
"check-cover": "for file in {src/*.js,src/**/*.js}; do echo $file; flow coverage $file; done",
32+
"benchmark": "node resources/benchmark.js",
3233
"build": "npm run build:clean && npm run build:npm && npm run build:npm-flow && npm run build:module && npm run build:module-flow && npm run build:package-json",
3334
"build:clean": "rm -rf ./dist",
3435
"build:package-json": "node ./resources/copy-package-json.js",
@@ -56,10 +57,13 @@
5657
"babel-plugin-transform-flow-strip-types": "6.22.0",
5758
"babel-plugin-transform-object-rest-spread": "6.23.0",
5859
"babel-preset-env": "^1.5.2",
60+
"beautify-benchmark": "^0.2.4",
61+
"benchmark": "^2.1.4",
5962
"chai": "4.1.1",
6063
"chai-json-equal": "0.0.1",
6164
"chai-spies-next": "^0.8.0",
6265
"chai-subset": "1.5.0",
66+
"chalk": "^2.3.0",
6367
"coveralls": "2.13.1",
6468
"eslint": "4.4.1",
6569
"eslint-plugin-babel": "4.1.2",
@@ -69,6 +73,7 @@
6973
"isparta": "4.0.0",
7074
"mocha": "3.5.0",
7175
"prettier": "^1.9.2",
72-
"sane": "2.0.0"
76+
"sane": "2.0.0",
77+
"shelljs": "^0.7.8"
7378
}
7479
}

resources/benchmark.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const benchmark = require('benchmark');
2+
const beautifyBenchmark = require('beautify-benchmark');
3+
const sh = require('shelljs');
4+
const chalk = require('chalk');
5+
const pathJoin = require('path').join;
6+
7+
const args = process.argv.slice(2);
8+
args[0] = args[0] || 'HEAD';
9+
args[1] = args[1] || 'local';
10+
11+
console.log('Benchmarking revisions: ' + args.join(', '));
12+
13+
const localDistDir = './benchmark/local';
14+
sh.rm('-rf', localDistDir);
15+
console.log(`Building local dist: ${localDistDir}`);
16+
sh.mkdir('-p', localDistDir);
17+
exec(`babel src --optional runtime --copy-files --out-dir ${localDistDir}`);
18+
19+
const revisions = {};
20+
for (const arg of args) {
21+
revisions[arg] = reqireFromCWD(buildRevisionDist(arg));
22+
}
23+
24+
global.suite = suite;
25+
const testFiles = sh.ls(`${localDistDir}/**/__tests__/**/*-benchmark.js`);
26+
for (const file of testFiles) {
27+
reqireFromCWD(file);
28+
}
29+
30+
function reqireFromCWD(path) {
31+
return require(pathJoin(process.cwd(), path))
32+
}
33+
34+
function suite(name, fn) {
35+
console.log(chalk.green(name) + '\n');
36+
37+
const measures = {};
38+
for (const [revision, graphql] of Object.entries(revisions)) {
39+
currentRevision = revision;
40+
global.measure = (name, fn) => {
41+
measures[name] = measures[name] || new benchmark.Suite();
42+
measures[name].add(revision, fn);
43+
};
44+
try {
45+
fn(graphql);
46+
} catch (e) {
47+
console.error(e.stack);
48+
}
49+
}
50+
global.measure = undefined;
51+
52+
for (const [name, suite] of Object.entries(measures)) {
53+
console.log(' ⏱️ ', name);
54+
suite
55+
.on('cycle', function(event) {
56+
beautifyBenchmark.add(event.target);
57+
})
58+
.on('complete', function() {
59+
beautifyBenchmark.log();
60+
})
61+
.run();
62+
}
63+
}
64+
65+
function exec(command) {
66+
const {code, stdout, stderr} = sh.exec(command, {silent: true});
67+
if (code !== 0) {
68+
console.error(stdout);
69+
console.error(stderr);
70+
sh.exit(code);
71+
}
72+
return stdout.trim();
73+
}
74+
75+
function buildRevisionDist(revision) {
76+
if (revision === 'local') {
77+
return localDistDir;
78+
}
79+
80+
const hash = exec(`git log -1 --format=%h "${revision}"`);
81+
const buildDir = './benchmark/' + hash;
82+
const distDir = buildDir + '/dist'
83+
84+
if (sh.test('-d', buildDir)) {
85+
return distDir;
86+
}
87+
console.log(`Building "${revision}"(${hash}) revision: ${buildDir}`);
88+
sh.mkdir('-p', buildDir);
89+
exec(`git archive "${hash}" | tar -xC "${buildDir}"`);
90+
91+
const pwd = sh.pwd();
92+
sh.cd(buildDir);
93+
exec('yarn && npm run build');
94+
sh.cd(pwd);
95+
return buildDir + '/dist';
96+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
import { readFileSync } from 'fs';
9+
import { join } from 'path';
10+
import { getIntrospectionQuery } from '../../utilities/introspectionQuery';
11+
/* global suite, measure */
12+
13+
suite('Parse string to AST', graphql => {
14+
const kitchenSink = readFileSync(join(__dirname, './kitchen-sink.graphql'), {
15+
encoding: 'utf8',
16+
});
17+
measure('Kitchen Sink', () => graphql.parse(kitchenSink));
18+
19+
const introspectionQuery = getIntrospectionQuery();
20+
measure('Introspection Query', () => graphql.parse(introspectionQuery));
21+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
import { readFileSync } from 'fs';
9+
import { join } from 'path';
10+
import { getIntrospectionQuery } from '../../utilities/introspectionQuery';
11+
/* global suite, measure */
12+
13+
suite('Print AST', graphql => {
14+
const kitchenSink = readFileSync(join(__dirname, './kitchen-sink.graphql'), {
15+
encoding: 'utf8',
16+
});
17+
const kitchenSinkAST = graphql.parse(kitchenSink);
18+
measure('Kitchen Sink', () => graphql.print(kitchenSinkAST));
19+
20+
const introspectionQueryAST = graphql.parse(getIntrospectionQuery());
21+
measure('Introspection Query', () => graphql.print(introspectionQueryAST));
22+
});

yarn.lock

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,17 @@ bcrypt-pbkdf@^1.0.0:
715715
dependencies:
716716
tweetnacl "^0.14.3"
717717

718+
beautify-benchmark@^0.2.4:
719+
version "0.2.4"
720+
resolved "https://registry.yarnpkg.com/beautify-benchmark/-/beautify-benchmark-0.2.4.tgz#3151def14c1a2e0d07ff2e476861c7ed0e1ae39b"
721+
722+
benchmark@^2.1.4:
723+
version "2.1.4"
724+
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
725+
dependencies:
726+
lodash "^4.17.4"
727+
platform "^1.3.3"
728+
718729
binary-extensions@^1.0.0:
719730
version "1.11.0"
720731
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.11.0.tgz#46aa1751fb6a2f93ee5e689bb1087d4b14c6c205"
@@ -829,7 +840,7 @@ chalk@^1.1.1, chalk@^1.1.3:
829840
strip-ansi "^3.0.0"
830841
supports-color "^2.0.0"
831842

832-
chalk@^2.0.0, chalk@^2.1.0:
843+
chalk@^2.0.0, chalk@^2.1.0, chalk@^2.3.0:
833844
version "2.3.0"
834845
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
835846
dependencies:
@@ -1592,6 +1603,10 @@ inquirer@^3.0.6:
15921603
strip-ansi "^4.0.0"
15931604
through "^2.3.6"
15941605

1606+
interpret@^1.0.0:
1607+
version "1.1.0"
1608+
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"
1609+
15951610
invariant@^2.2.2:
15961611
version "2.2.2"
15971612
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360"
@@ -2184,6 +2199,10 @@ path-is-inside@^1.0.1, path-is-inside@^1.0.2:
21842199
version "1.0.2"
21852200
resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53"
21862201

2202+
path-parse@^1.0.5:
2203+
version "1.0.5"
2204+
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
2205+
21872206
pathval@^1.0.0:
21882207
version "1.1.0"
21892208
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
@@ -2206,6 +2225,10 @@ pinkie@^2.0.0:
22062225
version "2.0.4"
22072226
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
22082227

2228+
platform@^1.3.3:
2229+
version "1.3.4"
2230+
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.4.tgz#6f0fb17edaaa48f21442b3a975c063130f1c3ebd"
2231+
22092232
pluralize@^4.0.0:
22102233
version "4.0.0"
22112234
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-4.0.0.tgz#59b708c1c0190a2f692f1c7618c446b052fd1762"
@@ -2287,6 +2310,12 @@ readdirp@^2.0.0:
22872310
readable-stream "^2.0.2"
22882311
set-immediate-shim "^1.0.1"
22892312

2313+
rechoir@^0.6.2:
2314+
version "0.6.2"
2315+
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
2316+
dependencies:
2317+
resolve "^1.1.6"
2318+
22902319
regenerate@^1.2.1:
22912320
version "1.3.3"
22922321
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f"
@@ -2416,6 +2445,12 @@ [email protected]:
24162445
version "1.1.7"
24172446
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
24182447

2448+
resolve@^1.1.6:
2449+
version "1.5.0"
2450+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.5.0.tgz#1f09acce796c9a762579f31b2c1cc4c3cddf9f36"
2451+
dependencies:
2452+
path-parse "^1.0.5"
2453+
24192454
restore-cursor@^2.0.0:
24202455
version "2.0.0"
24212456
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
@@ -2491,6 +2526,14 @@ shebang-regex@^1.0.0:
24912526
version "1.0.0"
24922527
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
24932528

2529+
shelljs@^0.7.8:
2530+
version "0.7.8"
2531+
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.8.tgz#decbcf874b0d1e5fb72e14b164a9683048e9acb3"
2532+
dependencies:
2533+
glob "^7.0.0"
2534+
interpret "^1.0.0"
2535+
rechoir "^0.6.2"
2536+
24942537
signal-exit@^3.0.0, signal-exit@^3.0.2:
24952538
version "3.0.2"
24962539
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

0 commit comments

Comments
 (0)