forked from diinvoke/websocket-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (114 loc) · 4.09 KB
/
index.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
/*global require, process*/
var Benchmark = require('./lib/benchmark.js'),
DefaultReporter = require('./lib/defaultreporter.js'),
fs = require('fs'),
program = require('commander'),
logger = require('./lib/logger'),
loadData = require('./util/loadData'),
path = require('path'),
fs = require('fs');
program
.version('0.0.3')
.usage('[options] <server>')
.option('-a, --amount <n>', 'Total number of persistent connection, Default to 100', parseInt)
.option('-c, --concurency <n>', 'Concurent connection per second, Default to 20', parseInt)
.option('-w, --worker <n>', 'number of worker', parseInt)
.option('-g, --generator <file>', 'js file for generate message or special event')
.option('-J, --JsonData <file>', 'Optional json file for init test data')
.option('-m, --message <n>', 'number of message for a client. Default to 0', parseInt)
.option('-q, --query <type>', 'request query default user_id=&token=')
.option('-P, --path <type>', 'socket path default /sokcet.io')
.option('-o, --output <output>', 'Output file')
.option('-t, --type <type>', 'type of websocket server to bench(socket.io, engine.io, faye, primus, wamp). Default to io')
.option('-p, --transport <type>', 'type of transport to websocket(engine.io, websockets, browserchannel, sockjs, socket.io). Default to websockets')
.option('-k, --keep-alive', 'Keep alive connection')
.option('-v, --verbose', 'Verbose Logging')
.parse(process.argv);
var queryPath = path.join(__dirname, './query.js');
function main() {
fs.existsSync(queryPath) && fs.unlinkSync(queryPath);
if (!program.JsonData) return init();
return loadData(program.JsonData).then((r) => init(r));
}
function init(replace_query) {
if (program.args.length < 1) {
program.help();
}
var server = program.args[0];
// Set default value
if (!program.worker) {
program.worker = 1;
}
if (!program.path) {
program.path = '/sokcet.io';
}
if (!program.verbose) {
program.verbose = false;
}
if (!program.query) {
program.query = 'user_id=&token=';
}
if (!program.amount) {
program.amount = 100;
}
if (!program.concurency) {
program.concurency = 20;
}
if (!program.generator) {
program.generator = __dirname + '/lib/generator.js';
}
if (program.generator.indexOf('/') !== 0) {
program.generator = process.cwd() + '/' + program.generator;
}
if (!program.message) {
program.message = 0;
}
if (!program.type) {
program.type = 'socket.io';
}
if (program.type === 'primus' && !program.transport) {
program.transPort = 'websockets';
}
logger.info('Launch bench with ' + program.amount + ' total connection, ' + program.concurency + ' concurent connection');
logger.info(program.message + ' message(s) send by client');
logger.info(program.worker + ' worker(s)');
logger.info('WS server : ' + program.type);
if (replace_query) {
var query = require(queryPath);
var length = query.length;
var params = query[Math.ceil(Math.random() * Number(length - 1))];
program.query = `token=${params.token}`;
}
var options = {
generatorFile : program.generator,
type : program.type,
transport : program.transport,
keepAlive : program.keepAlive,
verbose : program.verbose,
query : program.query,
path : program.path
};
if (program.verbose) {
logger.debug("Benchmark Options " + JSON.stringify(options));
}
var outputStream = null;
if (program.output) {
if (program.generator.indexOf('/') !== 0) {
program.output = __dirname + '/' + program.generator;
}
outputStream = fs.createWriteStream(program.output);
}
var reporter = new DefaultReporter(outputStream);
var bench = new Benchmark(server, reporter, options);
// On ctrl+c
process.on('SIGINT', function () {
logger.info("\nGracefully stoping worker from SIGINT (Ctrl+C)");
setTimeout(function () {
if (bench.monitor.isRunning()) {
bench.terminate();
}
}, 2000);
});
bench.launch(program.amount, program.concurency, program.worker, program.message, program.keepAlive);
}
main();