Zibi
Zibi

Reputation: 350

node.js measure response time with multiple requests

Is there a way to send multiple requests in a loop, and measure the time of each request? In python I'd do:

import requests
import time
for i in range(100):
        start = time.time()
        r = requests.get("http://localhost:80")
        end = time.time()
        total_ms = 1000 * (end - start)
        print("%.2f\n" % total_ms)

In node.js I tried:

var request = require('request');  
for(var i=0;i<100;i++){
    start = Date.now();
    request.get('http://localhost:80/', function () {
        end = Date.now();
        console.log(end-start);
    })
}

But obviously it doesn't give me what I need. Is there something I'm missing?

As for why I'm using node: I seem to get some random response spikes on our servers and I'd like to test it with something different than python and bash curls.

Upvotes: 0

Views: 1392

Answers (1)

Yuriy Anisimov
Yuriy Anisimov

Reputation: 825

If you really want to do this in nodejs here is an example:

var request = require('request');
var moment = require('moment');

    function makeRequest() {
        var startDate = moment();
        request('http://localhost', function (error,response,body) {
            var endDate = moment();
            console.log('Request took: ' + endDate.diff(startDate) + ' ms.');

        });
    }

    for(var i = 0; i < 100; i++){
        makeRequest();
    }

But I'd rather suggest to use stress load tools for such purpose like JMeter.

Upvotes: 2

Related Questions