-
Notifications
You must be signed in to change notification settings - Fork 1
/
perf.js
176 lines (141 loc) · 4.06 KB
/
perf.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
var cluster = require('cluster');
var pigato = require('pigato');
var async = require('async');
var _ = require('lodash');
var cmd = require('commander');
cmd
.option('--bn <val>', 'Num of Brokers', 1)
.option('--wn <val>', 'Num of Workers (for each Broker)', 1)
.option('--cn <val>', 'Num of Clients (for each Broker)', 1)
.option('--pn <val>', 'Num of Parallel Requests (for each Client)', 1)
.option('--p <val>', 'Num of messages (for each Client)', 50000)
.option('--m <val>', 'Use memory cache (1=enabled|0=disabled) (default=0)', 0)
.option('--s <val>', 'Num of waves (default=1)', 1)
.option('--e <val>', 'Num of waves (default=tcp://127.0.0.1:7777)', 'tcp://127.0.0.1:777')
.option('-N, --nofork', 'Don\'t use fork' );
cmd.on('--help', function() {
console.log('Examples:');
console.log('\tnode ' + cmd.name() + ' --bn 2 --wn 2 --cn 2 --p 50000');
});
cmd.parse(process.argv);
_.each(['bn', 'wn', 'cn', 'p', 'm', 's'], function(k) {
cmd[k] = +cmd[k];
});
var chunk = 'foo';
if (cluster.isMaster) {
console.log("RUNNING CONF");
console.log("\t", [
cmd.bn + " brokers",
cmd.wn + " workers",
cmd.cn + " clients",
"cache " + (cmd.m ? 'on' : 'off'),
cmd.p + " requests"
].join(", "));
for (var i = 0; i < (cmd.bn + (cmd.bn * cmd.wn) + (cmd.bn * cmd.cn)); i++) {
fork(i+1);
}
var kills = 0;
cluster.on('exit', function(worker, code, signal) {
kills++;
if (kills === (cmd.cn * cmd.bn)) {
process.exit(0);
}
});
return
}else{
fork();
};
function fork(ID){
if(!cmd['nofork'] && ID){
return cluster.fork();
}
setImmediate(function() {
var processID = ID || cluster.worker.processID || cluster.worker.id;
if (processID <= cmd.bn) {
var broker = new pigato.Broker(
cmd.e + processID,
{ cache: !!cmd.m }
);
broker.on('error', function(err) {
console.log("broker", err);
});
broker.start(function() {
console.log('BROKER ' + processID);
});
} else if (processID <= cmd.bn + (cmd.bn * cmd.wn)) {
var b = (processID % cmd.bn) + 1;
var worker = new pigato.Worker(
cmd.e + b, 'echo',
{ concurrency: 1000 }
);
worker.on('error', function(err) {
console.log("worker", err);
});
worker.on('request', function(inp, res) {
if (cmd.m) {
res.opts.cache = 100000;
}
setImmediate(function() {
res.end(inp + 'FINAL');
});
});
worker.start();
console.log("WORKER (BROKER " + b + ")");
} else {
var b = (processID % cmd.bn) + 1;
var client = new pigato.Client( cmd.e + b);
var sn = 0;
var tp = cmd.p * cmd.s;
console.log("CLIENT (" + tp + " reqs/" + cmd.s + " waves) (BROKER " + b + ")");
client.on('connect', function() {
console.log('connected');
})
.on('disconnect', function() {
console.log('disconnected');
})
.on('error', function(err) {
console.log(err);
});
client.start();
var d1;
var rcnt = 0;
function done() {
var d2 = new Date();
var hmany = d2.getTime() - d1.getTime();
console.log("CLIENT GOT answer", hmany + " milliseconds. " + (tp / (hmany / 1000)).toFixed(2) + " requests/sec.");
client.stop();
setTimeout(function() {
cluster.worker.kill();
}, 1000);
}
function send() {
var prcnt = 0;
sn++;
console.log("C/" + processID + ": WAVE=" + sn);
for (var k = 0; k < cmd.p; k++) {
client.request(
'echo', chunk,
{ timeout: -1 }
)
.on('data', function() {})
.on('end', function() {
rcnt++;
prcnt++;
if (prcnt === cmd.p && sn < cmd.s) {
send();
return;
}
if (rcnt < tp) {
return;
}
done();
});
}
}
setTimeout(function() {
d1 = new Date();
send();
}, 2000);
}
})
};