Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

benchmark-tester

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

benchmark-tester

Benchmark test runner with package loading and result printing functions.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

benchmark-tester NPM MIT License Build Status Build Status Coverage Status

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><!-- Creates in "Usage" below -->
<body>
<script src="./browser-test.js"></script><!-- Creates in "Usage" below -->
</body>
</html>

Usage

Node.js

  1. Add test functions for each package and run the test.

    // test.js
    var BenchmarkTester = require('benchmark-tester');
    
    new BenchmarkTester()
      .addTest('lodash', function(lodash, data) { // Loads `lodash` automatically
        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)
    
  2. Output the result as a Markdown table.

    // test.js
    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.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
    
  3. A test function can be verified as follows:

    // test.js
    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();
    
  4. A package to be loaded can be added manually.

    // test.js
    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.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
    
  5. A package can be configured before test as follows:

    // test.js
    var BenchmarkTester = require('benchmark-tester');
    
    new BenchmarkTester()
      .addTest('lodash', function(lodash, data) {
        return lodash.trim(data);
      })
      .configPackage('lodash', function(lodash, version) {
         ...
      })
      .runTest('Trim', '  abc  ')
      
      .print();
    
  6. A package test data can be converted before each test as follows:

    // test.js
    var BenchmarkTester = require('benchmark-tester');
    
    new BenchmarkTester()
      .addTest('lodash', function(lodash, data) {
        return lodash.trim(data);
      })
      .setConverter('lodash', function(data, module) {
        return '\t' + data + '\t';
      })
      .runTest('Trim', '  abc  ')
      
      .print();
    

Web browser

  1. 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];
    };
    
  2. Creates browser-test.js file in the above.

    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

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:

ParameterTypeDescription
testTitlestringThe test title to be output.
inputDataanyThe 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:

ParameterTypeDescription
packageNamestringThe package name.
testFuncfunctionThe test function for the package.
  • The API of testFunc is as follows:

    Parameter:

    ParameterTypeDescription
    moduleobject / functionThe module of the package.
    inputDataanyThe input data to be passed to the module.

.verifyTest(packageName, inputData, expectedData) => BenchmarkTester

Verifys a test function.

Parameter:

ParameterTypeDescription
packageNamestringThe package name.
inputDataanyThe input data to be passed to the module.
expectedDataanyThe expected data of the test function.

.verifyTest(packageName, verifyFunc) : BenchmarkTester

Verifys a test function.

Parameter:

ParameterTypeDescription
packageNamestringThe package name.
verifyFuncfunctionThe function to verify the test function.
  • The API of verifyFunc is as follows:

    Parameter:

    ParameterTypeDescription
    testFuncfunctionThe test function for the package.
    moduleobject / functionThe module of the package.

.addPackage(packageName, module, version) : BenchmarkTester

Add a package module be loaded manually.

Parameter:

ParameterTypeDescription
packageNamestringThe package name.
modleobject/functionThe module be loaded.
versionstringThe version of the package.

.configPackage(packageName, configFunc) : BenchmarkTester

Execute configFunc to configure a package module.

Parameter:

ParameterTypeDescription
packageNamestringThe package name.
configFuncfunctionThe function to configure the package module.
  • The API of configFunc is as follows:

    Parameter:

    ParameterTypeDescription
    modleobject/functionThe package module.
    versionstringThe version of the package.

.setConverter(packageName, convertFunc) : BenchmarkTester

Set a test data converter. convertFunc is executed a test data before a test.

Parameter:

ParameterTypeDescription
packageNamestringThe package name.
convertFuncfunctionThe function to convert a test data.
  • The API of convertFunc is as follows:

    Parameter:

    ParameterTypeDescription
    testDataanyTest data passed by .runTest or .verifyTest.
    modleobject/functionThe package module.

For customizing

._beforeTest(testInfo) : Void

Is called before starting a benchmark test.

Parameter:

ParameterTypeDescription
testInfoobjectThe benchmark test information.
  • The properties of testInfo is as follows:

    Properties:

    NameTypeDescription
    titleobjectThe test title.
    dataanyThe input data for the package test function.

._afterTest(testInfo) : Void

Is called after ending a benchmark test.

Parameter:

ParameterTypeDescription
testInfoobjectThe 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:

ParameterTypeDescription
cycleInfoobjectThe event.target of benchmark.
testInfoobjectThe 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:

ParameterTypeDescription
cycleInfoobjectThe 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:

ParameterTypeDescription
packageNamestringThe 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.

Keywords

FAQs

Package last updated on 07 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc