UNPKG

3.08 kBMarkdownView Raw
1# http-timer
2> Timings for HTTP requests
3
4[![Build Status](https://travis-ci.org/szmarczak/http-timer.svg?branch=master)](https://travis-ci.org/szmarczak/http-timer)
5[![Coverage Status](https://coveralls.io/repos/github/szmarczak/http-timer/badge.svg?branch=master)](https://coveralls.io/github/szmarczak/http-timer?branch=master)
6[![install size](https://packagephobia.now.sh/badge?p=@szmarczak/http-timer)](https://packagephobia.now.sh/result?p=@szmarczak/http-timer)
7
8Inspired by the [`request` package](https://github.com/request/request).
9
10## Installation
11
12NPM:
13
14> `npm install @szmarczak/http-timer`
15
16Yarn:
17
18> `yarn add @szmarczak/http-timer`
19
20## Usage
21```js
22const https = require('https');
23const timer = require('@szmarczak/http-timer');
24
25const request = https.get('https://httpbin.org/anything');
26const timings = timer(request);
27
28request.on('response', response => {
29 response.on('data', () => {}); // Consume the data somehow
30 response.on('end', () => {
31 console.log(timings);
32 });
33});
34
35// {
36// start: 1572712180361,
37// socket: 1572712180362,
38// lookup: 1572712180415,
39// connect: 1572712180571,
40// upload: 1572712180884,
41// response: 1572712181037,
42// end: 1572712181039,
43// error: null,
44// phases: {
45// wait: 1,
46// dns: 53,
47// tcp: 156,
48// request: 313,
49// firstByte: 153,
50// download: 2,
51// total: 678
52// }
53// }
54```
55
56## API
57
58### timer(request)
59
60Returns: `Object`
61
62- `start` - Time when the request started.
63- `socket` - Time when a socket was assigned to the request.
64- `lookup` - Time when the DNS lookup finished.
65- `connect` - Time when the socket successfully connected.
66- `secureConnect` - Time when the socket securely connected.
67- `upload` - Time when the request finished uploading.
68- `response` - Time when the request fired the `response` event.
69- `end` - Time when the response fired the `end` event.
70- `error` - Time when the request fired the `error` event.
71- `phases`
72 - `wait` - `timings.socket - timings.start`
73 - `dns` - `timings.lookup - timings.socket`
74 - `tcp` - `timings.connect - timings.lookup`
75 - `tls` - `timings.secureConnect - timings.connect`
76 - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
77 - `firstByte` - `timings.response - timings.upload`
78 - `download` - `timings.end - timings.response`
79 - `total` - `timings.end - timings.start` or `timings.error - timings.start`
80
81If something has not been measured yet, it will be `undefined`.
82
83**Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
84
85You can also access the timings through `request.timings` or `response.timings`:
86
87```js
88const https = require('https');
89const timer = require('@szmarczak/http-timer');
90
91const request = https.get('https://httpbin.org/anything');
92const timings = timer(request);
93
94console.log(request.timings === timings);
95// => true
96
97request.on('response', response => {
98 response.on('data', () => {}); // Consume the data somehow
99 response.on('end', () => {
100 console.log(response.timings === timings);
101 // => true
102 });
103});
104```
105
106## License
107
108MIT