You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@szmarczak/http-timer

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@szmarczak/http-timer

Timings for HTTP requests


Version published
Weekly downloads
12M
decreased by-18.62%
Maintainers
1
Install size
17.2 kB
Created
Weekly downloads
 

Package description

What is @szmarczak/http-timer?

@szmarczak/http-timer is an npm package designed to measure the timing of HTTP requests. It provides detailed timing information for various phases of an HTTP request, such as DNS lookup, TCP connection, and response time. This can be useful for performance monitoring and debugging.

What are @szmarczak/http-timer's main functionalities?

Measure HTTP request timings

This feature allows you to measure the timings of an HTTP request. The code sample demonstrates how to use the package to measure and log the timings of a GET request to 'http://example.com'.

const http = require('http');
const timer = require('@szmarczak/http-timer');

const request = http.get('http://example.com', response => {
  timer(request);
  response.on('end', () => {
    console.log(request.timings);
  });
});

Detailed timing phases

This feature provides detailed timing information for different phases of an HTTP request. The code sample shows how to log the DNS lookup time, TCP connection time, time to first byte, download time, and total time for a GET request.

const http = require('http');
const timer = require('@szmarczak/http-timer');

const request = http.get('http://example.com', response => {
  timer(request);
  response.on('end', () => {
    console.log('DNS Lookup Time:', request.timings.dns);
    console.log('TCP Connection Time:', request.timings.tcp);
    console.log('First Byte Time:', request.timings.firstByte);
    console.log('Download Time:', request.timings.download);
    console.log('Total Time:', request.timings.total);
  });
});

Other packages similar to @szmarczak/http-timer

Readme

Source

http-timer

Timings for HTTP requests

Build Status Coverage Status install size

Inspired by the request package.

Installation

NPM:

npm install @szmarczak/http-timer

Yarn:

yarn add @szmarczak/http-timer

Usage

Note:

  • The measured events resemble Node.js events, not the kernel ones.
  • Sending a chunk greater than highWaterMark will result in invalid upload and response timings. You can avoid this by splitting the payload into smaller chunks.
import https from 'https';
import timer from '@szmarczak/http-timer';

const request = https.get('https://httpbin.org/anything');
timer(request);

request.once('response', response => {
	response.resume();
	response.once('end', () => {
		console.log(response.timings); // You can use `request.timings` as well
	});
});

// {
//   start: 1572712180361,
//   socket: 1572712180362,
//   lookup: 1572712180415,
//   connect: 1572712180571,
//   upload: 1572712180884,
//   response: 1572712181037,
//   end: 1572712181039,
//   error: undefined,
//   abort: undefined,
//   phases: {
//     wait: 1,
//     dns: 53,
//     tcp: 156,
//     request: 313,
//     firstByte: 153,
//     download: 2,
//     total: 678
//   }
// }

API

timer(request)

Returns: Object

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

  • start - Time when the request started.
  • socket - Time when a socket was assigned to the request.
  • lookup - Time when the DNS lookup finished.
  • connect - Time when the socket successfully connected.
  • secureConnect - Time when the socket securely connected.
  • upload - Time when the request finished uploading.
  • response - Time when the request fired response event.
  • end - Time when the response fired end event.
  • error - Time when the request fired error event.
  • abort - Time when the request fired abort event.
  • phases
    • wait - timings.socket - timings.start
    • dns - timings.lookup - timings.socket
    • tcp - timings.connect - timings.lookup
    • tls - timings.secureConnect - timings.connect
    • request - timings.upload - (timings.secureConnect || timings.connect)
    • firstByte - timings.response - timings.upload
    • download - timings.end - timings.response
    • total - (timings.end || timings.error || timings.abort) - timings.start

If something has not been measured yet, it will be undefined.

License

MIT

Keywords

FAQs

Package last updated on 20 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc