node/benchmark
Anatoli Papirovski 6f33953d90
benchmark: fix timeout in write-stream-throughput
PR-URL: https://github.com/nodejs/node/pull/17958
Fixes: https://github.com/nodejs/node/issues/17901
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2018-02-12 19:28:32 -05:00
..
arrays benchmark: convert var to es6 const 2017-09-20 11:22:53 -04:00
assert benchmark: add assert.deep[Strict]Equal benchmarks 2017-03-08 17:10:46 -08:00
buffers tools: remove legacy indentation linting 2017-09-05 12:49:50 -04:00
child_process tools: remove legacy indentation linting 2017-09-05 12:49:50 -04:00
crypto crypto: fix Node_SignFinal 2017-10-25 04:09:43 -04:00
dgram benchmark: fix dgram/bind-params.js benchmark 2017-09-20 13:34:12 -04:00
domain
es benchmark: reformat code for clarity 2016-12-20 14:39:59 -05:00
events
fixtures
fs benchmark: fix timeout in write-stream-throughput 2018-02-12 19:28:32 -05:00
http benchmark: use smaller n value in some http tests 2017-10-25 04:09:43 -04:00
misc test: reduce run time for misc benchmark tests 2017-11-28 13:10:14 +09:00
module benchmark: add final clean-up to module-loader.js 2017-07-11 17:42:58 +01:00
net benchmark,windows: TCP.readStart() meaningful only after completion 2017-07-11 17:43:44 +01:00
os os: improve loadavg() performance 2017-03-08 17:11:23 -08:00
path
process process: improve memoryUsage() performance 2017-03-08 17:11:23 -08:00
querystring querystring: improve unescapeBuffer performance 2017-03-08 17:11:17 -08:00
streams stream: avoid additional validation for Buffers 2017-04-18 20:01:48 -04:00
string_decoder string_decoder: rewrite implementation 2016-06-02 22:41:59 +10:00
timers benchmark: add more thorough timers benchmarks 2017-03-08 17:11:20 -08:00
tls benchmark: reformat code for clarity 2016-12-20 14:39:59 -05:00
url url: adding WHATWG URL support 2018-02-12 19:28:11 -05:00
util benchmark: fix typo in inspect-proxy 2017-09-05 12:49:54 -04:00
common.js tools: update ESLint to current version 2017-03-08 17:11:24 -08:00
compare.js
fs-write-stream-throughput.js
http-flamegraph.sh
http.sh
http_bench.js
http_server_lag.js
http_simple.js benchmark: reformat code for clarity 2016-12-20 14:39:59 -05:00
http_simple.rb
http_simple_auto.js
http_simple_bench.sh
http_simple_cluster.js
idle_clients.js test,benchmark: fix lint errors on v6.x 2016-10-11 15:45:50 -04:00
idle_server.js
io.c
plot.R
plot_csv.R
README.md tools: remove legacy indentation linting 2017-09-05 12:49:50 -04:00
report-startup-memory.js
static_http_server.js benchmark: remove unused variables 2016-07-15 08:09:42 -05:00

Node.js core benchmark tests

This folder contains benchmark tests to measure the performance for certain Node.js APIs.

Prerequisites

Most of the http benchmarks require wrk and ab (ApacheBench) being installed. These may be available through your preferred package manager.

If they are not available:

  • wrk may easily be built from source via make.
  • ab is sometimes bundled in a package called apache2-utils.

How to run tests

There are three ways to run benchmark tests:

Run all tests of a given type

For example, buffers:

node benchmark/common.js buffers

The above command will find all scripts under buffers directory and require each of them as a module. When a test script is required, it creates an instance of Benchmark (a class defined in common.js). In the next tick, the Benchmark constructor iterates through the configuration object property values and runs the test function with each of the combined arguments in spawned processes. For example, buffers/buffer-read.js has the following configuration:

var bench = common.createBenchmark(main, {
  noAssert: [false, true],
  buffer: ['fast', 'slow'],
  type: ['UInt8', 'UInt16LE', 'UInt16BE',
         'UInt32LE', 'UInt32BE',
         'Int8', 'Int16LE', 'Int16BE',
         'Int32LE', 'Int32BE',
         'FloatLE', 'FloatBE',
         'DoubleLE', 'DoubleBE'],
  millions: [1]
});

The runner takes one item from each of the property array value to build a list of arguments to run the main function. The main function will receive the conf object as follows:

  • first run:
{   noAssert: false,
    buffer: 'fast',
    type: 'UInt8',
    millions: 1
}
  • second run:
{   noAssert: false,
    buffer: 'fast',
    type: 'UInt16LE',
    millions: 1
}

...

In this case, the main function will run 2214*1 = 56 times. The console output looks like the following:

buffers//buffer-read.js
buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 271.83
buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 239.43
buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 244.57
...

The last number is the rate of operations. Higher is better.

Run an individual test

For example, buffer-slice.js:

node benchmark/buffers/buffer-read.js

The output:

buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 246.79
buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 240.11
buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 245.91
...

Run tests with options

This example will run only the first type of url test, with one iteration. (Note: benchmarks require many iterations to be statistically accurate.)

node benchmark/url/url-parse.js type=one n=1

Output:

url/url-parse.js type=one n=1: 1663.74402

How to write a benchmark test

The benchmark tests are grouped by types. Each type corresponds to a subdirectory, such as arrays, buffers, or fs.

Let's add a benchmark test for Buffer.slice function. We first create a file buffers/buffer-slice.js.

The code snippet

var common = require('../common.js'); // Load the test runner

var SlowBuffer = require('buffer').SlowBuffer;

// Create a benchmark test for function `main` and the configuration variants
var bench = common.createBenchmark(main, {
  type: ['fast', 'slow'], // Two types of buffer
  n: [512] // Number of times (each unit is 1024) to call the slice API
});

function main(conf) {
  // Read the parameters from the configuration
  var n = +conf.n;
  var b = conf.type === 'fast' ? buf : slowBuf;
  bench.start(); // Start benchmarking
  for (var i = 0; i < n * 1024; i++) {
    // Add your test here
    b.slice(10, 256);
  }
  bench.end(n); // End benchmarking
}