Skip to content

Instantly share code, notes, and snippets.

@unigazer
Last active January 17, 2018 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unigazer/5de4194976fd90b9be66e7d38c797911 to your computer and use it in GitHub Desktop.
Save unigazer/5de4194976fd90b9be66e7d38c797911 to your computer and use it in GitHub Desktop.
const http = require('https')
// Nanoseconds per second 1000000
const NS_PER_SEC = 1e6
// In order to convert to ms, process.hrtime() must be declared previously
const time = process.hrtime();
// Gather the data
const req = http.request(process.argv[2], res => {
res.setEncoding('utf8')
console.log(`Site HTTP: ${res.statusCode} (${res.statusMessage})`)
res.once('readable', () => {
let diff = process.hrtime(time)
console.log(`Time took to receive readable data: ${diff[1] / NS_PER_SEC} ms`);
})
// "Chunks" are pieces of data
res.on('data', chunk => {
console.log(`Recieved ${chunk.length} bytes of data chunks`)
})
res.on('end', () => {
let diff = process.hrtime(time)
console.log(`Total response: ${diff[1] / NS_PER_SEC} ms`)
})
})
// You can manipiluate the data using Node's Stream API
req.on('socket', socket => {
socket.on('lookup', () => {
let diff = process.hrtime(time)
console.log(`DNS Lookup took: ${diff[1] / NS_PER_SEC} ms`)
})
socket.on('connect', () => {
let diff = process.hrtime(time)
console.log(`TCP connection took: ${diff[1] / NS_PER_SEC} ms`)
})
socket.on('secureConnect', () => {
let diff = process.hrtime(time)
console.log(`TLS certificate confirmation took: ${diff[1] / NS_PER_SEC} ms`)
})
})
req.on('error', err => {
console.log(`Problem with: ${err.message}`)
})
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment