Skip to content

Commit

Permalink
Merge pull request #27 from danielo515/feature/benchmarks
Browse files Browse the repository at this point in the history
chore(perf): added benchmarks for detect regresions
  • Loading branch information
danielo515 committed May 26, 2018
2 parents ff5ee6e + cff1e2a commit 07782bb
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ dist
node_modules
coverage
*.log
package-lock.json
7 changes: 5 additions & 2 deletions .travis.yml
@@ -1,13 +1,16 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "8"
- "9"
script:
- npm run lint:test
- 'bash bin/run_report.sh'
cache:
yarn: true
directories:
- node_modules
after_success: 'npm run coveralls'
after_success:
- 'npm run coveralls'
- 'bash bin/commit.sh'
10 changes: 10 additions & 0 deletions __benchmarks__/fixtures/small.js
@@ -0,0 +1,10 @@
module.exports = {
a: {
b: {
c: {
a: `This is a`,
b: `This is b`
}
}
}
};
9 changes: 9 additions & 0 deletions __benchmarks__/report-console/index.js
@@ -0,0 +1,9 @@
const Suite = require(`benchmarked`);
const suite = new Suite({
cwd: __dirname, // optionally define a base directory for code and fixtures
fixtures: `../fixtures/*.js`, // path or glob pattern to fixtures
code: `src/*.js`, // path or glob pattern to code files
});

// run the benchmarks
suite.run();
10 changes: 10 additions & 0 deletions __benchmarks__/report-console/src/basic.js
@@ -0,0 +1,10 @@
'use strict';

const OT = require(`../../../src`);

const map = {
a: `a.b.c.a`,
b: `a.b.c.b`
};

module.exports = OT(map);
22 changes: 22 additions & 0 deletions __benchmarks__/report-console/src/defaults.js
@@ -0,0 +1,22 @@
'use strict';

const OT = require(`../../../src`);

const map = {
a: `a.b.c.a`,
b: `a.b.c.b`,
x: `a.b.c.x`
};

const defaults = {
a: {
b: {
c: {
x: `X not defined`
}
}
}
};


module.exports = OT(map, defaults);
7 changes: 7 additions & 0 deletions __benchmarks__/report-json/index.js
@@ -0,0 +1,7 @@
'use strict';

const Runner = require(`benchr`);
const reporter = require(`./reporter`);
const runOptions = {progress: false, reporter, verbose: false, maxTime: 5, minTime: 0, delay: 0};
// The json reporter uses console.log to report, so we just cheat here
new Runner(runOptions, `${__dirname}/src/_basic.js`).run();
49 changes: 49 additions & 0 deletions __benchmarks__/report-json/reporter.js
@@ -0,0 +1,49 @@
const {platform} = require(`benchmark`);
const nVersion = `N${platform.version}`;
const {version} = require(`../../package`);
const report = require(`../report`);
const {promisify} = require(`util`);
const {writeFile: _writeFile} = require(`fs`);
const {join} = require(`path`);
const writeFile = promisify(_writeFile);
const curDir = x => join(__dirname, x);

module.exports = runner => {

// Handle runner events.
let currentFile, currentSuite;
const stats = {platform, files: []};
runner.on(`run.start`, () => {
stats.started = new Date();
}).on(`file.start`, file => {
currentFile = {
name: file,
suites: [],
};
stats.files.push(currentFile);
}).on(`suite.start`, suite => {
currentSuite = {
name: suite.name,
benchmarks: [],
};
currentFile.suites.push(currentSuite);
}).on(`suite.complete`, suite => {

// Add the name(s) of the fastest benchmark(s).
currentSuite.fastest = suite.filter(`fastest`).map(`name`);
// Handle benchmark statistics.
currentSuite.benchmarks = suite.map(bench => {
delete bench.stats.sample;
return {
name: bench.name,
hz: bench.hz,
aborted: bench.aborted,
stats: bench.stats,
};
});
}).on(`run.complete`, () => {
stats.ended = new Date();
report[nVersion] = Object.assign({}, report[nVersion], {[version]: stats});
writeFile(curDir(`../report.json`), JSON.stringify(report, null, 2), `utf8`).then(console.log(`Benchmark Finished`));
});
};
34 changes: 34 additions & 0 deletions __benchmarks__/report-json/src/_basic.js
@@ -0,0 +1,34 @@
'use strict';
const small = require(`../../fixtures/small`);
const OT = require(`../../../src`);

const map = {
a: `a.b.c.a`,
b: `a.b.c.b`
};

const triggerDefaults = Object.assign({}, map, {x: `a.b.c.x`});

const defaults = {
a: {
b: {
c: {
x: `X not defined`
}
}
}
};

const basic = OT(map);
const withDefaults = OT(triggerDefaults, defaults);

module.exports = (suite, benchmark) => {

suite(`4 nest levels`, () => {

benchmark(`Basic converter`, () => basic(small));
benchmark(`Using defaults`, () => withDefaults(small));

});
};

64 changes: 64 additions & 0 deletions __benchmarks__/report.json
@@ -0,0 +1,64 @@
{
"N8.9.4": {
"1.0.0": {
"platform": {
"description": "Node.js 8.9.4 on Win32 64-bit",
"layout": null,
"manufacturer": null,
"name": "Node.js",
"prerelease": null,
"product": null,
"ua": null,
"version": "8.9.4",
"os": {
"architecture": 64,
"family": "Win32",
"version": null
}
},
"files": [
{
"name": "C:\\Users\\rdani\\Dropbox\\programacion\\node\\object-translate\\__benchmarks__\\report-json/src/_basic.js",
"suites": [
{
"name": "4 nest levels",
"benchmarks": [
{
"name": "Basic converter",
"hz": 165640.18594310517,
"aborted": false,
"stats": {
"moe": 1.4346621272262202e-7,
"rme": 2.3763770151928187,
"sem": 7.319704730746022e-8,
"deviation": 6.944081624720084e-7,
"mean": 0.0000060371823075801454,
"variance": 4.822026961077513e-13
}
},
{
"name": "Using defaults",
"hz": 103028.45682340284,
"aborted": false,
"stats": {
"moe": 2.2288888017834302e-7,
"rme": 2.296389736787102,
"sem": 1.1371881641752195e-7,
"deviation": 0.00000112000045228473,
"mean": 0.000009706056276413632,
"variance": 1.2544010131179996e-12
}
}
],
"fastest": [
"Basic converter"
]
}
]
}
],
"started": "2018-05-26T12:17:53.847Z",
"ended": "2018-05-26T12:18:07.051Z"
}
}
}
28 changes: 28 additions & 0 deletions bin/commit.sh
@@ -0,0 +1,28 @@
#!/bin/bash
node_version=$(node -v);

function msg() {
echo "travis-commit: $*"
}

function err() {
msg "$*" 1>&2
}

if [ ${node_version:1:1} -lt 8 ]; then
err "Nothing to commit on node 6"
exit 0
fi
git add __benchmarks__/*
# make Travis CI skip this build
if ! git commit -m "Travis CI update [ci skip]"; then
err "failed to commit updates"
exit 1
fi

remote=https://$GITHUB_TOKEN@github.com/$TRAVIS_REPO_SLUG

if ! git push --follow-tags "$remote" "$TRAVIS_BRANCH" > /dev/null 2>&1; then
err "failed to push git changes to $remote $TRAVIS_BRANCH"
exit 1
fi
5 changes: 5 additions & 0 deletions bin/run_report.sh
@@ -0,0 +1,5 @@
#!/bin/bash
node_version=$(node -v);
if [ ${node_version:1:1} -gt 6 ]; then
npm run benchmark:report
fi
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -37,6 +37,8 @@
"build:es": "rimraf es && cross-env BABEL_ENV=es babel src --out-dir es",
"build:cjs": "rimraf cjs && cross-env BABEL_ENV=cjs babel src --out-dir cjs",
"build:umd": "rimraf dist && cross-env BABEL_ENV=es rollup -c & cross-env BABEL_ENV=es NODE_ENV=production rollup -c",
"benchmark:console": "node __benchmarks__/report-console",
"benchmark:report": "node __benchmarks__/report-json",
"cm": "git-cz"
},
"keywords": [],
Expand All @@ -48,12 +50,14 @@
"babel-core": "^6.17.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.16.0",
"benchmarked": "^2.0.0",
"benchr": "^4.3.0",
"commitizen": "^2.9.6",
"conventional-recommended-bump": "^1.0.0",
"coveralls": "^3.0.0",
"cross-env": "^5.1.3",
"cz-conventional-changelog": "^2.0.0",
"eslint": "^4.7.1",
"eslint": "^4.19.1",
"eslint-config-devine": "^1.6.1",
"eslint-plugin-babel": "^4.0.0",
"husky": "^0.14.3",
Expand All @@ -63,9 +67,9 @@
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-commonjs": "^8.2.1",
"rollup-plugin-filesize": "^1.4.2",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-uglify": "^3.0.0",
"rollup-plugin-filesize": "^1.4.2",
"standard-changelog": "^1.0.1"
},
"dependencies": {
Expand Down

0 comments on commit 07782bb

Please sign in to comment.