1 # io.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] being installed.
9 These are most often available through your preferred package manager.
11 [wrk]: https://github.com/wg/wrk
12 [ab]: http://httpd.apache.org/docs/2.2/programs/ab.html
16 There are two ways to run benchmark tests:
18 1. Run all tests of a given type, for example, buffers
21 iojs benchmark/common.js buffers
24 The above command will find all scripts under `buffers` directory and require
25 each of them as a module. When a test script is required, it creates an instance
26 of `Benchmark` (a class defined in common.js). In the next tick, the `Benchmark`
27 constructor iterates through the configuration object property values and run
28 the test function with each of the combined arguments in spawned processes. For
29 example, buffers/buffer-read.js has the following configuration:
32 var bench = common.createBenchmark(main, {
33 noAssert: [false, true],
34 buffer: ['fast', 'slow'],
35 type: ['UInt8', 'UInt16LE', 'UInt16BE',
36 'UInt32LE', 'UInt32BE',
37 'Int8', 'Int16LE', 'Int16BE',
40 'DoubleLE', 'DoubleBE'],
44 The runner takes one item from each of the property array value to build a list
45 of arguments to run the main function. The main function will receive the conf
67 In this case, the main function will run 2*2*14*1 = 56 times. The console output
68 looks like the following:
71 buffers//buffer-read.js
72 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 271.83
73 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 239.43
74 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 244.57
78 2. Run an individual test, for example, buffer-slice.js
81 iojs benchmark/buffers/buffer-read.js
85 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 246.79
86 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 240.11
87 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 245.91
91 3. Run tests with options
93 This example will run only the first type of url test, with one iteration.
94 (Note: benchmarks require __many__ iterations to be statistically accurate.)
98 iojs benchmark/url/url-parse.js type=one n=1
102 url/url-parse.js type=one n=1: 1663.74402
105 ## How to write a benchmark test
107 The benchmark tests are grouped by types. Each type corresponds to a subdirectory,
108 such as `arrays`, `buffers`, or `fs`.
110 Let's add a benchmark test for Buffer.slice function. We first create a file
111 buffers/buffer-slice.js.
116 var common = require('../common.js'); // Load the test runner
118 var SlowBuffer = require('buffer').SlowBuffer;
120 // Create a benchmark test for function `main` and the configuration variants
121 var bench = common.createBenchmark(main, {
122 type: ['fast', 'slow'], // Two types of buffer
123 n: [512] // Number of times (each unit is 1024) to call the slice API
126 function main(conf) {
127 // Read the parameters from the configuration
129 var b = conf.type === 'fast' ? buf : slowBuf;
130 bench.start(); // Start benchmarking
131 for (var i = 0; i < n * 1024; i++) {
132 // Add your test here
135 bench.end(n); // End benchmarking