1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00
Files
node-redis/multi_bench.js
Matt Ranney ee93d1b91b Add probe for server readiness.
When a Redis server starts up, it might take a while to load the dataset into memory.
During this time, the server will accept connections, but will return errors for all non-INFO
commands.  Now node_redis will send an INFO command whenever it connects to a server.
If the info command indicates that the server is not ready, the client will keep trying until
the server is ready.  Once it is ready, the client will emit a "ready" event as well as the
"connect" event.  The client will queue up all commands sent before the server is ready, just
like it did before.  When the server is ready, all offline/non-ready commands will be replayed.
This should be backward compatible with previous versions.

To disable this ready check behavior, set `options.no_ready_check` when creating the client.

As a side effect of this change, the key/val params from the info command are available as
`client.server_options`.  Further, the version string is decomposed into individual elements
in `client.server_options.versions`.
2011-02-16 15:46:27 -10:00

132 lines
3.2 KiB
JavaScript

var redis = require("./index"),
num_clients = parseInt(process.argv[2]) || 50,
active_clients = 0,
clients = new Array(num_clients),
num_requests = 20000,
issued_requests = 0,
latency = new Array(num_requests),
tests = [],
test_start,
client_options = {
return_buffers: false
};
redis.debug_mode = false;
tests.push({
descr: "PING",
command: ["ping"]
});
tests.push({
descr: "SET",
command: ["set", "foo_rand000000000000", "bar"]
});
tests.push({
descr: "GET",
command: ["get", "foo_rand000000000000"]
});
tests.push({
descr: "INCR",
command: ["incr", "counter_rand000000000000"]
});
tests.push({
descr: "LPUSH",
command: ["lpush", "mylist", Array(8).join("-")]
});
tests.push({
descr: "LRANGE (10 elements)",
command: ["lrange", "mylist", "0", "9"]
});
tests.push({
descr: "LRANGE (100 elements)",
command: ["lrange", "mylist", "0", "99"]
});
function create_clients(callback) {
if (active_clients === num_clients) {
// common case is all clients are already created
console.log("create_clients: all clients already created " + num_clients);
callback();
} else {
var client, connected = active_clients;
while (active_clients < num_clients) {
client = clients[active_clients++] = redis.createClient(6379, "127.0.0.1", client_options);
client.on("connect", function() {
// 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);
});
}
}
}
function issue_request(client, test, cmd, args) {
var i = issued_requests++;
latency[i] = Date.now();
client[cmd](args, function() {
latency[i] = Date.now() - latency[i];
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) {
create_clients(function() {
var i = num_clients,
cmd = test.command[0],
args = test.command.slice(1);
test_start = Date.now();
issued_requests = 0;
while(i-- && issued_requests < num_requests) {
issue_request(clients[i], test, cmd, args);
}
});
}
function test_complete(test) {
var min, max, sum, avg;
var total_time = Date.now() - test_start;
var op_rate = (issued_requests / (total_time / 1000.0)).toFixed(2);
var i;
latency.sort();
min = latency[0];
max = latency[issued_requests-1];
for (sum = 0, i = 0; i < issued_requests; i++)
sum += latency[i];
avg = (sum / issued_requests).toFixed(3);
console.log(test.descr + ": " + issued_requests + " ops " + op_rate + " ops/sec " + min + "/" + max + "/" + avg);
next();
}
function next() {
var test = tests.shift();
if (test) {
test_run(test);
}
}
next();