grunt-benchmark
Grunt task for benchmarking with Benchmark.js.
Getting Started
Install this grunt plugin next to your project's
Gruntfile with: npm install grunt-benchmark
Then add this line to your project's Gruntfile:
grunt;
Documentation
Basic Usage Example
Create a benchmarks/
folder and create a benchmark script within that folder,
ie fibonacci.js
:
var { return n < 2 ? n : + ;}; module { ;};
Then setup your Gruntfile config to run the benchmarks within the benchmarks/
folder:
grunt;
Then run the task:
$ grunt benchmark
Running "benchmark:all" (benchmark) task
Benchmarking "0" [benchmarks/test-timeout.js] x10...
>> test-timeout x 418,070 ops/sec ±12.73% (46 runs sampled)
Benchmark name, date, times and per iteration will be logged in a csv format.
Test Options
You can add test options to pass to Benchmark.js by exporting an object of [test options].
moduleexports = name: 'Timeout (asynchronous)' maxTime: 2 defer: true { console; } { ; };
Result:
$ grunt benchmark
Running "benchmark:singleTest" (benchmark) task
Benchmarking "Timeout (asynchronous)" [benchmarks/singleTest.js]...
Hooray!
>> Timeout (asynchronous) x 2.00 ops/sec ±0.14% (8 runs sampled)
Test Suite
You can pit implementations against one another by creating a test suite.
var { return n < 2 ? n : + ;}; var fibonacci_memoized = { var memo = 0 1; var { var result = memon; if typeof result !== 'number' result = + ; memon = result; return result; }; return fib;}; // A test suitemoduleexports = name: 'Fibonacci Showdown' tests: { ; ; } { ; ; } ;
Result:
$ grunt benchmark
Running "benchmark:fibonacci" (benchmark) task
Benchmarking suite "Fibonacci" [benchmarks/fibonacci.js]...
>> fibonacci x 13,386,628 ops/sec ±8.63% (74 runs sampled)
>> fibonacci_memoized x 30,509,658 ops/sec ±2.10% (89 runs sampled)
Fastest is fibonacci_memoized
exports.tests
as an Object:
Set exports.tests
to an Object that maps test names to functions and or [Benchmark.js test options].
moduleexports = name: 'Timeout Showdown' tests: { return; } 'Timeout: 50ms (asynchronous)': defer: true { ; } 'Timeout: 100ms (asynchronous)': defer: true { ; } ;
exports.tests
as an Array:
Set exports.tests
to an Array of functions and or [Benchmark.js test options].
moduleexports = name: 'Timeout Showdown' tests: name: 'Return immediately (synchronous)' { return; } name: 'Timeout: 50ms (asynchronous)' defer: true { ; } name: 'Timeout: 100ms (asynchronous)' defer: true { ; } ;
Benchmarking Tasks
Included is a helper, spawnTask
, for running Grunt tasks within your
benchmarks. This example will create a function to run the watch
task:
// benchmarks/watch.js// Create a spawnable watch task. Doesn't actually spawn until called.var watchTask = ; // Our actual benchmarkmodule { // start the watch task ; };
Saving output
Test results will be saved to a file if a destination file is provided.
grunt;
Results can be saved as 'csv' or 'json'. The format is determined from the dest
file extension or by setting the format
option:
grunt;
You can specify a truthy displayResults
option inside your Grunt config to
display the results using cli-table.
It will automatically pick up the dest
property, so that must be set for this
to work.
grunt;
The output will look something like:
Running "benchmark:fibonacci" task Running suite Fibonacci [benchmarks/fibonacci.js]...>> fibonacci x 13,386,628 ops/sec ±8.63% >> fibonacci_memoized x 30,509,658 ops/sec ±2.10% Results:┌──────────────────────┬───────────────────────────────────────────┬───────┬─────────┬────────┬────────────────────┐│ name │ date │ error │ count │ cycles │ hz │├──────────────────────┼───────────────────────────────────────────┼───────┼─────────┼────────┼────────────────────┤│ "fibonacci" │ "Tue Apr 23 2013 21:25:49 GMT-0700 (PDT)" │ │ 906237 │ 4 │ 15154635.038364386 │├──────────────────────┼───────────────────────────────────────────┼───────┼─────────┼────────┼────────────────────┤│ "fibonacci_memoized" │ "Fri May 24 2013 19:52:02 GMT-0400 (EDT)" │ │ 1804104 │ 4 │ 31131880.83560733 │├──────────────────────┼───────────────────────────────────────────┼───────┼─────────┼────────┼────────────────────┤│ "fibonacci" │ "Tue Apr 23 2013 22:10:55 GMT-0700 (PDT)" │ │ 910791 │ 4 │ 13386627.749339204 │├──────────────────────┼───────────────────────────────────────────┼───────┼─────────┼────────┼────────────────────┤│ "fibonacci_memoized" │ "Fri May 24 2013 19:52:11 GMT-0400 (EDT)" │ │ 1764921 │ 4 │ 30509657.596336514 │└──────────────────────┴───────────────────────────────────────────┴───────┴─────────┴────────┴────────────────────┘
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code using grunt.
Release History
- 1.0.0 Support multiple suites in one file (@zoltan-mihalyi). Update deps.
- 0.3.0 Add json output format (@creynders).
- 0.2.0 Switched to benchmark.js. Huge thanks to @lazd!
- 0.1.3 Ability to log dest to a csv file. Support Grunt@0.4.0rc7.
- 0.1.2 Update to work with Grunt@0.4.0rc3.
- 0.1.1 Fix require path
- 0.1.0 Initial release
License
Copyright (c) 2016 Kyle Robinson Young
Licensed under the MIT license.