Last active
March 27, 2019 08:16
-
-
Save krisanalfa/8359401bd5930d63a5385afecdc33d39 to your computer and use it in GitHub Desktop.
This file contains 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
// @ts-check | |
const https = require('https') | |
/** | |
* @param {[number, number]} hrtime | |
* | |
* @returns {string} | |
*/ | |
const parseHrtimeToSeconds = hrtime => (hrtime[0] + (hrtime[1] / 1e9)).toFixed(5) | |
const url = process.argv.pop() | |
console.info('Requesting %s ...', url) | |
const timings = { | |
dns: '', | |
tcp: '', | |
tls: '', | |
ttfb: '', | |
total: '' | |
} | |
const start = process.hrtime() | |
const req = https.get(url, response => { | |
response.once('readable', () => { | |
timings.ttfb = parseHrtimeToSeconds(process.hrtime(start)) | |
}) | |
response.on('data', () => {}) | |
response.on('end', () => { | |
timings.total = parseHrtimeToSeconds(process.hrtime(start)) | |
}) | |
response.on('close', () => console.table(timings)) | |
}) | |
req.on('socket', socket => { | |
socket.on('lookup', () => { | |
timings.dns = parseHrtimeToSeconds(process.hrtime(start)) | |
}) | |
socket.on('connect', () => { | |
timings.tcp = parseHrtimeToSeconds(process.hrtime(start)) | |
}) | |
socket.on('secureConnect', () => { | |
timings.tls = parseHrtimeToSeconds(process.hrtime(start)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Example: