Node.js concurrency of 10,000

I am new to node. I want to ask if it is possible to send an 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 10,000 times.

Something apache test can do, but in node.js

+3
source share
2 answers

Some time ago I wrote a node.js script that has the basic ApacheBench functionality:

(lines marked // #should be changed according 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();

Output Example:

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)


in case of the following error: Error: connect EMFILE(too many open files) you need to increase max. allowed number of open files -> ulimit
+4
source

, http- 10 000 , for. , , . , , .

0

All Articles