Benchmark test runner with package loading and result printing functions.
Install
$ npm install -D benchmark-tester benchmark lodash platform
Load this module
Node.js
var BenchmarkTester = require('benchmark-tester');
Web browser
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script src="node_modules/lodash/lodash.min.js"></script>
<script src="node_modules/platform/platform.js"></script>
<script src="node_modules/benchmark/benchmark.js"></script>
<script src="node_modules/benchmark-tester/web/benchmark-tester.min.js"></script>
<script src="node_modules/benchmark-tester/web/markdown-table.js"></script>
<script src="./load-packages.js"></script>
<body>
<script src="./browser-test.js"></script>
</body>
</html>
Usage
Node.js
-
Add test functions for each package and run the test.
var BenchmarkTester = require('benchmark-tester');
new BenchmarkTester()
.addTest('lodash', function(lodash, data) {
return lodash.trim(data);
})
.runTest('Trim', ' abc ');
The result of running the above test is:
$ node test.js
Trim:
lodash x 5,919,517 ops/sec ±2.04% (79 runs sampled)
-
Output the result as a Markdown table.
var BenchmarkTester = require('benchmark-tester');
new BenchmarkTester()
.addTest('lodash', function(lodash, data) {
return lodash.trim(data);
})
.runTest('Trim', ' abc ')
.print();
The result of running the above test is:
$ node test/readme-example.test.js
Trim:
lodash x 6,036,712 ops/sec ±1.59% (82 runs sampled)
| | lodash(4.17.11) |
|:-----|------------------:|
| Trim | 6,036,712 ops/sec |
- Platform: Node.js 10.8.0 on Darwin 64-bit
- Machine: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz, 16GB
-
You can verify a test function as follows:
var BenchmarkTester = require('benchmark-tester');
var assert = require('assert');
var inputData = ' abc ';
var expectedData = 'abc';
new BenchmarkTester()
.addTest('lodash', function(lodash, inputData) {
return lodash.trim(inputData);
})
.verifyTest('lodash', inputData, expectedData)
.verifyTest('lodash', function(testFn, lodash) {
assert.equal(testFn(lodash, inputData), expectedData);
})
.runTest('Trim', inputData)
.print();
-
You can add a package to be loaded manually.
var BenchmarkTester = require('benchmark-tester');
var assert = require('assert');
var inputData = ' abc ';
var expectedData = 'abc';
new BenchmarkTester()
.addPackage('String API', String.prototype)
.addTest('lodash', function(lodash, inputData) {
return lodash.trim(inputData);
})
.addTest('String API', function(proto, inputData) {
return proto.trim.call(inputData);
})
.runTest('Trim', inputData)
.print();
The result of running the above test is:
$ node test/readme-example.test.js
Trim:
lodash x 5,962,477 ops/sec ±2.06% (81 runs sampled)
String API x 23,272,338 ops/sec ±1.55% (85 runs sampled)
| | String API | lodash(4.17.11) |
|:-----|-------------------:|------------------:|
| Trim | 23,272,338 ops/sec | 5,962,477 ops/sec |
- Platform: Node.js 10.8.0 on Darwin 64-bit
- Machine: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz, 16GB
Web browser
-
Creates load-packages.js
file in the above.
var packages = {
lodash: { name: 'lodash', module: _, version: '4.17.11' },
};
BenchmarkTester.prototype._getPackage = function(name) {
return packages[name];
};
-
Creates browser-test.js
file in the above.
var BenchmarkTester = require('benchmark-tester');
var assert = require('assert');
var inputData = ' abc ';
var expectedData = 'abc';
new BenchmarkTester()
.addTest('lodash', function(lodash, inputData) {
return lodash.trim(inputData);
})
.verifyTest('lodash', inputData, expectedData)
.verifyTest('String API', inputData, expectedData)
.runTest('Trim', inputData)
.print();
The result of running the above test is:
(Page's HTML fragment)
<pre id="r1540722940278">
Trim:
String API x 21,569,892 ops/sec ±2.43% (56 runs sampled)
lodash x 5,380,868 ops/sec ±2.19% (55 runs sampled)
</pre>
<pre id="r1540722952219">
| | String API | lodash(4.17.11) |
|:--------------|-------------------:|------------------:|
| Trim | 21,569,892 ops/sec | 5,380,868 ops/sec |
- Platform: Chrome 69.0.3497.100 on OS X 10.13.6 64-bit
</pre>
(Web Develper Tool)
Trim:
String API x 21,569,892 ops/sec ±2.43% (56 runs sampled)
lodash x 5,380,868 ops/sec ±2.19% (55 runs sampled)
API
class BenchmarkTester
constructor() : BenchmarkTester
Creates an instance of BenchmarkTester.
.runTest(testTitle, inputData) : BenchmarkTester
Runs a benchmark test about package modules added by .addTest
method.
Parameter:
Parameter | Type | Description |
---|
testTitle | string | The test title to be output. |
inputData | any | The input data to be used in a test. |
.print() : Void
Prints a result text.
In default, this program prints a result as a Markdown table.
.addTest(packageName, testFunc) : BenchmarkTester
Adds a package and a test function for it.
Parameter:
Parameter | Type | Description |
---|
packageName | string | The package name. |
testFunc | function | The test function for the package. |
The API of testFunc is as follows:
Parameter:
Parameter | Type | Description |
---|
module | object / function | The module of the package. |
inputData | any | The input data to be passed to the module. |
.verifyTest(packageName, inputData, expectedData) : BenchmarkTester
Verifys a test function.
Parameter:
Parameter | Type | Description |
---|
packageName | string | The package name. |
inputData | any | The input data to be passed to the module. |
expectedData | any | The expected data of the test function. |
.verifyTest(packageName, verifyFunc) : BenchmarkTester
Verifys a test function.
Parameter:
Parameter | Type | Description |
---|
packageName | string | The package name. |
verifyFunc | function | The function to verify the test function. |
The API of testFunc is as follows:
Parameter:
Parameter | Type | Description |
---|
testFunc | function | The test function for the package. |
module | object / function | The module of the package. |
.addPackage(packageName, module, version) : BenchmarkTester
Add a package module be loaded manually.
Parameter:
Parameter | Type | Description |
---|
packageName | string | The package name. |
modle | object/function | The module be loaded. |
version | string | The version of the package. |
For customizing
._beforeTest(testInfo) : Void
Is called before starting a benchmark test.
Parameter:
Parameter | Type | Description |
---|
testInfo | object | The benchmark test information. |
The properties of testInfo is as follows:
Properties:
Name | Type | Description |
---|
title | object | The test title. |
data | any | The input data for the package test function. |
._afterTest(testInfo) : Void
Is called after ending a benchmark test.
Parameter:
Parameter | Type | Description |
---|
testInfo | object | The benchmark test information. |
The properties of testInfo is same with ._beforeTest
method.
._onCycle(cycleInfo, testInfo) : Void
Is called after executing each package test function.
Parameter:
Parameter | Type | Description |
---|
cycleInfo | object | The event.target of benchmark. |
testInfo | object | The benchmark test information. |
The properties of testInfo is same with ._beforeTest
method.
._formatCycle(cycleInfo) : string
Formats the result text for a package test function.
Parameter:
Parameter | Type | Description |
---|
cycleInfo | object | The event.target of benchmark. |
._getPackage(packageName) : object / function
Loads a package module and returns it.
In Web browser, this method is needed to be overriden like load-package.js
file in the example above.
The package name of the current project can be specified.
This program will find up package.json
file and load the package module automatically.
Parameter:
Parameter | Type | Description |
---|
packageName | string | The package name. |
License
Copyright (C) 2018 Takayuki Sato.
This program is free software under MIT License.
See the file LICENSE in this distribution for more details.