Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adds benchmarking support to Jest [WIP] #3026

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/benchmark/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": ["transform-async-to-generator"]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a newline!

23 changes: 23 additions & 0 deletions examples/benchmark/Fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All new files need the FB copyright header.

export function loop(num) {
let a = 1;
let b = 0;
let temp;

while (num >= 0) {
temp = a;
a += b;
b = temp;
num--;
}

return b;
}

export function recursive(num) {
if (num <= 1) {
return 1;
}

return recursive(num - 1) + recursive(num - 2);
}
18 changes: 18 additions & 0 deletions examples/benchmark/__benches__/Fibonacci-bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
recursive,
loop,
} from '../Fibonacci';

benchmark('fastest Fibonacci implementation', () => {
let amount;
beforeEach(() => {
amount = 10;
});
scenario('looping (10 times)', () => {
loop(amount);
});
scenario('recursive (10 times)', () => {
recursive(amount);
});
});

18 changes: 18 additions & 0 deletions examples/benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dependencies": {
"react": "~15.3.1",
"react-dom": "~15.3.1"
},
"devDependencies": {
"babel-jest": "*",
"babel-polyfill": "*",
"babel-preset-es2015": "*",
"babel-preset-react": "*",
"enzyme": "~2.4.1",
"jest": "*",
"react-addons-test-utils": "15.3.2"
},
"scripts": {
"test": "jest"
}
}
19 changes: 19 additions & 0 deletions packages/jest-benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "jest-benchmark",
"version": "19.0.2",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"main": "build/index.js",
"dependencies": {
"benchmark": "^2.1.3",
"chalk": "^1.1.3",
"graceful-fs": "^4.1.6",
"jest-matcher-utils": "^19.0.2",
"jest-matchers": "^19.0.2",
"jest-message-util": "^19.0.2",
"jest-snapshot": "^19.0.2"
}
}
125 changes: 125 additions & 0 deletions packages/jest-benchmark/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';

import type {Config} from 'types/Config';
import type {Environment} from 'types/Environment';
import type Runtime from 'jest-runtime';

const Benchmark = require('benchmark');
const {startBenchmark} = require('./reporter');

function createBenchmark(name, scenarios, beforeEach, afterEach) {
return {
afterEach,
beforeEach,
name,
scenarios,
};
}

function createScenario(name, func) {
return {
func,
name,
};
}

function noOp() {
console.warn('No-op');
}

async function runBenchmark(
benchPath,
{afterEach, beforeEach, scenarios, name},
) {
const suite = new Benchmark.Suite();

for (let i = 0; i < scenarios.length; i++) {
const {func, name} = scenarios[i];

suite.add(name, {
defer: true,
fn: deferred => {
const returnVal = func();

if (returnVal && returnVal.then) {
returnVal.then(() => {
deferred.resolve();
}).catch(err => {
throw err;
});
} else {
deferred.resolve();
}
},
});
}
const endBenchmark = await startBenchmark(benchPath, name);
return await new Promise((resolve, reject) => {
if (beforeEach) {
beforeEach();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this means that beforeEach/afterEach can't be async? It would be good to support that!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll add support for async beforeEach/afterEach :)

}
suite.on('complete', function() {
if (afterEach) {
afterEach();
}
endBenchmark(this).then(resolve).catch(reject);
}).run({'async': false});
});
}

async function jestBenchmark(
config: Config,
environment: Environment,
runtime: Runtime,
benchPath: string,
) {
const benchmarks = [];

try {
environment.global.benchmark = (benchName, scenariosFunc) => {
let beforeEach;
let afterEach;
const scenarios = [];

environment.global.beforeEach = beforeEachFunc => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we are ok having only a single beforeEach function per benchmark file, right? In Jasmine you can have many different ones that are grouped with "describe".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true but I wanted us to avoid doing that here ideally. What are your thoughts on it? If there's demand for it I guess we could add it?

beforeEach = beforeEachFunc;
};

environment.global.afterEach = afterEachFunc => {
afterEach = afterEachFunc;
};

environment.global.scenario = (scenarioName, benchFunc) => {
scenarios.push(createScenario(scenarioName, benchFunc));
};

scenariosFunc();
benchmarks.push(
createBenchmark(benchName, scenarios, beforeEach, afterEach)
);
};
const results = [];

environment.global.scenario = noOp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason for stubbing these out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If people call scenario() and they're not in a benchmark block it could potentially cause undesirable side-effects, I'd rather just explicitly stop that from ever becoming an issue.

runtime.requireModule(benchPath);
environment.global.scenario = noOp;

for (let i = 0; i < benchmarks.length; i++) {
results.push(await runBenchmark(benchPath, benchmarks[i]));
}
return results;
} catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "error" :)

throw err;
}
}

module.exports = jestBenchmark;
95 changes: 95 additions & 0 deletions packages/jest-benchmark/src/reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';

const path = require('path');
const chalk = require('chalk');
const readline = require('readline');

const RUNNING_TEXT = ' BENCHMARK ';
const RUNNING = chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';

const COMPLETE_TEXT = ' BENCHMARK ';
const COMPLETE = chalk.reset.inverse.green.bold(COMPLETE_TEXT) + ' ';

const SUCCESS = chalk.green('✓');
const ERROR = chalk.red('✗');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind checking what Jest uses already? I recall that we use different ones on windows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, I wasn't sure here – it's not the easiest thing to regex search for. Can you link me to the source?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const SUCCESS_TEXT = chalk.reset.inverse.green.bold(' FASTEST ');
const ERROR_TEXT = chalk.reset.inverse.red.bold(' ERROR ');

function roundToPrecision(number, precision) {
const factor = Math.pow(10, precision);
const tempNumber = number * factor;
const roundedTempNumber = Math.round(tempNumber);

return roundedTempNumber / factor;
};

function print(message: string) {
return new Promise(resolve => setImmediate(() => {
process.stdout.write(message);
resolve();
}));
}

function clear() {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
}

function colorFilePath(filePath) {
return chalk.gray(
path.dirname(filePath) + '/'
) + chalk.white.bold(
path.basename(filePath)
);
}

async function startBenchmark(filePath: string, benchmarkName: string) {
await print(RUNNING + colorFilePath(filePath));

return async function endBenchmark(results) {
clear();
const fastestResult = results.filter('fastest')[0];
let text = COMPLETE + colorFilePath(filePath) + '\n';
let hasError = false;

text += ` ${chalk.white.bold(benchmarkName)}:\n`;

for (let i = 0; i < results.length; i++) {
const {name, hz, cycles, stats, error} = results[i];

if (error) {
hasError = true;

text += ` ${ERROR} ${chalk.white(name)} ${ERROR_TEXT}`;
} else {
const ops = Math.floor(parseFloat(hz));
const margin = roundToPrecision(parseFloat(stats.rme), 2);
const detail = chalk.gray(
` [ ${ops} op/s (±${margin}%) ${ cycles } cycles ]`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy stuff

);

text += ` ${SUCCESS} ${chalk.white(name + detail)}`;
if (fastestResult === results[i]) {
text += ` ${SUCCESS_TEXT}`;
}
}
text += '\n';
}
text += '\n';
await print(text);
if (hasError) {
throw new Error('A benchmark scenario failed with an error!');
}
};
}

module.exports.startBenchmark = startBenchmark;
Loading