-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MVP of mocha-like benchmark tests
- Loading branch information
1 parent
b333b30
commit 8e2c7f1
Showing
9 changed files
with
398 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.*/dist/.* | ||
.*/coverage/.* | ||
.*/resources/.* | ||
.*/benchmark/.* | ||
|
||
[include] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ node_modules | |
coverage | ||
dist | ||
npm | ||
benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ resources | |
src | ||
dist | ||
npm | ||
benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const benchmark = require('benchmark'); | ||
const beautifyBenchmark = require('beautify-benchmark'); | ||
const sh = require('shelljs'); | ||
const chalk = require('chalk'); | ||
const pathJoin = require('path').join; | ||
|
||
const args = process.argv.slice(2); | ||
args[0] = args[0] || 'HEAD'; | ||
args[1] = args[1] || 'local'; | ||
|
||
console.log('Benchmarking revisions: ' + args.join(', ')); | ||
|
||
const localDistDir = './benchmark/local'; | ||
sh.rm('-rf', localDistDir); | ||
console.log(`Building local dist: ${localDistDir}`); | ||
sh.mkdir('-p', localDistDir); | ||
exec(`babel src --optional runtime --copy-files --out-dir ${localDistDir}`); | ||
|
||
const revisions = {}; | ||
for (const arg of args) { | ||
const distPath = buildRevisionDist(arg); | ||
const distRequire = (path) => reqireFromCWD(pathJoin(distPath, path)); | ||
revisions[arg] = distRequire; | ||
} | ||
|
||
const suites = {}; | ||
global.suite = suite; | ||
const testFiles = sh.ls(`${localDistDir}/**/__tests__/**/*-benchmark.js`); | ||
for (const file of testFiles) { | ||
reqireFromCWD(file); | ||
} | ||
|
||
dummyRun(); | ||
for (const [name, measures] of Object.entries(suites)) { | ||
console.log(chalk.green(name) + '\n'); | ||
benchmark.invoke(measures, 'run'); | ||
} | ||
|
||
function reqireFromCWD(path) { | ||
return require(pathJoin(process.cwd(), path)) | ||
} | ||
|
||
function dummyRun() { | ||
(new benchmark.Suite('dummy')) | ||
.add('dummy', () => { Math.pow(2, 256); }) | ||
.run(); | ||
} | ||
|
||
function newMeasurement(name) { | ||
return new benchmark.Suite(name, { | ||
onStart(event) { | ||
console.log(' ⏱️ ', event.currentTarget.name); | ||
}, | ||
onCycle(event) { | ||
beautifyBenchmark.add(event.target); | ||
}, | ||
onComplete() { | ||
beautifyBenchmark.log(); | ||
}, | ||
}); | ||
} | ||
|
||
function suite(name, fn) { | ||
const measures = {}; | ||
for (const [revision, distRequire] of Object.entries(revisions)) { | ||
currentRevision = revision; | ||
global.measure = (name, fn) => { | ||
measures[name] = measures[name] || newMeasurement(name); | ||
measures[name].add(revision, fn); | ||
}; | ||
try { | ||
fn(distRequire); | ||
} catch (e) { | ||
console.error(e.stack); | ||
} | ||
} | ||
global.measure = undefined; | ||
suites[name] = Object.values(measures); | ||
} | ||
|
||
function exec(command) { | ||
const {code, stdout, stderr} = sh.exec(command, {silent: true}); | ||
if (code !== 0) { | ||
console.error(stdout); | ||
console.error(stderr); | ||
sh.exit(code); | ||
} | ||
return stdout.trim(); | ||
} | ||
|
||
function buildRevisionDist(revision) { | ||
if (revision === 'local') { | ||
return localDistDir; | ||
} | ||
|
||
const hash = exec(`git log -1 --format=%h "${revision}"`); | ||
const buildDir = './benchmark/' + hash; | ||
const distDir = buildDir + '/dist' | ||
|
||
if (sh.test('-d', buildDir)) { | ||
return distDir; | ||
} | ||
console.log(`Building "${revision}"(${hash}) revision: ${buildDir}`); | ||
sh.mkdir('-p', buildDir); | ||
exec(`git archive "${hash}" | tar -xC "${buildDir}"`); | ||
|
||
const pwd = sh.pwd(); | ||
sh.cd(buildDir); | ||
exec('yarn && npm run build'); | ||
sh.cd(pwd); | ||
return buildDir + '/dist'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { getIntrospectionQuery } from '../../utilities/introspectionQuery'; | ||
/* global suite, measure */ | ||
|
||
function readFile(path) { | ||
const fullPath = join(__dirname, path); | ||
return readFileSync(fullPath, { encoding: 'utf8' }); | ||
} | ||
|
||
const introspectionQuery = getIntrospectionQuery(); | ||
const kitchenSink = readFile('./kitchen-sink.graphql'); | ||
const schemaKitchenSink = readFile('./schema-kitchen-sink.graphql'); | ||
|
||
suite('Run lexer on a string', distRequire => { | ||
const { Source } = distRequire('language/source'); | ||
const { createLexer } = distRequire('language/lexer'); | ||
|
||
function runLexer(source) { | ||
const lexer = createLexer(source); | ||
let token; | ||
do { | ||
token = lexer.advance(); | ||
} while (token.kind !== '<EOF>'); | ||
} | ||
|
||
measure('Introspection Query', () => { | ||
runLexer(new Source(introspectionQuery)); | ||
}); | ||
|
||
measure('Kitchen Sink', () => { | ||
runLexer(new Source(kitchenSink)); | ||
}); | ||
|
||
measure('Schema Kitchen Sink', () => { | ||
runLexer(new Source(schemaKitchenSink)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { getIntrospectionQuery } from '../../utilities/introspectionQuery'; | ||
/* global suite, measure */ | ||
|
||
function readFile(path) { | ||
const fullPath = join(__dirname, path); | ||
return readFileSync(fullPath, { encoding: 'utf8' }); | ||
} | ||
|
||
const introspectionQuery = getIntrospectionQuery(); | ||
const kitchenSink = readFile('./kitchen-sink.graphql'); | ||
const schemaKitchenSink = readFile('./schema-kitchen-sink.graphql'); | ||
|
||
suite('Parse string to AST', distRequire => { | ||
const { parse } = distRequire('language/parser'); | ||
|
||
measure('Introspection Query', () => parse(introspectionQuery)); | ||
measure('Kitchen Sink', () => parse(kitchenSink)); | ||
measure('Schema Kitchen Sink', () => parse(schemaKitchenSink)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { getIntrospectionQuery } from '../../utilities/introspectionQuery'; | ||
/* global suite, measure */ | ||
|
||
suite('Print AST', distRequire => { | ||
const { print } = distRequire('language/printer'); | ||
const { parse } = distRequire('language/parser'); | ||
|
||
const kitchenSink = readFileSync(join(__dirname, './kitchen-sink.graphql'), { | ||
encoding: 'utf8', | ||
}); | ||
const kitchenSinkAST = parse(kitchenSink); | ||
measure('Kitchen Sink', () => print(kitchenSinkAST)); | ||
|
||
const introspectionQueryAST = parse(getIntrospectionQuery()); | ||
measure('Introspection Query', () => print(introspectionQueryAST)); | ||
}); |
Oops, something went wrong.