-
Notifications
You must be signed in to change notification settings - Fork 6
/
wsst.js
executable file
·288 lines (240 loc) · 9.99 KB
/
wsst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright (c) 2011 Bogdan Tkachenko <[email protected]>
Tool written in NodeJS that allows to make a stress test for
your application that uses WebSockets
This file is part of WebSockets Stress Test.
WebSockets Stress Test is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* WebSockets Stress Test
*
* Tool written in NodeJS that allows to make a stress test for
* your application that uses WebSockets. You can create behavior
* scenarios that tool will run on every connection in test.
*
* @author Bogdan Tkachenko <[email protected]>
* @version 0.1
*/
var
cli = require('cli'),
WebSocket = require('ws'),
test,
multipleTest,
writeJson;
/**
* Run single test for given scenario on given URL.
*
* Result passed to callback
*
* @param webSocketUrl
* @param scenarioName
* @param countConnections
* @param cli
* @param callback
*/
test = function (webSocketUrl, scenarioName, countConnections, cli, callback) {
var
i,
startTime = (new Date()).getTime(),
endTime = null,
connections = [],
scenario = require(scenarioName[0] === '/' ? scenarioName : './' + scenarioName),
url = webSocketUrl + (scenario.path ? scenario.path : '')
countOpened = 0;
cli.info('Scenario: ' + scenario.name);
cli.info(scenario.description);
cli.info('-----------------------------\n');
cli.info('Starting test for ' + countConnections + ' connections...');
for (i=0; i<countConnections; i++) {
(function(index) {
var api;
connections[index] = {
socket: new WebSocket(url),
checkpoints: []
};
api = {
/**
* Create checkpoint in scenario
*
* @param text
*/
checkpoint: function (text) {
var time = (new Date()).getTime(), count;
count = connections[index].checkpoints.length;
if (count > 0) {
connections[index].checkpoints[count - 1].end = time;
connections[index].checkpoints[count - 1].total = time;
connections[index].checkpoints[count - 1].total -= connections[index].checkpoints[count - 1].start;
}
connections[index].checkpoints.push({
text: text,
start: time,
end: time,
total: 0
});
cli.debug('Checkpoint: ' + text);
}
};
connections[index].socket.on('open', function () {
countOpened++;
// Add default checkpoint when connection opens
api.checkpoint('Connection opened');
// And run scenario on this connection
scenario.init(connections[index].socket, api);
});
connections[index].socket.on('close', function () {
var i, j, result, connectionTime;
// Add default checkoint when connection closed
api.checkpoint('Connection closed');
countOpened--;
// If we haven't any another connections
if (countOpened === 0) {
// Save test end time
endTime = (new Date()).getTime();
// Prepare result
result = {
connections: countConnections,
scenarioName: scenarioName,
url: url,
total: (endTime - startTime),
avg: 0,
min: null,
max: null,
checkpoints: []
};
for (i in connections) {
// Calculate total connection time
connectionTime = connections[i].checkpoints[connections[i].checkpoints.length-1].end - connections[i].checkpoints[0].start;
result.avg += connectionTime;
if (connectionTime < result.min || result.min === null) {
result.min = connectionTime;
}
if (connectionTime > result.max || result.max === null) {
result.max = connectionTime;
}
// And now for every checkpoint
for (j in connections[i].checkpoints) {
if (!result.checkpoints[j]) {
result.checkpoints[j] = {
text: connections[i].checkpoints[j].text,
avg: 0,
min: null,
max: null
};
}
result.checkpoints[j].avg += connections[i].checkpoints[j].total;
if (result.checkpoints[j].min > connections[i].checkpoints[j].total || result.checkpoints[j].min === null) {
result.checkpoints[j].min = connections[i].checkpoints[j].total;
}
if (result.checkpoints[j].max < connections[i].checkpoints[j].total || result.checkpoints[j].max === null) {
result.checkpoints[j].max = connections[i].checkpoints[j].total;
}
}
}
result.avg /= countConnections;
for (j in result.checkpoints) {
result.checkpoints[j].avg /= countConnections;
}
cli.ok('');
cli.ok('Test completed!');
cli.ok('--------------------------------------------------');
cli.ok('Total test time: ' + result.total + ' ms.');
cli.ok('Average time per connection: ' + result.avg + ' ms.');
cli.ok('Minimum connection time: ' + result.min + ' ms.');
cli.ok('Maximum connection time: ' + result.max + ' ms.');
cli.ok('--------------------------------------------------\n');
cli.ok('Time profiler:');
cli.ok('----------------------------------------------------------------------------------');
cli.ok('| #\t| Average\t| Minimum\t| Maximum\t| Name')
cli.ok('----------------------------------------------------------------------------------');
for (j in result.checkpoints) {
cli.ok('| ' + j + '\t| '
+ result.checkpoints[j].avg + '\t\t| '
+ result.checkpoints[j].min + '\t\t| '
+ result.checkpoints[j].max + '\t\t| '
+ result.checkpoints[j].text);
}
cli.ok('----------------------------------------------------------------------------------');
if (typeof callback === 'function') {
callback.call(cli, result);
}
}
});
})(i);
}
};
/**
* Write test results to file in JSON format
*
* @param fileName
* @param data
*/
writeJson = function (fileName, data) {
var fs = require('fs');
// TODO: getting error on this line. don't know how to fix
//fs.writeFile(fileName, data);
};
/**
* Run several tests for scenario on given URL
*
* @param webSocketUrl
* @param scenarioName
* @param countConnections
* @param cli
* @param callback
*/
multipleTest = function (webSocketUrl, scenarioName, countConnections, cli, callback) {
var
i = 0, results = [];
var singleTest = function (result) {
if (result) {
results.push(result);
}
if (countConnections[i]) {
test(webSocketUrl, scenarioName, countConnections[i], cli, singleTest);
i++;
} else {
callback.call(cli, results);
}
};
singleTest();
};
cli.setUsage(
cli.app + ' [OPTIONS] <URL> <scenario>\n\n' +
'\x1b[1mExample\x1b[0m:\n ' +
' ' + cli.app + ' --connections 100 ws://localhost:8080 myScenario.js'
);
cli.parse({
connections: ['c', 'Single test for specified count of connections', 'int', '100'],
connectionsList: ['l', 'Multiple tests for specified list count of connections (-l 1,10,100,1000)', 'string'],
output: ['o', 'File to save JSON result', 'file']
});
cli.main(function (args, options) {
if (args.length !== 2) {
cli.fatal('Wrong number of arguments. Must be exactly 2 arguments! See `' + cli.app + ' -h` for details');
}
var connections;
if (options.connectionsList) {
connections = options.connectionsList.split(',');
multipleTest(args[0], args[1], connections, cli, function (result) {
if (options.output) {
writeJson(result);
}
});
} else {
test(args[0], args[1], options.connections, cli, function (result) {
if (options.output) {
writeJson([result]);
}
});
}
});