https-timer
A lightweight, dependency-free Node.js module for timing HTTP/HTTPS requests.
Useful for determining the duration of different HTTPS phases:
- Socket Initialization
- DNS Lookup
- TCP Connection
- TLS Handshake
- Time to First Byte
- Content Transfer
Used on PingMe.io for testing website latency.
Installation
$ npm install https-timer --save
Basic Usage
const httpsTimer = ; httpsTimer;
When a request has ended, a timing
object is added to the response
object.
Here is an example snapshot of the timing
object. The timing durations are in milliseconds:
Request with custom options
Since httpsTimer
utilizes the native Node.js http
and https
modules, you can pass an options
object when making a request:
const httpsTimer = ; const options = url: 'https://api.github.com/repos/JoshCrozier/https-timer' headers: 'User-Agent': 'HTTPS Request Timer' ; httpsTimer;
Promises and Async/Await
The get
and request
methods also have async equivalents: getAsync
and requestAsync
respectively.
Promise usage:
const httpsTimer = ; httpsTimer;
Async/Await usage:
const httpsTimer = ; const response = await httpsTimer; console;
For more detailed examples with error handling, see the examples directory.
Command-Line Usage
If you prefer to time requests directly from the command-line with preformatted output, you can alternatively install the time-request package:
$ npm install -g time-request
Usage:
$ time-request https://google.com
Example Output:
Request Phase Duration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Socket Open 1.61 ms
DNS Lookup 34.15 ms
TCP Connection 47.69 ms
TLS Handshake 102.25 ms
Time to First Byte 67.23 ms
Content Transfer 1.69 ms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Total 254.62 ms
License
Copyright (c) 2016-2019 Josh Crozier