Giter Site home page Giter Site logo

request-time's Introduction

request-time

Timings for HTTP requests

Understanding and measuring HTTP timings helps us to discover performance. bottlenecks in client to server or server to server communication.

Request Timing

Timings explained

  • DNS Lookup: Time spent performing the DNS lookup. DNS lookup resolves domain names to IP addresses. Every new domain requires a full round trip to do the DNS lookup.

    • There is no DNS lookup when the destination is already an IP address.

  • TCP Connection: Time it took to establish TCP connection between a source host and destination host. Connections must be properly established in a multi-step handshake process. TCP connection is managed by an operating system, if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout config of our application.

  • TLS handshake: Time spent completing a TLS handshake. During the handshake process endpoints exchange authentication and keys to establish or resume secure sessions.

    • There is no TLS handshake with a not HTTPS request.

  • Time to First Byte (TTFB): Time spent waiting for the initial response. This time captures the latency of a round trip to the server in addition to the time spent waiting for the server to process the request and deliver the response.

  • Content Transfer: Time spent receiving the response data. The size of the response data and the available network bandwidth determinates its duration.

Installation

NPM:

npm install @abellsmythe/request-time

Usage

'use strict';

const https = require('https');
const timer = require('@abellsmythe/request-time').default;

const request = https.get('https://google.com');
const timings = timer(request);

request.on('response', response => {
	response.on('data', () => {
        // Do something
    });
	response.on('end', () => {
		console.log(timings);
	});
});
{
    start: 20019170.414002,
    socket: 20019171.943738,
    dnsLookup: 20019182.811348,
    connect: 20019194.637675,
    tlsHandshake: 20019245.32874,
    upload: 20019246.615841,
    response: 20019284.613933,
    end: 20019288.80115,
    error: undefined,
    phases:
    {
        wait: 1.5297359973192215,
        dns: 10.86761000007391,
        tcp: 11.826326999813318,
        request: 51.9781660027802,
        firstByte: 37.998091999441385,
        download: 4.187217000871897,
        total: 118.38714800029993
    }
}

API

timer(request)

Returns: Object

  • start - Time when the request started.
  • socket - Time when a socket was assigned to the request.
  • dnsLookup - Time when the DNS lookup finished.
  • tlsHandshake - Time when the secure connection is done.
  • connect - Time when the socket successfully connected.
  • upload - Time when the request finished uploading.
  • response - Time when the request fired the response event.
  • end - Time when the response fired the end event.
  • error - Time when the request fired the error event.
  • phases
    • wait - timings.socket - timings.start
    • dns - timings.dnsLookup - timings.socket
    • tcp - timings.connect - timings.dnsLookup
    • request - timings.upload - timings.connect
    • firstByte - timings.response - timings.upload
    • download - timings.end - timings.response
    • total - timings.end - timings.start or timings.error - timings.start

If something is not measured yet, it will be undefined.

Note: The time is a number representing the milliseconds without elapsing since the UNIX epoch.

Note: request-time uses process.hrtime() from node as it's not a subject of clock drift

request-time's People

Contributors

abellsmythe avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.