-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
103 lines (86 loc) · 2.37 KB
/
client.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
var sys = require('sys');
var http = require('http');
var repl = require('repl');
var serverPort = 8080;
var serverHost = 'localhost';
var serverPath = '/';
var clientCreationDelay = 0.01 * 1000;
var reportDelay = 5 * 1000;
var clientLimit = 1000;
var responseCount = 0;
var clients = [];
function getTimestamp () {
return(Math.round(new Date().getTime() / 1000));
}
function handleClientConnect () { }
function handleClientClose (self) {
if (self.loop)
self.request(serverPath, { "Host": serverHost })
.addListener("response", handleRequestFinish);
}
function handleResponseBody (chunk) { }
function handleRequestFinish (res) {
res.addListener('complete', handleResponseComplete);
}
function handleResponseComplete () {
responseCount++;
}
function formatStats () {
var memInfo = process.memoryUsage();
return('LIMIT: ' + clientLimit
+ ' | CONN: ' + clients.length
+ ' | RESP: ' + responseCount
+ " | RS: " + memInfo.rss
+ " | VS: " + memInfo.vsize
+ " | HT: " + memInfo.heapTotal
+ " | HU: " + memInfo.heapUsed
);
}
process.addListener("SIGINT", function () {
sys.puts("Quitting. " + formatStats());
process.exit(0);
});
function checkClientCount () {
if (clients.length < clientLimit) {
var client = http.createClient(serverPort, serverHost)
//.addListener('connect', handleClientConnect)
.addListener('close', function () { handleClientClose(client) });
clients.push(client);
client.loop = true;
handleClientClose(client);
//sys.puts('Client added');
}
else if (clients.length > clientLimit) {
var client = clients.pop();
client.loop = false;
/* Arg@! Doesn't work...
var handlers = client.listeners('close');
sys.puts("DEB: " + handlers.length);
for (var handler in handlers)
client.removeListener('close', handler);
*/
//sys.puts('Client removed');
}
if (clients.length != clientLimit)
setTimeout(checkClientCount, clientCreationDelay);
}
function add (count) {
clientLimit += count || 1;
if (clientLimit < 0)
clientLimit = 0;
checkClientCount();
}
function rem (count) {
clientLimit -= count || 1;
if (clientLimit < 0)
clientLimit = 0;
checkClientCount();
}
setInterval(function() {
sys.puts(formatStats());
}, reportDelay);
sys.puts("Starting clients to load test http://" + serverHost + ":" + serverPort + serverPath);
checkClientCount();
var replConext = repl.start().context
replConext.add = add;
replConext.rem = rem;