项目作者: JoshCrozier

项目描述 :
A lightweight, dependency-free Node.js module for timing HTTP/HTTPS requests; useful for determining the duration of HTTPS phases: DNS Lookup, TCP Connection, TLS Handshake, TTFB, and Content Transfer
高级语言: JavaScript
项目地址: git://github.com/JoshCrozier/https-timer.git
创建时间: 2018-12-27T05:31:30Z
项目社区:https://github.com/JoshCrozier/https-timer

开源协议:MIT License

下载


https-timer

npm package

NPM version
Build status
Coverage
Vulnerabilities

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

  1. $ npm install https-timer --save

Basic Usage

  1. const httpsTimer = require('https-timer');
  2. httpsTimer.get('https://www.google.com', (error, response) => {
  3. if (!error && response) {
  4. console.log(response.timing); // Prints the timing durations below
  5. }
  6. });

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:

  1. {
  2. "durations": {
  3. "socketOpen": 1.579389,
  4. "dnsLookup": 39.922508,
  5. "tcpConnection": 28.770425,
  6. "tlsHandshake": 218.159047,
  7. "firstByte": 148.640706,
  8. "contentTransfer": 1.954565,
  9. "total": 439.02664
  10. }
  11. }

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:

  1. const httpsTimer = require('https-timer');
  2. const options = {
  3. url: 'https://api.github.com/repos/JoshCrozier/https-timer',
  4. headers: {
  5. 'User-Agent': 'HTTPS Request Timer'
  6. }
  7. };
  8. httpsTimer.get(options, (error, response) => {
  9. if (!error && response && response.statusCode === 200) {
  10. console.log('Response body: ', JSON.parse(response.body));
  11. console.log('Response Timing: ', response.timing);
  12. } else {
  13. console.log('Request error: ', error);
  14. }
  15. });

Promises and Async/Await

The get and request methods also have async equivalents: getAsync and requestAsync respectively.

Promise usage:

  1. const httpsTimer = require('https-timer');
  2. httpsTimer.getAsync('https://www.google.com').then(response => {
  3. console.log(response.timing);
  4. });

Async/Await usage:

  1. const httpsTimer = require('https-timer');
  2. const response = await httpsTimer.getAsync('https://www.google.com');
  3. console.log(response.timing);

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:

  1. $ npm install -g time-request

Usage:

  1. $ time-request https://google.com

Example Output:

  1. Request Phase Duration
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. Socket Open 1.61 ms
  4. DNS Lookup 34.15 ms
  5. TCP Connection 47.69 ms
  6. TLS Handshake 102.25 ms
  7. Time to First Byte 67.23 ms
  8. Content Transfer 1.69 ms
  9. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. Total 254.62 ms

License

MIT License

Copyright (c) 2016-2019 Josh Crozier