Reputation: 28362
What's the easiest way to make an outgoing http request in node.js while tracking time it takes to: lookup DNS, connect to server, time to first byte, time to completion, etc. ?
I've tried the request
and needle
modules, but they don't seem to have this functionality. Should I hack together my own solution using plain http
module?
Upvotes: 3
Views: 1945
Reputation: 28362
Seems like request
actually supports an option to measure the timings by adding the time
option:
request = require 'request'
opts =
url: 'http://localhost/redirect.php'
time: true
request opts, (e, r, body) ->
console.log r.timingPhases
prints
{ wait: 4.005603000000008,
dns: 0.5604900000000157,
tcp: 0.2195809999999483,
firstByte: 1.0195130000000177,
download: 0.7950860000000262,
total: 6.600273000000016 }
Upvotes: 2
Reputation: 19428
request
wraps the http
module so you can use the 'socket'
event emitted by the request object to listen for net.Socket
events.
Based on your question you can listen for the'lookup'
, 'connect'
, 'data'
and 'close'
events on the Socket and use console.time()
and console.timeEnd()
to keep track of the time taken for the operation.
// You can use request from npm
const request = require('request')
// Or the native http/https module
// const {request} = require('https')
// Store the http.ClientRequest object returned
const req = request('https://www.google.com')
// Listen for the 'socket' event and then attach listeners to the socket
req.on('socket', socket => {
console.time('dnsInfo')
socket.once('lookup', (err, address, family, host) => {
console.timeEnd('dnsInfo')
console.log(address, family, host)
})
socket.once('connect', () => {
console.time('requestTime')
console.time('initialData')
console.log('Connected to server')
})
socket.once('data', chunk => console.timeEnd('initialData'))
socket.once('close', () => console.timeEnd('requestTime'))
})
req.end()
Upvotes: 3