benchmark-tester

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 = ;
Web browser
<!-- Creates in "Usage" below --><!-- Creates in "Usage" below -->
Usage
Node.js
-
Add test functions for each package and run the test.
// test.jsvar BenchmarkTester = ;;The result of running the above test is:
$ node test.jsTrim:lodash x 5,919,517 ops/sec ±2.04% -
Output the result as a Markdown table.
// test.jsvar BenchmarkTester = ;;The result of running the above test is:
$ node test.jsTrim:lodash x 6,036,712 ops/sec ±1.59%| | lodash ||:-----|------------------:|| Trim | 6,036,712 ops/sec |- Platform: Node.js 10.8.0 on Darwin 64-bit- Machine: Intel Core i7-2620M CPU @ 2.70GHz, 16GB -
A test function can be verified as follows:
// test.jsvar BenchmarkTester = ;var assert = ;var inputData = ' abc ';var expectedData = 'abc';; -
A package to be loaded can be added manually.
// test.jsvar BenchmarkTester = ;var assert = ;var inputData = ' abc ';var expectedData = 'abc';;The result of running the above test is:
$ node test.jsTrim:lodash x 5,962,477 ops/sec ±2.06%String API x 23,272,338 ops/sec ±1.55%| | String API | lodash ||:-----|-------------------:|------------------:|| Trim | 23,272,338 ops/sec | 5,962,477 ops/sec |- Platform: Node.js 10.8.0 on Darwin 64-bit- Machine: Intel Core i7-2620M CPU @ 2.70GHz, 16GB -
A package can be configured before test as follows:
// test.jsvar BenchmarkTester = ;; -
A package test data can be converted before each test as follows:
// test.jsvar BenchmarkTester = ;;
Web browser
-
Creates
load-packages.js
file in the above.var packages =lodash: name: 'lodash' module: _ version: '4.17.11';BenchmarkTesterprototype {return packagesname;}; -
Creates
browser-test.js
file in the above.var inputData = ' abc ';var expectedData = 'abc';;The result of running the above test is:
(Page's HTML fragment)
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)| | 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(Web Develper Tool)
Trim:String API x 21569892 ops/sec ±243% 56 runs sampledlodash x 5380868 ops/sec ±219% 55 runs sampled
API
The BenchmarkTester
class has following methods:
constructor => BenchmarkTester
Creates an instance of BenchmarkTester class.
.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 verifyFunc 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. |
.configPackage(packageName, configFunc) : BenchmarkTester
Execute configFunc to configure a package module.
Parameter:
Parameter | Type | Description |
---|---|---|
packageName | string | The package name. |
configFunc | function | The function to configure the package module. |
-
The API of configFunc is as follows:
Parameter:
Parameter Type Description modle object/function The package module. version string The version of the package.
.setConverter(packageName, convertFunc) : BenchmarkTester
Set a test data converter. convertFunc is executed a test data before a test.
Parameter:
Parameter | Type | Description |
---|---|---|
packageName | string | The package name. |
convertFunc | function | The function to convert a test data. |
-
The API of convertFunc is as follows:
Parameter:
Parameter Type Description testData any Test data passed by .runTest
or.verifyTest
.modle object/function The package module.
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. |
CLI options
On command line interface, this program accepts the following CLI options:
--verify-only, -V
If this option is given, this program executes only .verifyTest
methods.
--no-verify
If this option is given, this program skips .verifyTest
methods.
License
Copyright (C) 2018 Takayuki Sato.
This program is free software under MIT License. See the file LICENSE in this distribution for more details.