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 | ```js
|
22 | 'use strict';
|
23 | const https = require('https');
|
24 | const timer = require('@szmarczak/http-timer').default;
|
25 |
|
26 | const request = https.get('https://httpbin.org/anything');
|
27 | const timings = timer(request);
|
28 |
|
29 | request.on('response', response => {
|
30 | response.on('data', () => {}); // Consume the data somehow
|
31 | response.on('end', () => {
|
32 | console.log(timings);
|
33 | });
|
34 | });
|
35 |
|
36 | // { start: 1535708511443,
|
37 | // socket: 1535708511444,
|
38 | // lookup: 1535708511444,
|
39 | // connect: 1535708511582,
|
40 | // upload: 1535708511887,
|
41 | // response: 1535708512037,
|
42 | // end: 1535708512040,
|
43 | // phases:
|
44 | // { wait: 1,
|
45 | // dns: 0,
|
46 | // tcp: 138,
|
47 | // request: 305,
|
48 | // firstByte: 150,
|
49 | // download: 3,
|
50 | // total: 597 } }
|
51 | ```
|
52 |
|
53 | ## API
|
54 |
|
55 | ### timer(request)
|
56 |
|
57 | Returns: `Object`
|
58 |
|
59 | - `start` - Time when the request started.
|
60 | - `socket` - Time when a socket was assigned to the request.
|
61 | - `lookup` - Time when the DNS lookup finished.
|
62 | - `connect` - Time when the socket successfully connected.
|
63 | - `upload` - Time when the request finished uploading.
|
64 | - `response` - Time when the request fired the `response` event.
|
65 | - `end` - Time when the response fired the `end` event.
|
66 | - `error` - Time when the request fired the `error` event.
|
67 | - `phases`
|
68 | - `wait` - `timings.socket - timings.start`
|
69 | - `dns` - `timings.lookup - timings.socket`
|
70 | - `tcp` - `timings.connect - timings.lookup`
|
71 | - `request` - `timings.upload - timings.connect`
|
72 | - `firstByte` - `timings.response - timings.upload`
|
73 | - `download` - `timings.end - timings.response`
|
74 | - `total` - `timings.end - timings.start` or `timings.error - timings.start`
|
75 |
|
76 | If something is not measured yet, it will be `undefined`.
|
77 |
|
78 | **Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
79 |
|
80 | ## License
|
81 |
|
82 | MIT
|