Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@szmarczak/http-timer
Advanced tools
@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.
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);
});
});
Axios is a popular HTTP client for Node.js and the browser. It supports request and response interceptors, automatic transforms for JSON data, and more. While it does not provide detailed timing information out of the box, it can be extended with interceptors to measure request timings.
Request is a simplified HTTP client for Node.js. It is known for its ease of use and flexibility. Although it does not natively support detailed timing information, it can be combined with other packages or custom code to achieve similar functionality.
Got is a lightweight and powerful HTTP request library for Node.js. It includes built-in support for retries, streams, and hooks. Got also provides detailed timing information similar to @szmarczak/http-timer, making it a strong alternative.
Timings for HTTP requests
Inspired by the request
package.
NPM:
npm install @szmarczak/http-timer
Yarn:
yarn add @szmarczak/http-timer
'use strict';
const https = require('https');
const timer = require('@szmarczak/http-timer').default;
const request = https.get('https://httpbin.org/anything');
const timings = timer(request);
request.on('response', response => {
response.on('data', () => {}); // Consume the data somehow
response.on('end', () => {
console.log(timings);
});
});
// { start: 1535708511443,
// socket: 1535708511444,
// lookup: 1535708511444,
// connect: 1535708511582,
// upload: 1535708511887,
// response: 1535708512037,
// end: 1535708512040,
// phases:
// { wait: 1,
// dns: 0,
// tcp: 138,
// request: 305,
// firstByte: 150,
// download: 3,
// total: 597 } }
Returns: Object
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.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.lookup - timings.socket
tcp
- timings.connect - timings.lookup
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 elapsed since the UNIX epoch.
MIT
FAQs
Timings for HTTP requests
The npm package @szmarczak/http-timer receives a total of 13,661,564 weekly downloads. As such, @szmarczak/http-timer popularity was classified as popular.
We found that @szmarczak/http-timer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.