UNPKG

2.63 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');
26timer(request);
27
28request.once('response', response => {
29 response.resume();
30 response.once('end', () => {
31 console.log(response.timings); // You can use `request.timings` as well
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: undefined,
44// abort: undefined,
45// phases: {
46// wait: 1,
47// dns: 53,
48// tcp: 156,
49// request: 313,
50// firstByte: 153,
51// download: 2,
52// total: 678
53// }
54// }
55```
56
57## API
58
59### timer(request)
60
61Returns: `Object`
62
63**Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
64
65- `start` - Time when the request started.
66- `socket` - Time when a socket was assigned to the request.
67- `lookup` - Time when the DNS lookup finished.
68- `connect` - Time when the socket successfully connected.
69- `secureConnect` - Time when the socket securely connected.
70- `upload` - Time when the request finished uploading.
71- `response` - Time when the request fired `response` event.
72- `end` - Time when the response fired `end` event.
73- `error` - Time when the request fired `error` event.
74- `abort` - Time when the request fired `abort` event.
75- `phases`
76 - `wait` - `timings.socket - timings.start`
77 - `dns` - `timings.lookup - timings.socket`
78 - `tcp` - `timings.connect - timings.lookup`
79 - `tls` - `timings.secureConnect - timings.connect`
80 - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
81 - `firstByte` - `timings.response - timings.upload`
82 - `download` - `timings.end - timings.response`
83 - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
84
85If something has not been measured yet, it will be `undefined`.
86
87## License
88
89MIT