Skip to content

Commit ff47dc3

Browse files
author
Ruben Bridgewater
committed
Improve multi bench
It will now print the total time elapsed and start a redis server if none is running and closes it afterwards again
1 parent bd4fca1 commit ff47dc3

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

benchmarks/multi_bench.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
'use strict';
22

3-
var redis = require("../index"),
4-
metrics = require("metrics"),
5-
num_clients = parseInt(process.argv[2], 10) || 5,
6-
num_requests = 20000,
7-
tests = [],
8-
versions_logged = false,
9-
client_options = {
10-
return_buffers: false
11-
},
12-
small_str, large_str, small_buf, large_buf, very_large_str, very_large_buf;
3+
var path = require('path');
4+
var RedisProcess = require("../test/lib/redis-process");
5+
var rp;
6+
var redis = require("../index");
7+
var totalTime = 0;
8+
var metrics = require("metrics");
9+
var num_clients = parseInt(process.argv[2], 10) || 5;
10+
var num_requests = 20000;
11+
var tests = [];
12+
var versions_logged = false;
13+
var client_options = {
14+
return_buffers: false,
15+
max_attempts: 4,
16+
parser: process.argv.indexOf('parser=javascript') === -1 ? 'hiredis' : 'javascript'
17+
};
18+
var small_str, large_str, small_buf, large_buf, very_large_str, very_large_buf;
1319

1420
function lpad(input, len, chr) {
1521
var str = input.toString();
@@ -57,7 +63,7 @@ Test.prototype.run = function (callback) {
5763
Test.prototype.new_client = function (id) {
5864
var self = this, new_client;
5965

60-
new_client = redis.createClient(6379, "127.0.0.1", this.client_options);
66+
new_client = redis.createClient(this.client_options);
6167
new_client.create_time = Date.now();
6268

6369
new_client.on("connect", function () {
@@ -77,6 +83,24 @@ Test.prototype.new_client = function (id) {
7783
}
7884
});
7985

86+
// If no redis server is running, start one
87+
new_client.on("error", function(err) {
88+
if (err.code === 'CONNECTION_BROKEN') {
89+
throw err;
90+
}
91+
if (rp) {
92+
return;
93+
}
94+
rp = true;
95+
var conf = '../test/conf/redis.conf';
96+
RedisProcess.start(function (err, _rp) {
97+
if (err) {
98+
throw err;
99+
}
100+
rp = _rp;
101+
}, path.resolve(__dirname, conf));
102+
});
103+
80104
self.clients[id] = new_client;
81105
};
82106

@@ -133,6 +157,7 @@ Test.prototype.send_next = function () {
133157

134158
Test.prototype.print_stats = function () {
135159
var duration = Date.now() - this.test_start;
160+
totalTime += duration;
136161

137162
console.log("min/max/avg/p95: " + this.command_latency.print_line() + " " + lpad(duration, 6) + "ms total, " +
138163
lpad((this.num_requests / (duration / 1000)).toFixed(2), 8) + " ops/sec");
@@ -199,8 +224,14 @@ function next() {
199224
test.run(function () {
200225
next();
201226
});
227+
} else if (rp) {
228+
// Stop the redis process if started by the benchmark
229+
rp.stop(function() {
230+
rp = undefined;
231+
next();
232+
});
202233
} else {
203-
console.log("End of tests.");
234+
console.log("End of tests. Total time elapsed:", totalTime, 'ms');
204235
process.exit(0);
205236
}
206237
}

0 commit comments

Comments
 (0)