benchmarkify
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -12,3 +12,3 @@ "use strict"; | ||
let bench1 = benchmark.createSuite({ name: "Without promise", time: 5000 }); | ||
let bench1 = benchmark.createSuite("Without promise"); | ||
@@ -24,3 +24,3 @@ bench1.add("Sync", () => { | ||
let bench2 = benchmark.createSuite({ name: "ES6", time: 5000 }); | ||
let bench2 = benchmark.createSuite("ES6"); | ||
@@ -41,3 +41,3 @@ bench2.add("ES6 Promise.resolve", done => { | ||
let bench3 = benchmark.createSuite({ name: "Bluebird", time: 5000 }) | ||
let bench3 = benchmark.createSuite("Bluebird"); | ||
@@ -44,0 +44,0 @@ bench3.add("Bluebird Promise.resolve", done => { |
@@ -6,3 +6,3 @@ "use strict"; | ||
let bench1 = benchmark.createSuite({ name: "Date performance", time: 1000 }); | ||
let bench1 = benchmark.createSuite("Date performance", { time: 1000 }); | ||
@@ -30,8 +30,8 @@ const cycle = 10 * 1000; | ||
let bench2 = benchmark.createSuite({ name: "Increment integer", time: 1000 }); | ||
let bench2 = benchmark.createSuite("Increment integer", { time: 1000 }); | ||
const ITERATION = 1000; | ||
let i1 = 0; | ||
bench2.add("Increment with ++", () => { | ||
i1++; | ||
return i1; | ||
}); | ||
@@ -42,2 +42,3 @@ | ||
i2 += 1; | ||
return i2; | ||
}); | ||
@@ -44,0 +45,0 @@ |
@@ -8,19 +8,17 @@ "use strict"; | ||
benchmark.createSuite({ | ||
name: "String concatenate", | ||
time: 1000 | ||
}).add("Concat with '+'", () => { | ||
let s = ""; | ||
for(let i = 0; i < ITERATION; i++) | ||
s += "test" + i; | ||
return s; | ||
}).add("Concat with array & join", () => { | ||
let s = []; | ||
for(let i = 0; i < ITERATION; i++) | ||
s.push("test" + i); | ||
return s.join(); | ||
}).run().then(res => { | ||
console.log(JSON.stringify(res, null, 2)); | ||
}); | ||
benchmark.createSuite("String concatenate", { time: 1000 }) | ||
.add("Concat with '+'", () => { | ||
let s = ""; | ||
for(let i = 0; i < ITERATION; i++) | ||
s += "test" + i; | ||
return s; | ||
}) | ||
.ref("Concat with array & join", () => { | ||
let s = []; | ||
for(let i = 0; i < ITERATION; i++) | ||
s.push("test" + i); | ||
return s.join(); | ||
}) | ||
.run().then(res => { | ||
console.log(JSON.stringify(res, null, 2)); | ||
}); |
55
index.js
@@ -9,6 +9,7 @@ const _ = require("lodash"); | ||
/** | ||
* Formatting number | ||
* | ||
* | ||
* @param {any} value | ||
* @param {number} [decimals=0] | ||
* @param {any} value Number value | ||
* @param {number} [decimals=0] Count of decimals | ||
* @param {boolean} [sign=false] Put '+' sign if the number is positive | ||
* @returns | ||
@@ -24,4 +25,4 @@ */ | ||
/** | ||
* Test case class | ||
* | ||
* | ||
* @class TestCase | ||
@@ -32,7 +33,8 @@ */ | ||
* Creates an instance of TestCase. | ||
* @param {any} suite | ||
* @param {any} name | ||
* @param {any} fn | ||
* @param {any} opts | ||
* | ||
* @param {Suite} suite | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @param {Object} opts | ||
* | ||
* @memberOf TestCase | ||
@@ -63,3 +65,3 @@ */ | ||
rps: null | ||
} | ||
}; | ||
} | ||
@@ -189,3 +191,3 @@ | ||
*/ | ||
cyclingAsyncCb(resolve, reject) { | ||
cyclingAsyncCb(resolve) { | ||
const self = this; | ||
@@ -230,9 +232,11 @@ const fn = self.fn; | ||
* Creates an instance of Suite. | ||
* @param {any} parent | ||
* @param {any} opts | ||
* @param {Benchmarkify} parent | ||
* @param {String} name | ||
* @param {Object} opts | ||
* | ||
* @memberOf Suite | ||
*/ | ||
constructor(parent, opts) { | ||
constructor(parent, name, opts) { | ||
this.parent = parent; | ||
this.name = name; | ||
this.logger = this.parent.logger; | ||
@@ -246,3 +250,2 @@ this.onlyTest = null; | ||
_.assign(this, { | ||
name: "<Anonymous suite>", | ||
time: 5000, | ||
@@ -351,3 +354,3 @@ minSamples: 0 | ||
return new Promise((resolve, reject) => { | ||
return new Promise(resolve => { | ||
self.running = true; | ||
@@ -414,3 +417,3 @@ self.logger.log(chalk.magenta.bold(`Suite: ${self.name}`)); | ||
return printAndRun("fail", chalk.red("[ERR] " + test.name), err); | ||
}) | ||
}); | ||
} | ||
@@ -468,3 +471,3 @@ | ||
" ", | ||
pe(test.name + flag, maxTitleLength + 1), | ||
pe(test.name + flag, maxTitleLength + 5), | ||
ps(formatNumber(test.stat.percent, 2, true) + "%", 8), | ||
@@ -481,8 +484,11 @@ ps(" (" + formatNumber(test.stat.rps) + " rps)", 20), | ||
let item = { | ||
name: test.name, | ||
reference: test.reference | ||
} | ||
name: test.name | ||
}; | ||
if (test === fastest) | ||
item.fastest = true; | ||
if (test.reference) | ||
item.reference = true; | ||
if (test.error) | ||
@@ -521,3 +527,3 @@ item.error = test.error.toString(); | ||
this.spinner = ora({ | ||
text: 'Running benchmark...', | ||
text: "Running benchmark...", | ||
spinner: { | ||
@@ -577,2 +583,3 @@ interval: 400, | ||
* | ||
* @param {String} name | ||
* @param {any} opts | ||
@@ -583,4 +590,4 @@ * @returns | ||
*/ | ||
createSuite(opts) { | ||
const suite = new Suite(this, opts); | ||
createSuite(name, opts) { | ||
const suite = new Suite(this, name, opts); | ||
this.suites.push(suite); | ||
@@ -626,3 +633,3 @@ return suite; | ||
elapsedMs: Date.now() - start | ||
} | ||
}; | ||
}); | ||
@@ -629,0 +636,0 @@ } |
{ | ||
"name": "benchmarkify", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Benchmark runner for NodeJS", | ||
@@ -20,2 +20,3 @@ "main": "index.js", | ||
"license": "MIT", | ||
"reveal": true, | ||
"dependencies": { | ||
@@ -27,3 +28,6 @@ "bluebird": "3.5.0", | ||
"tiny-human-time": "1.2.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "3.19.0" | ||
} | ||
} |
150
README.md
@@ -1,2 +0,148 @@ | ||
# benchmarkify | ||
Benchmark runner for NodeJS | ||
# :zap: benchmarkify | ||
Benchmark framework for NodeJS for measure the execution time of JS codes. | ||
# Installation | ||
``` | ||
$ npm install benchmarkify --save-dev | ||
``` | ||
# Usage | ||
**Example benchmark suite** | ||
```js | ||
let Benchmarkify = require("benchmarkify"); | ||
// Create a new benchmark | ||
// The `.printHeader` method will print the name of benchmark & some | ||
// information from the OS/PC to the console. | ||
let benchmark = new Benchmarkify("Simple example").printHeader(); | ||
let i = 0; | ||
// Create a test suite | ||
let bench1 = benchmark.createSuite("Increment integer"); | ||
// Add first func | ||
bench1.add("Increment with ++", () => { | ||
i++; | ||
}); | ||
// Add second func. This result will be the reference | ||
bench1.ref("Increment with i + 1", () => { | ||
i = i + 1; | ||
}); | ||
bench1.run(); | ||
``` | ||
**Output** | ||
``` | ||
================== | ||
Simple example | ||
================== | ||
Platform info: | ||
============== | ||
Windows_NT 6.1.7601 x64 | ||
Node.JS: 6.10.0 | ||
V8: 5.1.281.93 | ||
Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz × 8 | ||
Suite: Increment integer | ||
√ Increment with ++ 98,878,885 rps | ||
√ Increment with i + 1 89,930,539 rps | ||
Increment with ++ +9.95% (98,878,885 rps) (avg: 10ns) | ||
Increment with i + 1 (#) 0% (89,930,539 rps) (avg: 11ns) | ||
----------------------------------------------------------------------- | ||
``` | ||
**JSON result** | ||
If you need the results in JSON use `.then` after `run()` | ||
```js | ||
bench1.run().then(res => console.log(res)); | ||
``` | ||
Result on console: | ||
```js | ||
[ | ||
{ | ||
name: 'Increment with ++', | ||
fastest: true, | ||
stat: { | ||
duration: 4.999651845, | ||
cycle: 492086, | ||
count: 492086000, | ||
avg: 1.0160118038310376e-8, | ||
rps: 98424053.36525989, | ||
percent: 9.95071720945748 | ||
} | ||
}, | ||
{ | ||
name: 'Increment with i + 1', | ||
reference: true, | ||
stat: { | ||
duration: 4.999535403, | ||
cycle: 447541, | ||
count: 447541000, | ||
avg: 1.117112265244972e-8, | ||
rps: 89516517.82112603, | ||
percent: 0 | ||
} | ||
} | ||
] | ||
``` | ||
# API | ||
## Class Benchmarkify | ||
```js | ||
let benchmark = new Benchmarkify("Benchmark #1", opts); | ||
``` | ||
### Constructor options | ||
* `logger` - print messages to this logger. Default: `console` | ||
* `spinner` - show spinner when running tests. Default: `true` | ||
* `minSamples` - Minimum samples. Default: `0` - not used | ||
### Methods | ||
* `createSuite` - Create a new benchmark suite. | ||
* `run(suites: Array): Promise` - | ||
## Class Suite | ||
```js | ||
let bench1 = benchmark.createSuite("Date performance", { time: 1000 }); | ||
``` | ||
### Constructor options | ||
* `name` - Name of suite. | ||
* `time` - Time of test. Default: `5000` (5sec) | ||
* `minSamples` - Minimum samples. Default `0` - disabled | ||
### Methods | ||
* `add(name: string, fn: Function, opts: Object)` - Add a function to the suite | ||
* `skip(name: string, fn: Function, opts: Object)` - Skip the function | ||
* `only(name: string, fn: Function, opts: Object)` - Run only this function | ||
* `ref(name: string, fn: Function, opts: Object)` - Add a function and it'll be the reference | ||
* `run(): Promise` - Run the suite. | ||
### Async functions | ||
If you would like to test async function use the `done` callback. | ||
```js | ||
bench.add("Async call test", done => { | ||
asyncFunction(data).then(() => done()); | ||
}); | ||
``` | ||
# License | ||
Benchmarkify is available under the [MIT license](https://tldrlegal.com/license/mit-license). | ||
# Contact | ||
Copyright (C) 2017 Icebob | ||
[![@icebob](https://img.shields.io/badge/github-icebob-green.svg)](https://github.com/icebob) [![@icebob](https://img.shields.io/badge/twitter-Icebobcsi-blue.svg)](https://twitter.com/Icebobcsi) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
52794
13
745
149
0
1