简体   繁体   中英

How do I profile outgoing http requests in node.js?

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?

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()

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM