2012-07-26 00:10:40 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2011-10-13 21:24:25 +08:00
|
|
|
// small benchmark to execute with nodejs
|
|
|
|
|
|
|
|
var http = require('http')
|
|
|
|
|
2012-07-26 00:10:40 +08:00
|
|
|
if ( process.argv.length < 3 ) {
|
|
|
|
console.error("Usage: " + process.argv[1] + " <baseurl> [<map_key>]");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseurl = process.argv[2];
|
|
|
|
console.log("Baseurl is " + baseurl);
|
|
|
|
var baseurl_comps = baseurl.match(/(https?:\/\/)?([^:\/]*)(:([^\/]*))?(\/.*).*/);
|
|
|
|
|
2011-10-13 21:24:25 +08:00
|
|
|
var options = {
|
2012-07-26 00:10:40 +08:00
|
|
|
host: baseurl_comps[2],
|
|
|
|
port: baseurl_comps[4] ? baseurl_comps[4] : 8181,
|
|
|
|
path: baseurl_comps[5] + '/{z}/{x}/{y}.png?cache_buster=0'
|
2011-10-13 21:24:25 +08:00
|
|
|
};
|
|
|
|
|
2012-07-26 00:10:40 +08:00
|
|
|
if ( process.argv.length > 3 ) {
|
|
|
|
options.path += '&map_key=' + process.argv[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
console.dir(options);
|
|
|
|
|
2011-10-13 21:24:25 +08:00
|
|
|
function randInt(min, max) {
|
|
|
|
return min + Math.floor(Math.random()*(max- min +1));
|
|
|
|
}
|
|
|
|
|
|
|
|
var start_time = new Date().getTime();
|
|
|
|
function end() {
|
|
|
|
var end_time = new Date().getTime();
|
|
|
|
var t = (end_time - start_time)/1000;
|
|
|
|
console.log("ok: ", ok)
|
|
|
|
console.log("error: ", error)
|
|
|
|
console.log("time: ", t);
|
|
|
|
console.log("req/s: ", ok/t);
|
|
|
|
}
|
|
|
|
|
|
|
|
var N = 1000
|
|
|
|
var ok = 0;
|
|
|
|
var error = 0;
|
2012-07-26 00:10:40 +08:00
|
|
|
|
|
|
|
function fail(msg) {
|
|
|
|
console.log(msg);
|
|
|
|
++ error;
|
|
|
|
if ( error + ok === N ) end();
|
|
|
|
}
|
|
|
|
|
|
|
|
function pass() {
|
|
|
|
++ ok;
|
|
|
|
if ( error + ok === N ) end();
|
|
|
|
}
|
|
|
|
|
2011-10-13 21:24:25 +08:00
|
|
|
for(var i = 0; i < N; ++i) {
|
|
|
|
var opt = {
|
|
|
|
host: options.host,
|
|
|
|
port: options.port,
|
|
|
|
path: new String(options.path)
|
|
|
|
};
|
|
|
|
opt.path = opt.path.replace('{z}', 2).replace('{x}', randInt(0, 3)).replace('{y}', randInt(0, 3));
|
2012-07-26 00:10:40 +08:00
|
|
|
//console.log(opt.path)
|
2011-10-13 21:24:25 +08:00
|
|
|
http.get(opt, function(res) {
|
2012-07-26 00:10:40 +08:00
|
|
|
res.body = '';
|
|
|
|
res.on('data', function(chunk) {
|
|
|
|
// Save only first chunk, to reduce cost of the operation
|
|
|
|
if ( res.body.length == 0 ) res.body += chunk;
|
|
|
|
});
|
|
|
|
res.on('end', function() {
|
|
|
|
if ( res.statusCode == 200 ) pass();
|
|
|
|
else {
|
|
|
|
fail(res.statusCode + ' http://' + opt.host + ':' + opt.port + opt.path + ' ' + res.body);
|
|
|
|
}
|
|
|
|
});
|
2011-10-13 21:24:25 +08:00
|
|
|
}).on('error', function(e) {
|
2012-07-26 00:10:40 +08:00
|
|
|
fail('unknown (http) error');
|
2011-10-13 21:24:25 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|