Skip to content

Commit f3f864c

Browse files
committed
doc: describe how the benchmarks are run
* Add detailed description of the arguments of `createBenchmark` * Describe the two passes of running the benchmarks * Suggest what kind of code should go where in the benchmark example
1 parent 54f2f07 commit f3f864c

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

benchmark/README.md

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,37 +287,84 @@ chunk encoding mean confidence.interval
287287
## Creating a benchmark
288288

289289
All benchmarks use the `require('../common.js')` module. This contains the
290-
`createBenchmark(main, configs)` method which will setup your benchmark.
290+
`createBenchmark(main, configs[, options])` method which will setup your
291+
benchmark.
291292

292-
The first argument `main` is the benchmark function, the second argument
293-
specifies the benchmark parameters. `createBenchmark` will run all possible
294-
combinations of these parameters, unless specified otherwise. Note that the
295-
configuration values can only be strings or numbers.
293+
The arguments of `createBenchmark` are:
296294

297-
`createBenchmark` also creates a `bench` object, which is used for timing
295+
* `main` {Function} The benchmark function,
296+
where the code running operations and controlling timers should go
297+
* `configs` {Object} The benchmark parameters. `createBenchmark` will run all
298+
possible combinations of these parameters, unless specified otherwise.
299+
Each configuration is a property with an array of possible values.
300+
Note that the configuration values can only be strings or numbers.
301+
* `options` {Object} The benchmark options. At the moment only the `flags`
302+
option for specifying command line flags is supported.
303+
304+
`createBenchmark` returns a `bench` object, which is used for timing
298305
the runtime of the benchmark. Run `bench.start()` after the initialization
299306
and `bench.end(n)` when the benchmark is done. `n` is the number of operations
300307
you performed in the benchmark.
301308

309+
The benchmark script will be run twice:
310+
311+
The first pass will configure the benchmark with the combination of
312+
parameters specified in `configs`, and WILL NOT run the `main` function.
313+
In this pass, no flags except the ones directly passed via commands
314+
that you run the benchmarks with will be used.
315+
316+
In the second pass, the `main` function will be run, and the process
317+
will be launched with:
318+
319+
* The flags you've passed into `createBenchmark` (the third argument)
320+
* The flags in the command that you run this benchmark with
321+
322+
Beware that any code outside the `main` function will be run twice
323+
in different processes. This could be troublesome if the code
324+
outside the `main` function has side effects. In general, prefer putting
325+
the code inside the `main` function if it's more than just declaration.
326+
302327
```js
303328
'use strict';
304329
const common = require('../common.js');
305330
const SlowBuffer = require('buffer').SlowBuffer;
306331

307-
const bench = common.createBenchmark(main, {
332+
const configs = {
333+
// Number of operations, specified here so they show up in the report.
334+
// Most benchmarks just use one value for all runs.
308335
n: [1024],
309-
type: ['fast', 'slow'],
310-
size: [16, 128, 1024]
311-
});
336+
type: ['fast', 'slow'], // Custom configurations
337+
size: [16, 128, 1024] // Custom configurations
338+
};
339+
340+
const options = {
341+
// Add --expose-internals if you want to require internal modules in main
342+
flags: ['--zero-fill-buffers']
343+
};
344+
345+
// main and configs are required, options is optional.
346+
const bench = common.createBenchmark(main, configs, options);
347+
348+
// Note that any code outside main will be run twice,
349+
// in different processes, with different command line arguments.
312350

313351
function main(conf) {
352+
// You will only get the flags that you have passed to createBenchmark
353+
// earlier when main is run. If you want to benchmark the internal modules,
354+
// require them here. For example:
355+
// const URL = require('internal/url').URL
356+
357+
// Start the timer
314358
bench.start();
315359

360+
// Do operations here
316361
const BufferConstructor = conf.type === 'fast' ? Buffer : SlowBuffer;
317362

318363
for (let i = 0; i < conf.n; i++) {
319364
new BufferConstructor(conf.size);
320365
}
366+
367+
// End the timer, pass in the number of operations
321368
bench.end(conf.n);
322369
}
323370
```

0 commit comments

Comments
 (0)