multi_bench.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var redis = require("./index"),
  2. metrics = require("metrics"),
  3. num_clients = parseInt(process.argv[2], 10) || 5,
  4. num_requests = 20000,
  5. tests = [],
  6. versions_logged = false,
  7. client_options = {
  8. return_buffers: false
  9. },
  10. small_str, large_str, small_buf, large_buf;
  11. redis.debug_mode = false;
  12. function lpad(input, len, chr) {
  13. var str = input.toString();
  14. chr = chr || " ";
  15. while (str.length < len) {
  16. str = chr + str;
  17. }
  18. return str;
  19. }
  20. metrics.Histogram.prototype.print_line = function () {
  21. var obj = this.printObj();
  22. return lpad(obj.min, 4) + "/" + lpad(obj.max, 4) + "/" + lpad(obj.mean.toFixed(2), 7) + "/" + lpad(obj.p95.toFixed(2), 7);
  23. };
  24. function Test(args) {
  25. var self = this;
  26. this.args = args;
  27. this.callback = null;
  28. this.clients = [];
  29. this.clients_ready = 0;
  30. this.commands_sent = 0;
  31. this.commands_completed = 0;
  32. this.max_pipeline = this.args.pipeline || num_requests;
  33. this.client_options = args.client_options || client_options;
  34. this.connect_latency = new metrics.Histogram();
  35. this.ready_latency = new metrics.Histogram();
  36. this.command_latency = new metrics.Histogram();
  37. }
  38. Test.prototype.run = function (callback) {
  39. var self = this, i;
  40. this.callback = callback;
  41. for (i = 0; i < num_clients ; i++) {
  42. this.new_client(i);
  43. }
  44. };
  45. Test.prototype.new_client = function (id) {
  46. var self = this, new_client;
  47. new_client = redis.createClient(6379, "127.0.0.1", this.client_options);
  48. new_client.create_time = Date.now();
  49. new_client.on("connect", function () {
  50. self.connect_latency.update(Date.now() - new_client.create_time);
  51. });
  52. new_client.on("ready", function () {
  53. if (! versions_logged) {
  54. console.log("Client count: " + num_clients + ", node version: " + process.versions.node + ", server version: " +
  55. new_client.server_info.redis_version + ", parser: " + new_client.reply_parser.name);
  56. versions_logged = true;
  57. }
  58. self.ready_latency.update(Date.now() - new_client.create_time);
  59. self.clients_ready++;
  60. if (self.clients_ready === self.clients.length) {
  61. self.on_clients_ready();
  62. }
  63. });
  64. self.clients[id] = new_client;
  65. };
  66. Test.prototype.on_clients_ready = function () {
  67. process.stdout.write(lpad(this.args.descr, 13) + ", " + lpad(this.args.pipeline, 5) + "/" + this.clients_ready + " ");
  68. this.test_start = Date.now();
  69. this.fill_pipeline();
  70. };
  71. Test.prototype.fill_pipeline = function () {
  72. var pipeline = this.commands_sent - this.commands_completed;
  73. while (this.commands_sent < num_requests && pipeline < this.max_pipeline) {
  74. this.commands_sent++;
  75. pipeline++;
  76. this.send_next();
  77. }
  78. if (this.commands_completed === num_requests) {
  79. this.print_stats();
  80. this.stop_clients();
  81. }
  82. };
  83. Test.prototype.stop_clients = function () {
  84. var self = this;
  85. this.clients.forEach(function (client, pos) {
  86. if (pos === self.clients.length - 1) {
  87. client.quit(function (err, res) {
  88. self.callback();
  89. });
  90. } else {
  91. client.quit();
  92. }
  93. });
  94. };
  95. Test.prototype.send_next = function () {
  96. var self = this,
  97. cur_client = this.commands_sent % this.clients.length,
  98. command_num = this.commands_sent,
  99. start = Date.now();
  100. this.clients[cur_client][this.args.command](this.args.args, function (err, res) {
  101. if (err) {
  102. throw err;
  103. }
  104. self.commands_completed++;
  105. self.command_latency.update(Date.now() - start);
  106. self.fill_pipeline();
  107. });
  108. };
  109. Test.prototype.print_stats = function () {
  110. var duration = Date.now() - this.test_start;
  111. console.log("min/max/avg/p95: " + this.command_latency.print_line() + " " + lpad(duration, 6) + "ms total, " +
  112. lpad((num_requests / (duration / 1000)).toFixed(2), 8) + " ops/sec");
  113. };
  114. small_str = "1234";
  115. small_buf = new Buffer(small_str);
  116. large_str = (new Array(4097).join("-"));
  117. large_buf = new Buffer(large_str);
  118. tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 1}));
  119. tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 50}));
  120. tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 200}));
  121. tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 20000}));
  122. tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 1}));
  123. tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 50}));
  124. tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 200}));
  125. tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 20000}));
  126. tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 1}));
  127. tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 50}));
  128. tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 200}));
  129. tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 20000}));
  130. tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 1}));
  131. tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 50}));
  132. tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 200}));
  133. tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 20000}));
  134. tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 1, client_opts: { return_buffers: true} }));
  135. tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 50, client_opts: { return_buffers: true} }));
  136. tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 200, client_opts: { return_buffers: true} }));
  137. tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 20000, client_opts: { return_buffers: true} }));
  138. tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 1}));
  139. tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 50}));
  140. tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 200}));
  141. tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 20000}));
  142. tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 1}));
  143. tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 50}));
  144. tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 200}));
  145. tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 20000}));
  146. tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 1}));
  147. tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 50}));
  148. tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 200}));
  149. tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 20000}));
  150. tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 1, client_opts: { return_buffers: true} }));
  151. tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 50, client_opts: { return_buffers: true} }));
  152. tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 200, client_opts: { return_buffers: true} }));
  153. tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 20000, client_opts: { return_buffers: true} }));
  154. tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 1}));
  155. tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 50}));
  156. tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 200}));
  157. tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 20000}));
  158. tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 1}));
  159. tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 50}));
  160. tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 200}));
  161. tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 20000}));
  162. tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 1}));
  163. tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 50}));
  164. tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 200}));
  165. tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 20000}));
  166. tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 1}));
  167. tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 50}));
  168. tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 200}));
  169. tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 20000}));
  170. function next() {
  171. var test = tests.shift();
  172. if (test) {
  173. test.run(function () {
  174. next();
  175. });
  176. } else {
  177. console.log("End of tests.");
  178. process.exit(0);
  179. }
  180. }
  181. next();