Hi all, 

I posted the following question on SO yesterday but it got little attention 
so I decided to ask here as well, I hope you don't mind:
http://stackoverflow.com/questions/26977272/how-to-measure-http-clientrequest-response-time-in-nodejs

Here's my problem:


I'm creating http requests from my NodeJS application like this:

var start;
var req = http.request(options, function (res) {
    res.setEncoding('utf8');

    var body = '';
    res.on('data', function (chunk) {
        body += chunk;
    });
    res.on('end', function () {
        var elapsed = process.hrtime(start)[1] / 1000000; // in milliseconds
        console.log(elapsed);
        // whatever
    });});

req.on('socket', function (res) {
    start = process.hrtime();});

req.on('error', function (res) {
    // whatever});
if (props.data) req.write(props.data);
req.end();

I want to find out how long my requests take - starting from (or the 
closest I can get to) the moment the request has been over the wire (and 
not the moment that the promise has been created) and up to the moment that 
"response end" event kicked in.

I'm having a bit of trouble finding out the closest moment / event which I 
could hook to to start measuring the time. Http client *socket event 
<http://nodejs.org/api/http.html#http_event_socket>* is my best bet so far 
but its description:

Emitted after a socket is assigned to this request.

doesn't really tell whether that's the event I'm looking for.. So, my 
question is - am I doing this right or is there a better event I could use 
(or even a better way of doing the whole thing)? Thanks.


So if anyone is able to help - I am most grateful for any feedback / help. 
:)


Regards,

Tihomir

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/6734dfa2-aec8-4cc9-98c5-7a05bab448e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to