Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / redis / multi_bench.js
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
12 redis.debug_mode = false;
13
14 function lpad(input, len, chr) {
15     var str = input.toString();
16     chr = chr || " ";
17
18     while (str.length < len) {
19         str = chr + str;
20     }
21     return str;
22 }
23
24 metrics.Histogram.prototype.print_line = function () {
25     var obj = this.printObj();
26     
27     return lpad(obj.min, 4) + "/" + lpad(obj.max, 4) + "/" + lpad(obj.mean.toFixed(2), 7) + "/" + lpad(obj.p95.toFixed(2), 7);
28 };
29
30 function Test(args) {
31     var self = this;
32
33     this.args = args;
34     
35     this.callback = null;
36     this.clients = [];
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;
42     
43     this.connect_latency = new metrics.Histogram();
44     this.ready_latency = new metrics.Histogram();
45     this.command_latency = new metrics.Histogram();
46 }
47
48 Test.prototype.run = function (callback) {
49     var self = this, i;
50
51     this.callback = callback;
52
53     for (i = 0; i < num_clients ; i++) {
54         this.new_client(i);
55     }
56 };
57
58 Test.prototype.new_client = function (id) {
59     var self = this, new_client;
60     
61     new_client = redis.createClient(6379, "127.0.0.1", this.client_options);
62     new_client.create_time = Date.now();
63
64     new_client.on("connect", function () {
65         self.connect_latency.update(Date.now() - new_client.create_time);
66     });
67
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;
73         }
74         self.ready_latency.update(Date.now() - new_client.create_time);
75         self.clients_ready++;
76         if (self.clients_ready === self.clients.length) {
77             self.on_clients_ready();
78         }
79     });
80
81     self.clients[id] = new_client;
82 };
83
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();
87
88     this.fill_pipeline();
89 };
90
91 Test.prototype.fill_pipeline = function () {
92     var pipeline = this.commands_sent - this.commands_completed;
93
94     while (this.commands_sent < num_requests && pipeline < this.max_pipeline) {
95         this.commands_sent++;
96         pipeline++;
97         this.send_next();
98     }
99     
100     if (this.commands_completed === num_requests) {
101         this.print_stats();
102         this.stop_clients();
103     }
104 };
105
106 Test.prototype.stop_clients = function () {
107     var self = this;
108     
109     this.clients.forEach(function (client, pos) {
110         if (pos === self.clients.length - 1) {
111             client.quit(function (err, res) {
112                 self.callback();
113             });
114         } else {
115             client.quit();
116         }
117     });
118 };
119
120 Test.prototype.send_next = function () {
121     var self = this,
122         cur_client = this.commands_sent % this.clients.length,
123         command_num = this.commands_sent,
124         start = Date.now();
125
126     this.clients[cur_client][this.args.command](this.args.args, function (err, res) {
127         if (err) {
128             throw err;
129         }
130         self.commands_completed++;
131         self.command_latency.update(Date.now() - start);
132         self.fill_pipeline();
133     });
134 };
135
136 Test.prototype.print_stats = function () {
137     var duration = Date.now() - this.test_start;
138     
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");
141 };
142
143 small_str = "1234";
144 small_buf = new Buffer(small_str);
145 large_str = (new Array(4097).join("-"));
146 large_buf = new Buffer(large_str);
147
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}));
152
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}));
157
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}));
162
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}));
167
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} }));
172
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}));
177
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}));
182
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}));
187
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} }));
192
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}));
197
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}));
202
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}));
207
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}));
212
213 function next() {
214     var test = tests.shift();
215     if (test) {
216         test.run(function () {
217             next();
218         });
219     } else {
220         console.log("End of tests.");
221         process.exit(0);
222     }
223 }
224
225 next();