node/benchmark
James M Snell 85ab4a5f12 buffer: add .from(), .alloc() and .allocUnsafe()
Several changes:

* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs

PR-URL: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2016-03-16 08:34:02 -07:00
..
arrays benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
assert benchmark: fix configuation parameters 2016-02-26 20:29:10 +11:00
buffers buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
child_process benchmark: move misc to categorized directories 2016-02-26 20:28:45 +11:00
crypto buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
dgram buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
domain benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
events benchmark: refactor to eliminate redeclared vars 2016-03-01 13:48:42 -08:00
fixtures src: replace naive search in Buffer::IndexOf 2015-10-07 21:09:53 -07:00
fs buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
http buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
misc benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
module benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
net buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
path tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08:00
process benchmark: move misc to categorized directories 2016-02-26 20:28:45 +11:00
querystring benchmark: refactor to eliminate redeclared vars 2016-03-01 13:48:42 -08:00
string_decoder buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
timers benchmark: move misc to categorized directories 2016-02-26 20:28:45 +11:00
tls buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
url benchmark: merge url.js with url-resolve.js 2016-02-26 20:29:05 +11:00
util benchmark: add util.format benchmark 2016-03-04 17:27:10 -06:00
common.js tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08:00
compare.js benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
fs-write-stream-throughput.js buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
http_bench.js tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08:00
http_server_lag.js tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08:00
http_simple_auto.js buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
http_simple_bench.sh meta: remove use of profanity in source 2015-12-02 11:05:11 -08:00
http_simple_cluster.js benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
http_simple.js buffer: add .from(), .alloc() and .allocUnsafe() 2016-03-16 08:34:02 -07:00
http_simple.rb fix whitespace errors 2010-06-29 23:59:24 -07:00
http-flamegraph.sh node: rename from io.js to node 2015-08-23 17:59:43 -04:00
http.sh node: rename from io.js to node 2015-08-23 17:59:43 -04:00
idle_clients.js benchmark: fix lint errors 2016-03-03 09:53:22 -08:00
idle_server.js tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08:00
io.c bench: Make io.c output easier to read 2013-02-19 14:14:37 -08:00
plot_csv.R node: rename from io.js to node 2015-08-23 17:59:43 -04:00
plot.R node: rename from io.js to node 2015-08-23 17:59:43 -04:00
README.md doc: rename from iojs(1) to node(1) in benchmarks 2015-09-15 14:02:02 -04:00
report-startup-memory.js benchmark: use strict mode 2016-02-22 11:09:26 -08:00
static_http_server.js tools,benchmark: increase lint compliance 2016-02-27 20:15:17 -08: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
}