UNPKG

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