1 # Node.js core benchmark tests
3 This folder contains benchmark tests to measure the performance for certain
8 Most of the http benchmarks require [`wrk`][wrk] and [`ab`][ab] (ApacheBench) being installed.
9 These may be available through your preferred package manager.
11 If they are not available:
12 - `wrk` may easily be built [from source][wrk] via `make`.
13 - `ab` is sometimes bundled in a package called `apache2-utils`.
15 [wrk]: https://github.com/wg/wrk
16 [ab]: http://httpd.apache.org/docs/2.2/programs/ab.html
20 There are three ways to run benchmark tests:
22 ### Run all tests of a given type
27 node benchmark/common.js buffers
30 The above command will find all scripts under `buffers` directory and require
31 each of them as a module. When a test script is required, it creates an instance
32 of `Benchmark` (a class defined in common.js). In the next tick, the `Benchmark`
33 constructor iterates through the configuration object property values and runs
34 the test function with each of the combined arguments in spawned processes. For
35 example, buffers/buffer-read.js has the following configuration:
38 var bench = common.createBenchmark(main, {
39 noAssert: [false, true],
40 buffer: ['fast', 'slow'],
41 type: ['UInt8', 'UInt16LE', 'UInt16BE',
42 'UInt32LE', 'UInt32BE',
43 'Int8', 'Int16LE', 'Int16BE',
46 'DoubleLE', 'DoubleBE'],
50 The runner takes one item from each of the property array value to build a list
51 of arguments to run the main function. The main function will receive the conf
73 In this case, the main function will run 2*2*14*1 = 56 times. The console output
74 looks like the following:
77 buffers//buffer-read.js
78 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 271.83
79 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 239.43
80 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 244.57
84 The last number is the rate of operations. Higher is better.
86 ### Run an individual test
88 For example, buffer-slice.js:
91 node benchmark/buffers/buffer-read.js
95 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 246.79
96 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 240.11
97 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 245.91
101 ### Run tests with options
103 This example will run only the first type of url test, with one iteration.
104 (Note: benchmarks require __many__ iterations to be statistically accurate.)
108 node benchmark/url/url-parse.js type=one n=1
112 url/url-parse.js type=one n=1: 1663.74402
115 ## How to write a benchmark test
117 The benchmark tests are grouped by types. Each type corresponds to a subdirectory,
118 such as `arrays`, `buffers`, or `fs`.
120 Let's add a benchmark test for Buffer.slice function. We first create a file
121 buffers/buffer-slice.js.
126 var common = require('../common.js'); // Load the test runner
128 var SlowBuffer = require('buffer').SlowBuffer;
130 // Create a benchmark test for function `main` and the configuration variants
131 var bench = common.createBenchmark(main, {
132 type: ['fast', 'slow'], // Two types of buffer
133 n: [512] // Number of times (each unit is 1024) to call the slice API
136 function main(conf) {
137 // Read the parameters from the configuration
139 var b = conf.type === 'fast' ? buf : slowBuf;
140 bench.start(); // Start benchmarking
141 for (var i = 0; i < n * 1024; i++) {
142 // Add your test here
145 bench.end(n); // End benchmarking