1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

new version of multi_bench that tests more realistic scenarios.

This commit is contained in:
Matt Ranney
2011-11-13 18:13:28 -10:00
parent 2fa2ffc438
commit 66a32f86dc

View File

@@ -1,135 +1,224 @@
var redis = require("./index"), var redis = require("./index"),
num_clients = parseInt(process.argv[2], 10) || 50, metrics = require("metrics"),
active_clients = 0, num_clients = parseInt(process.argv[2], 10) || 5,
clients = new Array(num_clients),
num_requests = 20000, num_requests = 20000,
issued_requests = 0,
latency = new Array(num_requests),
tests = [], tests = [],
test_start, parser_logged = false, versions_logged = false,
client_options = { client_options = {
return_buffers: false return_buffers: false
}; },
small_str, large_str, small_buf, large_buf;
redis.debug_mode = false; redis.debug_mode = false;
tests.push({ function lpad(input, len, chr) {
descr: "PING", var str = input.toString();
command: ["ping"] chr = chr || " ";
while (str.length < len) {
str = chr + str;
}
return str;
}
metrics.Histogram.prototype.print_line = function () {
var obj = this.printObj();
return lpad(obj.min, 4) + "/" + lpad(obj.max, 4) + "/" + lpad(obj.mean.toFixed(2), 7) + "/" + lpad(obj.p95.toFixed(2), 7);
};
function Test(args) {
var self = this;
this.args = args;
this.callback = null;
this.clients = [];
this.clients_ready = 0;
this.commands_sent = 0;
this.commands_completed = 0;
this.max_pipeline = this.args.pipeline || num_requests;
this.client_options = args.client_options || client_options;
this.connect_latency = new metrics.Histogram();
this.ready_latency = new metrics.Histogram();
this.command_latency = new metrics.Histogram();
}
Test.prototype.run = function (callback) {
var self = this, i;
this.callback = callback;
for (i = 0; i < num_clients ; i++) {
this.new_client(i);
}
};
Test.prototype.new_client = function (id) {
var self = this, new_client;
new_client = redis.createClient(6379, "127.0.0.1", this.client_options);
new_client.create_time = Date.now();
new_client.on("connect", function () {
self.connect_latency.update(Date.now() - new_client.create_time);
}); });
tests.push({ new_client.on("ready", function () {
descr: "SET", if (! versions_logged) {
command: ["set", "foo_rand000000000000", "bar"] console.log("Client count: " + num_clients + ", node version: " + process.versions.node + ", server version: " +
new_client.server_info.redis_version + ", parser: " + new_client.reply_parser.name);
versions_logged = true;
}
self.ready_latency.update(Date.now() - new_client.create_time);
self.clients_ready++;
if (self.clients_ready === self.clients.length) {
self.on_clients_ready();
}
}); });
tests.push({ self.clients[id] = new_client;
descr: "GET", };
command: ["get", "foo_rand000000000000"]
});
tests.push({ Test.prototype.on_clients_ready = function () {
descr: "INCR", process.stdout.write(lpad(this.args.descr, 13) + ", " + lpad(this.args.pipeline, 5) + "/" + this.clients_ready + " ");
command: ["incr", "counter_rand000000000000"] this.test_start = Date.now();
});
tests.push({ this.fill_pipeline();
descr: "LPUSH", };
command: ["lpush", "mylist", new Array(8).join("-")]
});
tests.push({ Test.prototype.fill_pipeline = function () {
descr: "LRANGE (10 elements)", var pipeline = this.commands_sent - this.commands_completed;
command: ["lrange", "mylist", "0", "9"]
});
tests.push({ while (this.commands_sent < num_requests && pipeline < this.max_pipeline) {
descr: "LRANGE (100 elements)", this.commands_sent++;
command: ["lrange", "mylist", "0", "99"] pipeline++;
}); this.send_next();
}
function create_clients(callback) { if (this.commands_completed === num_requests) {
if (active_clients === num_clients) { this.print_stats();
// common case is all clients are already created this.stop_clients();
console.log("create_clients: all clients already created " + num_clients); }
callback(); };
Test.prototype.stop_clients = function () {
var self = this;
this.clients.forEach(function (client, pos) {
if (pos === self.clients.length - 1) {
client.quit(function (err, res) {
self.callback();
});
} else { } else {
var client, connected = active_clients; client.quit();
while (active_clients < num_clients) {
client = clients[active_clients++] = redis.createClient(6379, "127.0.0.1", client_options);
client.on("connect", function () {
if (! parser_logged) {
console.log("Using reply parser " + client.reply_parser.name);
parser_logged = true;
}
// Fire callback when all clients are connected
connected += 1;
if (connected === num_clients) {
callback();
} }
}); });
// TODO - need to check for client disconnect };
client.on("error", function (msg) {
console.log("Connect problem:" + msg.stack); Test.prototype.send_next = function () {
var self = this,
cur_client = this.commands_sent % this.clients.length,
command_num = this.commands_sent,
start = Date.now();
this.clients[cur_client][this.args.command](this.args.args, function (err, res) {
if (err) {
throw err;
}
self.commands_completed++;
self.command_latency.update(Date.now() - start);
self.fill_pipeline();
}); });
} };
}
}
function issue_request(client, test, cmd, args) { Test.prototype.print_stats = function () {
var i = issued_requests++; var duration = Date.now() - this.test_start;
latency[i] = Date.now();
client[cmd](args, function() { console.log("min/max/avg/p95: " + this.command_latency.print_line() + " " + lpad(duration, 6) + "ms total, " +
latency[i] = Date.now() - latency[i]; lpad((num_requests / (duration / 1000)).toFixed(2), 8) + " ops/sec");
if (issued_requests < num_requests) { };
issue_request(client, test, cmd, args);
} else {
client.end();
if (--active_clients == 0)
test_complete(test);
}
});
}
function test_run(test) { small_str = "1234";
create_clients(function() { small_buf = new Buffer(small_str);
var i = num_clients, large_str = (new Array(4097).join("-"));
cmd = test.command[0], large_buf = new Buffer(large_str);
args = test.command.slice(1);
test_start = Date.now(); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 1}));
issued_requests = 0; tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 50}));
while(i-- && issued_requests < num_requests) { tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 200}));
issue_request(clients[i], test, cmd, args); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 20000}));
}
});
}
function test_complete(test) { tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 1}));
var min, max, sum, avg; tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 50}));
var total_time = Date.now() - test_start; tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 200}));
var op_rate = (issued_requests / (total_time / 1000.0)).toFixed(2); tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 20000}));
var i;
latency.sort(); tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 1}));
min = latency[0]; tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 50}));
max = latency[issued_requests-1]; tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 200}));
for (sum = 0, i = 0; i < issued_requests; i++) tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 20000}));
sum += latency[i];
avg = (sum / issued_requests).toFixed(3);
console.log(test.descr + ": " + issued_requests + " ops " + op_rate + " ops/sec " + min + "/" + max + "/" + avg); tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 1}));
tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 50}));
tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 200}));
tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 20000}));
next(); tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 1, client_opts: { return_buffers: true} }));
} tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 50, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 200, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 20000, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 1}));
tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 50}));
tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 200}));
tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 20000}));
tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 1}));
tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 50}));
tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 200}));
tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 20000}));
tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 1}));
tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 50}));
tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 200}));
tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 20000}));
tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 1, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 50, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 200, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 20000, client_opts: { return_buffers: true} }));
tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 1}));
tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 50}));
tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 200}));
tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 20000}));
tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 1}));
tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 50}));
tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 200}));
tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 20000}));
tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 1}));
tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 50}));
tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 200}));
tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 20000}));
tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 1}));
tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 50}));
tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 200}));
tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 20000}));
function next() { function next() {
var test = tests.shift(); var test = tests.shift();
if (test) { if (test) {
test_run(test); test.run(function () {
next();
});
} else {
console.log("End of tests.");
process.exit(0);
} }
} }