1.0.1 • Published 5 years ago

http-duration-client v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

http-duration-client

Measure duration (in milliseconds) for Node HTTP lifecycle events from various phases of HTTP:

Http lifecycle

  • DNS lookup
  • TCP/Socket connect
  • TLS connect
  • First byte
  • Content transfer
  • Total request

Build Status

Development

  1. To check the HTTP lifecycle events, clone this repository
git clone git@github.com:congruencelabs/http-duration-client.git
  1. Install the package dependencies using yarn
yarn install
  1. Run the examples/client-with-duration.js file to see the lifecycle events being logged for a request to https://api.github.com/users

Usage

  1. Install the http-duration-client using npm or yarn
npm install http-duration-client

or

yarn add http-duration-client
  1. Make an http request to an endpoint as shown in example below
const { requestWithDuration } = require('http-duration-client');

requestWithDuration({
        url: "https://api.github.com/users",
        headers: {
            'User-Agent': 'Example'
        },
        timeout: 500
    }), (err, res) => {
        if(err) {
            console.error(err);
        } else {
            console.log(res.duration);
        }
});

The result of the above should look similar to

{ 
    "dnsLookup": 62.253469,
    "tcpConnection": 20.589025,
    "tlsHandshake": 44.643525,
    "firstByte": 490.091997,
    "contentTransfer": 15.480998,
    "total": 633.059014
}

You can also use it with async and await requestWithDuration() or requestWithDuration().then()

(async () => {
    try {
        const resp = await requestWithDuration(
            {
              url: "https://api.github.com/users",
              headers: {
                "User-Agent": "Example"
              },
              timeout: 500
            },
        );
        console.log(resp.duration)
    } catch(err) {
        console.error(err)
    }
})()

The result of the above should look similar to

{ 
    "dnsLookup": 62.253469,
    "tcpConnection": 20.589025,
    "tlsHandshake": 44.643525,
    "firstByte": 490.091997,
    "contentTransfer": 15.480998,
    "total": 633.059014
}