Hi everyone,
These days my standard first program after "hello world" when I'm
learning a new language is a clone of ApacheBench, AKA "ab". (I
recently wrote one in Scala, and I'm working on one in Go.)
So now that I'm exploring Node, I've started working on "NodeBench", or "nb".
Note that this is just for my own educational purposes, I'm not trying
to build software to be released.
Here's my current code:
——BEGIN CODE——
var sys = require("sys");
var http = require("http");
var client = http.createClient(8000, "localhost");
var start_time = new Date();
var target_requests = 5000;
var finished_successful = 0;
var finished_failed = 0;
for (var started_requests = 0;
started_requests < target_requests;
started_requests++) {
var request = client.get("/");
request.finish(
function(response) {
response.addListener("complete",
function() {
if (response.statusCode < 400)
finished_successful++;
else
finished_failed++;
if (finished_successful + finished_failed >= target_requests) {
var elapsed_time_ms = new Date() - start_time;
epilogue(elapsed_time_ms);
}
}
)
}
)
}
function epilogue(elapsed_time_ms) {
var finished_total = finished_successful + finished_failed;
var elapsed_time_secs = elapsed_time_ms / 1000;
var requests_per_sec = finished_total / elapsed_time_secs;
sys.print("\n" + finished_total + " requests completed in ");
sys.puts(elapsed_time_ms + " MS");
sys.puts("Time taken for tests: " + elapsed_time_secs + " secs");
sys.puts("Total requests: " + finished_total);
sys.puts(" successful: " + finished_successful);
sys.puts(" failed: " + finished_failed);
sys.puts("Requests per second: " + requests_per_sec);
}
——END CODE——
Here's what it prints out:
5000 requests completed in 4735 MS
Time taken for tests: 4.735 secs
Total requests: 5000
successful: 5000
failed: 0
Requests per second: 1055.9662090813092
So this is working fairly well, and so far I'm pleased.
My first question is: is there anything I'm doing particularly wrong
here? I'd love some feedback!
My second question is: I don't see any obvious way to make this
faster, but ab is currently getting this done faster:
$ ab -c 1 -n 5000
http://127.0.0.1:8000/
results in:
Time taken for tests: 2.604 seconds
Complete requests: 5000
Requests per second: 1920.27
both seem to be fully saturating both my cores (C2D 2.4).
So: is there anything I can do to make this faster? Or would this have
to be tuned in the internals of Node? Any theories on why is ab so
much faster? Is it because it doesn't run in a VM?
I appreciate any feedback!
Thanks,
Avi
--
Avi Flax » Partner » Arc90 »
http://arc90.com
➙ Have you tried Kindling‽
http://kindlingapp.com