Original

Some Basic Concepts:

  • IP(Internet Protocol): IP is a network-layer protocol, deals with network addressing and routing. IP is responsible for delivering packets from the source host to the destination host based on the packet headers across one or more IP network. It also defines packet structures that encapsulate the data to be delivered.

  • DNS(Domain Name Server): DNS is a hierarchical decentralized naming system used to resolve human-readable hostnames into machine-readable IP address.

  • TCP(Transmission Control Protocol): The TCP standard defines how to establish and maintain a network conversation between applications to exchange data. TCP provides reliable, ordered, and error-checked deliver of a stream of octects between applications running on hosts communicating over an IP network. An HTTP client initiates a request by establishing a TCP connection.

  • SSL(Secure Sockets Layer)/TLS(Transport Layer Security): TLS is a cryptographic protocol that provides communications security over a computer network. SSL is a deprecated predecessor to TLS. Both TLS and SSL use certificates to establish a secure connection. SSL certificates are not dependent on cryptographic protocols like TLS, a certificate contains a key pair: a public and a private key.

Timings Explained:

  • DNS Lookup: Time spent performing the DNS Lookup. DNS lookup resolves domain names to IP addresses.

  • TCP Connection: Time it took to establish TCP Connection between a source host and destination host. Connections must be properly established in a multi-step handshake process.

  • TLS handshake: Time spent completing a TLS handshake. During the handshake process endpoints exchange authentication and keys to establish or resume secure sessions. There is no TLS handshake with a not HTTPS request.

  • Time To First Byte(TTFB): Time spent waiting for the initial response. This time captures the latency of a round trip to the server in addition to the time spent waiting for the server to process the request and deliver the response.

  • Content Transfer: Time spent receiving the response data. The size of the response data and the available network bandwidth determinates its duration.

Measuring HTTP timings in Node.js

To measure HTTP timings in Node.js, we need to subscribe to a specific request, response and socket events. Here is a short code snippet how to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const timings = {
startAt: process.hrtime(),
dnsLookupAt: undefined,
tcpConnectionAt: undefined,
tlsHandshakeAt: undefined,
firstByteAt: undefined,
endAt: undefined,
}

const req = http.request({...}, (res) => {
res.once('readable', () => {
timings.firstByteAt = process.hrtime()
})
res.on('data', (chunk) => {
responseBody += chunk
})
res.on('end', () => {
timings.endAt = process.hrtime()
})
})

req.on('socket', (socket) => {
socket.on('lookup', () => {
timings.dnsLookupAt = process.hrtime()
})
socket.('connect', () => {
timings.tcpConnectionAt = process.hrtime()
})
socket.on('secureConnect, () => {
timings.tlsHandshake = process.hrtime()
})
})