1 # Node.js core benchmark tests
3 This folder contains benchmark tests to measure the performance for certain
8 There are two ways to run benchmark tests:
10 1. Run all tests of a given type, for example, buffers
13 node benchmark/common.js buffers
16 The above command will find all scripts under `buffers` directory and require
17 each of them as a module. When a test script is required, it creates an instance
18 of `Benchmark` (a class defined in common.js). In the next tick, the `Benchmark`
19 constructor iterates through the configuration object property values and run
20 the test function with each of the combined arguments in spawned processes. For
21 example, buffers/buffer-read.js has the following configuration:
24 var bench = common.createBenchmark(main, {
25 noAssert: [false, true],
26 buffer: ['fast', 'slow'],
27 type: ['UInt8', 'UInt16LE', 'UInt16BE',
28 'UInt32LE', 'UInt32BE',
29 'Int8', 'Int16LE', 'Int16BE',
32 'DoubleLE', 'DoubleBE'],
36 The runner takes one item from each of the property array value to build a list
37 of arguments to run the main function. The main function will receive the conf
59 In this case, the main function will run 2*2*14*1 = 56 times. The console output
60 looks like the following:
63 buffers//buffer-read.js
64 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 271.83
65 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 239.43
66 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 244.57
70 2. Run an individual test, for example, buffer-slice.js
73 node benchmark/buffers/buffer-read.js
77 buffers/buffer-read.js noAssert=false buffer=fast type=UInt8 millions=1: 246.79
78 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16LE millions=1: 240.11
79 buffers/buffer-read.js noAssert=false buffer=fast type=UInt16BE millions=1: 245.91
83 ## How to write a benchmark test
85 The benchmark tests are grouped by types. Each type corresponds to a subdirectory,
86 such as `arrays`, `buffers`, or `fs`.
88 Let's add a benchmark test for Buffer.slice function. We first create a file
89 buffers/buffer-slice.js.
94 var common = require('../common.js'); // Load the test runner
96 var SlowBuffer = require('buffer').SlowBuffer;
98 // Create a benchmark test for function `main` and the configuration variants
99 var bench = common.createBenchmark(main, {
100 type: ['fast', 'slow'], // Two types of buffer
101 n: [512] // Number of times (each unit is 1024) to call the slice API
104 function main(conf) {
105 // Read the parameters from the configuration
107 var b = conf.type === 'fast' ? buf : slowBuf;
108 bench.start(); // Start benchmarking
109 for (var i = 0; i < n * 1024; i++) {
110 // Add your test here
113 bench.end(n); // End benchmarking