Skip to content

Commit

Permalink
benchmark: check for wrk ahead of running benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jbergstroem committed Feb 28, 2015
1 parent 3bc1fb0 commit a281235
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pkgsrc: $(PKGSRC)
haswrk=$(shell which wrk > /dev/null 2>&1; echo $$?)
wrk:
ifneq ($(haswrk), 0)
@echo "please install wrk before proceeding"; >&2
@echo "please install wrk before proceeding. More information can be found in benchmark/README.md." >&2
@exit 1
endif

Expand Down
8 changes: 6 additions & 2 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
This folder contains benchmark tests to measure the performance for certain
io.js APIs.

## prerequisites
## Prerequisites

Most of the http benchmarks require `wrk` and `ab` being installed.
Most of the http benchmarks require [`wrk`][wrk] and [`ab`][ab] being installed.
These are most often available through your preferred package manager.

[wrk]: https://github.com/wg/wrk
[ab]: http://httpd.apache.org/docs/2.2/programs/ab.html

## How to run tests

Expand Down
21 changes: 17 additions & 4 deletions benchmark/common.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var child_process = require('child_process');

var silent = +process.env.NODE_BENCH_SILENT;

exports.PORT = process.env.PORT || 12346;

// If this is the main module, then run the benchmarks
if (module === require.main) {
hasWrk();
var type = process.argv[2];
var testFilter = process.argv[3];
if (!type) {
Expand Down Expand Up @@ -36,6 +37,15 @@ if (module === require.main) {
runBenchmarks();
}

function hasWrk() {
var result = child_process.spawnSync('wrk', ['-h']);
if (result.error.code === 'ENOENT') {
console.error('Couldn\'t locate `wrk` which is needed for running ' +
'benchmarks. Check benchmark/README.md for further instructions.');
process.exit(-1);
}
}

function runBenchmarks() {
var test = tests.shift();
if (!test)
Expand All @@ -48,7 +58,7 @@ function runBenchmarks() {
test = path.resolve(dir, test);

var a = (process.execArgv || []).concat(test);
var child = spawn(process.execPath, a, { stdio: 'inherit' });
var child = child_process.spawn(process.execPath, a, { stdio: 'inherit' });
child.on('close', function(code) {
if (code) {
process.exit(code);
Expand All @@ -70,7 +80,10 @@ function Benchmark(fn, options) {
this._name = require.main.filename.split(/benchmark[\/\\]/).pop();
this._start = [0,0];
this._started = false;

var self = this;

hasWrk();
process.nextTick(function() {
self._run();
});
Expand All @@ -85,7 +98,7 @@ Benchmark.prototype.http = function(p, args, cb) {
args = args.concat(url);

var out = '';
var child = spawn('wrk', args);
var child = child_process.spawn('wrk', args);

child.stdout.setEncoding('utf8');

Expand Down Expand Up @@ -145,7 +158,7 @@ Benchmark.prototype._run = function() {
if (!argv)
return;
argv = process.execArgv.concat(argv);
var child = spawn(node, argv, { stdio: 'inherit' });
var child = child_process.spawn(node, argv, { stdio: 'inherit' });
child.on('close', function(code, signal) {
if (code)
console.error('child process exited with code ' + code);
Expand Down

0 comments on commit a281235

Please sign in to comment.