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 |
|
8 | Inspired by the [`request` package](https://github.com/request/request).
|
9 |
|
10 | ## Installation
|
11 |
|
12 | NPM:
|
13 |
|
14 | > `npm install @szmarczak/http-timer`
|
15 |
|
16 | Yarn:
|
17 |
|
18 | > `yarn add @szmarczak/http-timer`
|
19 |
|
20 | ## Usage
|
21 | **Note:**
|
22 | > - The measured events resemble Node.js events, not the kernel ones.
|
23 | > - Sending a chunk greater than [`highWaterMark`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options) will result in invalid `upload` and `response` timings. You can avoid this by splitting the payload into smaller chunks.
|
24 |
|
25 | ```js
|
26 | const https = require('https');
|
27 | const timer = require('@szmarczak/http-timer');
|
28 |
|
29 | const request = https.get('https://httpbin.org/anything');
|
30 | timer(request);
|
31 |
|
32 | request.once('response', response => {
|
33 | response.resume();
|
34 | response.once('end', () => {
|
35 | console.log(response.timings); // You can use `request.timings` as well
|
36 | });
|
37 | });
|
38 |
|
39 | // {
|
40 | // start: 1572712180361,
|
41 | // socket: 1572712180362,
|
42 | // lookup: 1572712180415,
|
43 | // connect: 1572712180571,
|
44 | // upload: 1572712180884,
|
45 | // response: 1572712181037,
|
46 | // end: 1572712181039,
|
47 | // error: undefined,
|
48 | // abort: undefined,
|
49 | // phases: {
|
50 | // wait: 1,
|
51 | // dns: 53,
|
52 | // tcp: 156,
|
53 | // request: 313,
|
54 | // firstByte: 153,
|
55 | // download: 2,
|
56 | // total: 678
|
57 | // }
|
58 | // }
|
59 | ```
|
60 |
|
61 | ## API
|
62 |
|
63 | ### timer(request)
|
64 |
|
65 | Returns: `Object`
|
66 |
|
67 | **Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
68 |
|
69 | - `start` - Time when the request started.
|
70 | - `socket` - Time when a socket was assigned to the request.
|
71 | - `lookup` - Time when the DNS lookup finished.
|
72 | - `connect` - Time when the socket successfully connected.
|
73 | - `secureConnect` - Time when the socket securely connected.
|
74 | - `upload` - Time when the request finished uploading.
|
75 | - `response` - Time when the request fired `response` event.
|
76 | - `end` - Time when the response fired `end` event.
|
77 | - `error` - Time when the request fired `error` event.
|
78 | - `abort` - Time when the request fired `abort` event.
|
79 | - `phases`
|
80 | - `wait` - `timings.socket - timings.start`
|
81 | - `dns` - `timings.lookup - timings.socket`
|
82 | - `tcp` - `timings.connect - timings.lookup`
|
83 | - `tls` - `timings.secureConnect - timings.connect`
|
84 | - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
|
85 | - `firstByte` - `timings.response - timings.upload`
|
86 | - `download` - `timings.end - timings.response`
|
87 | - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
|
88 |
|
89 | If something has not been measured yet, it will be `undefined`.
|
90 |
|
91 | ## License
|
92 |
|
93 | MIT
|