Skip to content

Commit 05d6319

Browse files
committed
Add benchmark scripts.
To use the benchmarks: node benchmarks/run.js or: make benchmark The numbers reported are the elapsed milliseconds the script took to complete. Currently only benching HTTP code and timers.
1 parent bf6a457 commit 05d6319

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

benchmark/process_loop.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function next (i) {
2+
if (i <= 0) return;
3+
4+
var process = node.createProcess("echo hello");
5+
6+
process.addListener("output", function (chunk) {
7+
if (chunk) print(chunk);
8+
});
9+
10+
process.addListener("exit", function (code) {
11+
if (code != 0) node.exit(-1);
12+
next(i - 1);
13+
});
14+
}
15+
16+
next(500);

benchmark/run.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var benchmarks = [ "static_http_server.js"
2+
, "timers.js"
3+
, "process_loop.js"
4+
];
5+
6+
var benchmark_dir = node.path.dirname(__filename);
7+
8+
function exec (script, callback) {
9+
var command = ARGV[0] + " " + node.path.join(benchmark_dir, script);
10+
var start = new Date();
11+
var process = node.createProcess(command);
12+
process.addListener("exit", function (code) {
13+
var elapsed = new Date() - start;
14+
callback(elapsed, code);
15+
});
16+
}
17+
18+
function runNext (i) {
19+
if (i >= benchmarks.length) return;
20+
print(benchmarks[i] + ": ");
21+
exec(benchmarks[i], function (elapsed, code) {
22+
if (code != 0) {
23+
puts("ERROR ");
24+
}
25+
puts(elapsed);
26+
runNext(i+1);
27+
});
28+
};
29+
runNext(0);

benchmark/static_http_server.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var concurrency = 30;
2+
var nrequests = 700;
3+
var port = 8000;
4+
var completed_requests = 0;
5+
var bytes = 1024*5;
6+
7+
var body = "";
8+
for (var i = 0; i < bytes; i++) {
9+
body += "C";
10+
}
11+
12+
var server = node.http.createServer(function (req, res) {
13+
res.sendHeader(200, [
14+
["Content-Type", "text/plain"],
15+
["Content-Length", body.length]
16+
]);
17+
res.sendBody(body);
18+
res.finish();
19+
})
20+
server.listen(port);
21+
22+
function responseListener (res) {
23+
res.addListener("complete", function () {
24+
//puts("response " + completed_requests + " from client " + res.client.id);
25+
if (completed_requests++ < nrequests) {
26+
res.client.get("/").finish(responseListener);
27+
} else {
28+
server.close();
29+
}
30+
});
31+
}
32+
33+
function onLoad () {
34+
for (var i = 0; i < concurrency; i++) {
35+
var client = node.http.createClient(port);
36+
client.id = i;
37+
client.get("/").finish(responseListener);
38+
}
39+
}

benchmark/timers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function next (i) {
2+
if (i <= 0) return;
3+
setTimeout(function () { next(i-1); }, 1);
4+
}
5+
next(700);

configure

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ test: all
100100
test-all: all
101101
python tools/test.py --mode=debug,release
102102
103+
test-debug: all
104+
python tools/test.py --mode=release,debug
105+
106+
benchmark: all
107+
build/default/node benchmark/run.js
108+
103109
website: website/api.html website/index.html
104110
105111
website/api.html: website/api.txt
@@ -123,7 +129,7 @@ check:
123129
dist:
124130
@$WAF dist
125131
126-
.PHONY: clean dist distclean check uninstall install all test test-all website website-upload
132+
.PHONY: benchmark clean dist distclean check uninstall install all test test-all website website-upload
127133
128134
EOF
129135
}

0 commit comments

Comments
 (0)