1 var redis = require("./index"),
2 metrics = require("metrics"),
3 num_clients = parseInt(process.argv[2], 10) || 5,
6 versions_logged = false,
10 small_str, large_str, small_buf, large_buf;
12 redis.debug_mode = false;
14 function lpad(input, len, chr) {
15 var str = input.toString();
18 while (str.length < len) {
24 metrics.Histogram.prototype.print_line = function () {
25 var obj = this.printObj();
27 return lpad(obj.min, 4) + "/" + lpad(obj.max, 4) + "/" + lpad(obj.mean.toFixed(2), 7) + "/" + lpad(obj.p95.toFixed(2), 7);
37 this.clients_ready = 0;
38 this.commands_sent = 0;
39 this.commands_completed = 0;
40 this.max_pipeline = this.args.pipeline || num_requests;
41 this.client_options = args.client_options || client_options;
43 this.connect_latency = new metrics.Histogram();
44 this.ready_latency = new metrics.Histogram();
45 this.command_latency = new metrics.Histogram();
48 Test.prototype.run = function (callback) {
51 this.callback = callback;
53 for (i = 0; i < num_clients ; i++) {
58 Test.prototype.new_client = function (id) {
59 var self = this, new_client;
61 new_client = redis.createClient(6379, "127.0.0.1", this.client_options);
62 new_client.create_time = Date.now();
64 new_client.on("connect", function () {
65 self.connect_latency.update(Date.now() - new_client.create_time);
68 new_client.on("ready", function () {
69 if (! versions_logged) {
70 console.log("Client count: " + num_clients + ", node version: " + process.versions.node + ", server version: " +
71 new_client.server_info.redis_version + ", parser: " + new_client.reply_parser.name);
72 versions_logged = true;
74 self.ready_latency.update(Date.now() - new_client.create_time);
76 if (self.clients_ready === self.clients.length) {
77 self.on_clients_ready();
81 self.clients[id] = new_client;
84 Test.prototype.on_clients_ready = function () {
85 process.stdout.write(lpad(this.args.descr, 13) + ", " + lpad(this.args.pipeline, 5) + "/" + this.clients_ready + " ");
86 this.test_start = Date.now();
91 Test.prototype.fill_pipeline = function () {
92 var pipeline = this.commands_sent - this.commands_completed;
94 while (this.commands_sent < num_requests && pipeline < this.max_pipeline) {
100 if (this.commands_completed === num_requests) {
106 Test.prototype.stop_clients = function () {
109 this.clients.forEach(function (client, pos) {
110 if (pos === self.clients.length - 1) {
111 client.quit(function (err, res) {
120 Test.prototype.send_next = function () {
122 cur_client = this.commands_sent % this.clients.length,
123 command_num = this.commands_sent,
126 this.clients[cur_client][this.args.command](this.args.args, function (err, res) {
130 self.commands_completed++;
131 self.command_latency.update(Date.now() - start);
132 self.fill_pipeline();
136 Test.prototype.print_stats = function () {
137 var duration = Date.now() - this.test_start;
139 console.log("min/max/avg/p95: " + this.command_latency.print_line() + " " + lpad(duration, 6) + "ms total, " +
140 lpad((num_requests / (duration / 1000)).toFixed(2), 8) + " ops/sec");
144 small_buf = new Buffer(small_str);
145 large_str = (new Array(4097).join("-"));
146 large_buf = new Buffer(large_str);
148 tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 1}));
149 tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 50}));
150 tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 200}));
151 tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 20000}));
153 tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 1}));
154 tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 50}));
155 tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 200}));
156 tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 20000}));
158 tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 1}));
159 tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 50}));
160 tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 200}));
161 tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 20000}));
163 tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 1}));
164 tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 50}));
165 tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 200}));
166 tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 20000}));
168 tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 1, client_opts: { return_buffers: true} }));
169 tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 50, client_opts: { return_buffers: true} }));
170 tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 200, client_opts: { return_buffers: true} }));
171 tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 20000, client_opts: { return_buffers: true} }));
173 tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 1}));
174 tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 50}));
175 tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 200}));
176 tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 20000}));
178 tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 1}));
179 tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 50}));
180 tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 200}));
181 tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 20000}));
183 tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 1}));
184 tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 50}));
185 tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 200}));
186 tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 20000}));
188 tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 1, client_opts: { return_buffers: true} }));
189 tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 50, client_opts: { return_buffers: true} }));
190 tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 200, client_opts: { return_buffers: true} }));
191 tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 20000, client_opts: { return_buffers: true} }));
193 tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 1}));
194 tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 50}));
195 tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 200}));
196 tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 20000}));
198 tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 1}));
199 tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 50}));
200 tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 200}));
201 tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 20000}));
203 tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 1}));
204 tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 50}));
205 tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 200}));
206 tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 20000}));
208 tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 1}));
209 tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 50}));
210 tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 200}));
211 tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 20000}));
214 var test = tests.shift();
216 test.run(function () {
220 console.log("End of tests.");