Last active
January 17, 2018 23:07
-
-
Save unigazer/5de4194976fd90b9be66e7d38c797911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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