forked from tautologistics/node_loadtest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (57 loc) · 1.53 KB
/
server.js
File metadata and controls
67 lines (57 loc) · 1.53 KB
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
var sys = require('sys');
var http = require('http');
var repl = require('repl');
var serverPort = 8080;
var connCount = 0;
var connCountTotal = 0;
var countReportDelay = 5 * 1000;
var resDelay = 15 * 1000;
function handleConnClose () {
connCount--;
}
function handleConnOpen (conn) {
connCountTotal++;
connCount++;
conn.addListener("close", handleConnClose);
}
function formatConnStat () {
var memInfo = process.memoryUsage();
return("NOW: " + Math.round(new Date().getTime() / 1000)
+ " | OC: " + connCount
+ " | TC: " + connCountTotal
+ " | RS: " + memInfo.rss
+ " | VS: " + memInfo.vsize
+ " | HT: " + memInfo.heapTotal
+ " | HU: " + memInfo.heapUsed
);
}
function connStat () {
sys.puts(formatConnStat());
}
function handleRequest (req, res) {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('x');
res.finish();
}
process.addListener("SIGINT", function () {
sys.puts("Quitting. " + connCountTotal + " connections received");
process.exit(0);
});
var server = http.createServer(function(req, res) {
if (req.uri.full.toLowerCase() == "/cstat") {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody(formatConnStat());
res.finish();
} else {
setTimeout(function () { handleRequest(req, res); }, resDelay)
}
})
server.addListener("connection", handleConnOpen);
server.listen(8080);
//*/
setInterval(connStat, countReportDelay);
/*/
repl.scope.cstat = connStat;
repl.start();
//*/
sys.puts("Listening on port " + serverPort + " with a response delay of " + (resDelay / 1000) + "sec");