I am new to node. I want to ask, is it possible to send http request from node.js to any server with a concurrency of 10,000 per second? Without putting the code in a for loop and running it for 10000 times.
Something what apache benchmark might do but in node.js
Some time ago i wrote a node.js script that has the basic functionality of ApacheBench:
(lines marked with // #
should be changed to your needs)
var http=require('http');
var inp_num_requests=20000; // #
var inp_concurrency =10000; // #
var options={
host:'127.0.0.1', // #
port:80, // #
path:'/', // #
method:'GET',
agent:false
};
var active=0;
var num_reqests=0;
var errors=0;
var total_bytes=0;
var last=-1;
var timer=setInterval(function(){
if (last!=num_reqests) console.log(num_reqests+'/'+inp_num_requests,'err:',errors);
last=num_reqests;
},1000);
var start_time=+new Date;
function checkEnd(){
if (num_reqests<inp_num_requests) {
makeRequest();
}else if(!active){
clearInterval(timer);
console.log('\nFinished '+num_reqests+' requests');
var took=((+new Date)-start_time)/1000;
console.log(
'Time taken for tests: '+took+' seconds\n'+
'Complete requests: '+num_reqests+'\n'+
'Failed requests: '+errors+'\n'+
'HTML transferred: '+total_bytes+' bytes\n'+
'Requests per second: '+(num_reqests/took)+' [#/sec]\n'+
'Time per request: '+(took/num_reqests*1000)+' [ms] (mean, across all concurrent requests)\n'
);
}
}
function makeRequest(){
active++;
num_reqests++;
var r=http.request(options,function(res){
res.on('data',function(chunk){
total_bytes+=chunk.length;
});
res.once('end',function(){
active--;
checkEnd();
});
});
r.once('error',function(e){
if ('ECONNRESET'!=e.code) {
console.log(e);
process.exit();
}
errors++;
active--;
checkEnd();
});
if (active<inp_concurrency) makeRequest();
r.end();
}
makeRequest();
Example output:
10000/20000 err: 0
11622/20000 err: 0
12036/20000 err: 0
20000/20000 err: 0
Finished 20000 requests
Time taken for tests: 10.817 seconds
Complete requests: 20000
Failed requests: 0
HTML transferred: 2037900 bytes
Requests per second: 1848.9414810021262 [#/sec]
Time per request: 0.54085 [ms] (mean, across all concurrent requests)
Error: connect EMFILE
(too many open files) you have to increase the max. allowed number of open files -> ulimit
I guess you could repeat the http request call 10 000 times if you don't want to use a for loop. It would probably work. Whether it's a good idea, well, not so sure.