Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: use autocannon instead of wrk #7180

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ ifeq ($(XZ), 0)
ssh $(STAGINGSERVER) "touch nodejs/$(DISTTYPEDIR)/$(FULLVERSION)/node-$(FULLVERSION)-$(OSTYPE)-$(ARCH).tar.xz.done"
endif

haswrk=$(shell which wrk > /dev/null 2>&1; echo $$?)
wrk:
ifneq ($(haswrk), 0)
@echo "please install wrk before proceeding. More information can be found in benchmark/README.md." >&2
hasautocannon=$(shell which autocannon > /dev/null 2>&1; echo $$?)
autocannon:
ifneq ($(hasautocannon), 0)
@echo "please install autocannon before proceeding. More information can be found in benchmark/README.md." >&2
@exit 1
endif

Expand All @@ -615,7 +615,7 @@ bench-crypto: all
bench-tls: all
@$(NODE) benchmark/common.js tls

bench-http: wrk all
bench-http: autocannon all
@$(NODE) benchmark/common.js http

bench-fs: all
Expand Down
15 changes: 8 additions & 7 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Node.js APIs.

## Prerequisites

Most of the http benchmarks require [`wrk`][wrk] and [`ab`][ab] (ApacheBench) being installed.
These may be available through your preferred package manager.
Most of the http benchmarks require [`autocannon`][autocannon] npm module
being installed. You can do this by running:

If they are not available:
- `wrk` may easily be built [from source][wrk] via `make`.
- `ab` is sometimes bundled in a package called `apache2-utils`.
`npm install -g autocannon`

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

Autocannon is a Node script and will use Node executable that is in the path.
If you want to compare two HTTP benchmark runs make sure that the Node version
in the path is not altered.

## How to run tests

Expand Down
45 changes: 27 additions & 18 deletions benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if (['default', 'csv', 'silent'].indexOf(outputFormat) == -1) {
throw new Error('OUTPUT_FORMAT set to invalid value');
}

const autocannon_exe = process.platform === 'win32'
? 'autocannon.cmd'
: 'autocannon';

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

// If this is the main module, then run the benchmarks
Expand Down Expand Up @@ -44,11 +48,12 @@ if (module === require.main) {
runBenchmarks();
}

function hasWrk() {
var result = child_process.spawnSync('wrk', ['-h']);
function hasAutocannon() {
var result = child_process.spawnSync(autocannon_exe, ['-h']);
if (result.error && result.error.code === 'ENOENT') {
console.error('Couldn\'t locate `wrk` which is needed for running ' +
'benchmarks. Check benchmark/README.md for further instructions.');
console.error('Couldn\'t locate `autocannon` which is needed for ' +
'running benchmarks. Check benchmark/README.md for further ' +
'instructions.');
process.exit(-1);
}
}
Expand Down Expand Up @@ -98,19 +103,17 @@ function Benchmark(fn, options) {
}

// benchmark an http server.
Benchmark.prototype.http = function(p, args, cb) {
hasWrk();
Benchmark.prototype.http = function(path, duration, connections, cb) {
hasAutocannon();
var self = this;
var regexp = /Requests\/sec:[ \t]+([0-9\.]+)/;
var url = 'http://127.0.0.1:' + exports.PORT + p;

args = args.concat(url);

var out = '';
var child = child_process.spawn('wrk', args);
const args = ['-d', duration, '-c', connections, '-j',
'http://127.0.0.1:' + exports.PORT + path ];
var child = child_process.spawn(autocannon_exe, args);

child.stdout.setEncoding('utf8');

var out = '';
child.stdout.on('data', function(chunk) {
out += chunk;
});
Expand All @@ -120,17 +123,23 @@ Benchmark.prototype.http = function(p, args, cb) {
cb(code);

if (code) {
console.error('wrk failed with ' + code);
console.error('autocannon failed with ' + code);
process.exit(code);
}
var match = out.match(regexp);
var qps = match && +match[1];
if (!qps) {

try {
var result = JSON.parse(out);
} catch (err) {
// do nothing, let next line handle this
}

if (!result || !result.requests || !result.requests.average) {
console.error('%j', out);
console.error('wrk produced strange output');
console.error('autocannon produced strange output');
process.exit(1);
}
self.report(+qps);

self.report(result.requests.average);
});
};

Expand Down
4 changes: 1 addition & 3 deletions benchmark/http/chunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ function main(conf) {
const http = require('http');
var chunk = Buffer.alloc(conf.size, '8');

var args = ['-d', '10s', '-t', 8, '-c', conf.c];

var server = http.createServer(function(req, res) {
function send(left) {
if (left === 0) return res.end();
Expand All @@ -34,7 +32,7 @@ function main(conf) {
});

server.listen(common.PORT, function() {
bench.http('/', args, function() {
bench.http('/', 10, conf.c, function() {
server.close();
});
});
Expand Down
3 changes: 1 addition & 2 deletions benchmark/http/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ function main(conf) {

setTimeout(function() {
var path = '/' + conf.type + '/' + conf.length;
var args = ['-d', '10s', '-t', 8, '-c', conf.c];

bench.http(path, args, function() {
bench.http(path, 10, conf.c, function() {
w1.destroy();
w2.destroy();
});
Expand Down
3 changes: 1 addition & 2 deletions benchmark/http/end-vs-write-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ function main(conf) {
}

var method = conf.method === 'write' ? write : end;
var args = ['-d', '10s', '-t', 8, '-c', conf.c];

var server = http.createServer(function(req, res) {
method(res);
});

server.listen(common.PORT, function() {
bench.http('/', args, function() {
bench.http('/', 10, conf.c, function() {
server.close();
});
});
Expand Down
3 changes: 1 addition & 2 deletions benchmark/http/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ function main(conf) {
var server = require('../http_simple.js');
setTimeout(function() {
var path = '/' + conf.type + '/' + conf.length + '/' + conf.chunks;
var args = ['-d', '10s', '-t', 8, '-c', conf.c];

bench.http(path, args, function() {
bench.http(path, 10, conf.c, function() {
server.close();
});
}, 2000);
Expand Down