Write a function that estimates load time from round trips.
The tea shop wants a quick estimate of when its HTML has fully arrived, before it tests on real phones. Write estimateLoad({ rttMs, mbps, htmlBytes, connection, protocol }). rttMs is the round-trip time in ms, mbps the bandwidth in megabits per second, htmlBytes the HTML's size on the wire, connection is "new" or "reused", and protocol is "h2" (HTTP/2 over TCP and TLS 1.3) or "h3" (HTTP/3 over QUIC). Return { setup, html, roundTrips, ms }, counted with exactly this model:
setup round trips before the request can leave
a new connection over "h2": 1 for the DNS lookup + 1 for TCP + 1 for TLS 1.3 = 3
a new connection over "h3": 1 for the DNS lookup + 1 for QUIC, which includes TLS 1.3 = 2
a reused connection: 0
html round trips until every byte of htmlBytes has been sent
a new connection: the request's round trip brings back the first window,
10 segments of 1,460 bytes (14,600 bytes), and each later round carries twice the one before
a reused connection: its window has already grown, so 1
a response always takes at least 1, even with no bytes
roundTrips setup + html
ms roundTrips × rttMs + htmlBytes × 8 ÷ (mbps × 1,000), rounded with Math.round
It's a model, not a browser. It leaves out the server's think time, redirects, packet loss, TLS resumption and 0-RTT, servers that start with other windows, and the part of the download that overlaps with the round trips. It also gives QUIC TCP's window, though RFC 9002's is close: ten packets, at most 14,720 bytes with normal packet sizes.
estimateLoad({ rttMs: 100, mbps: 10, htmlBytes: 50_000, connection: "new", protocol: "h2" });
// { setup: 3, html: 3, roundTrips: 6, ms: 640 }
// 14,600 + 29,200 bytes in two rounds is short of 50,000, so it takes a third;
// then 6 round trips of 100 ms, and 400,000 bits at 10,000 bits each ms is 40 ms more.
solution.jsfunction estimateLoad({ rttMs, mbps, htmlBytes, connection, protocol }) {
// Today: three round trips of setup on every visit, and the whole page in one more.
const setup = 3;
const html = 1;
const roundTrips = setup + html;
return { setup, html, roundTrips, ms: roundTrips * rttMs };
}
- a 50 KB page over a new HTTP/2 connection takes six round trips
- 14,600 bytes fit in the first round, and one byte more needs a second
- each round carries twice as much as the one before
- HTTP/3 needs one round trip less to set up a new connection
- a reused connection skips the setup and slow start, whatever the protocol
- bandwidth only changes the download, and the round trip changes every step
- rounds the total to a whole millisecond, and an empty response still takes a round trip
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…