Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Report Latency as Percentiles

In this lesson, you measured how long hikers wait for the assistant over a day of 200 questions, saw what an average hides, and reported the wait as percentiles of total time and of the time to the first words.

Key Points

  • Every request’s log line holds its duration. The day in the video is the same 200 July questions you priced in Price Each Request From Its Usage, run on the stand-in model from the course’s exercises with every trace kept. The stand-in’s waits are scripted: the kinds of delay are real ones, but the numbers aren’t yours, and a real provider’s day is messier.
  • The natural first number is an average, the same arithmetic as the cost script: add up the durations and divide by the count. The script counts completed answers, the /ask lines with status 200. Failures are counted as errors, as in Log One JSON Line per Request. On the video’s day, the average came to 2,404 ms.
  • The average describes no hiker’s wait. In a histogram with a bar for every quarter of a second, 141 requests took between 1.5 and 2 seconds, and the slowest took 12.6 seconds. The average fell in a bar with no requests in it, 168 of the 200 requests were faster than it, and the slow ones were far slower.
  • Model calls always have a few slow requests, from a long answer, a retry after a rate limit, or an extra tool round. Those few pull the average up.
  • A percentile counts along the sorted times. Sort the day’s times from fastest to slowest. The p50 is the time at the halfway point, the 100th of 200: half the requests took that long or less. The p95 is the 190th: 190 requests finished within it, and 10 took longer.
  • percentile() makes a sorted copy of the times with .toSorted(). The compare function, (a, b) => a - b, sorts them as numbers: without it, JavaScript compares them as text, and 12587 comes before 1886. Functions You Can Pass Around covers sorting with a compare function.
  • Multiplying the count by the percentage and rounding up gives the position, and the minus one turns that position into an array index, which counts from zero.
  • On the video’s day, total time had a p50 of 1,886 ms and a p95 of 5,670 ms. Half the hikers waited under two seconds. The p95 is three times the p50, and one in 20 waited even longer.
  • The time to first token is the other wait: how long the hiker waits for the first words. A streamAnswer span’s firstChunkMs counts from the start of the answer call, after retrieval and the tool round, so it’s only the model’s part.
  • The script pairs each span with its log line by the request ID. The line is written when the request finishes, so its time minus its durationMs is the start, and the first words went out at the span’s start plus its firstChunkMs. The script keeps only the answers with a span that recorded a first chunk, so both numbers count the same answers: on the video’s day, all 200.
  • On the video’s day, the wait for the first words had a p50 of 1,178 ms and a p95 of 3,215 ms. That tail came mostly from rate-limit retries before the answer call, while long answers slowed only the total. The causes differ, so you report both.
  • Report latency as percentiles: the p50 and the p95 of total time and of time to first token, never the average alone. With more traffic, add the p99 too. On a day of 200 requests, only two sit above it.

Final Code

Save this as scripts/latency.js. It reads the day’s request log and trace file, given as its two arguments:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In Price Each Request From Its Usage, you priced a day of 200 questions. Now measure how long hikers wait for an answer.

00:09 Every request’s log line holds its duration. This one took 1,833 milliseconds. This log is the video’s day, the same 200 questions on the stand-in model, with every trace kept.

00:23 Its waits are scripted, with real kinds of delay but not your numbers.

00:30 Your cost script divided the day’s total by its count. Here, the natural first move is an average too. This script parses each log line, skipping any that isn’t JSON.

00:42 It keeps the completed answers: the ask route’s lines with status 200. Then it adds up the durations and divides by the count. Run it on the day’s log. The average is 2,404 milliseconds.

00:57 About two and a half seconds sounds like a fair number for the wardens.

01:03 Now picture the same 200 durations as a histogram, with a bar for every quarter of a second. Most land in one tall hump: 141 took between 1.5 and 2 seconds.

01:17 A few land far to the right, and the slowest took 12.6 seconds.

01:23 Now mark the average. It falls in a bar with no requests at all. 168 requests were faster than the average, and the slow ones were far slower.

01:34 Model calls always have a few slow ones, from a long answer, a retry after a rate limit, or an extra tool round. Those few pull the average up, so it describes no hiker’s wait.

01:48 Instead, sort the times, fastest to slowest, and count along the row. The p50 is the time at the halfway point, the 100th of 200. Half the requests took that long or less.

02:02 The p95 is the 190th time. 190 requests finished within it, and 10 took longer. In code, a percentile function first makes a sorted copy of the times. The compare function sorts them as numbers.

02:19 Without it, JavaScript compares them as text, and 12,587 comes before 1,886. Then it multiplies the count by the percentage and rounds up. That’s the position, 190 for the p95.

02:36 Arrays count from zero, so minus one turns it into an index. Run it. The p50 is 1,886 milliseconds, and the p95 is 5,670. Half the hikers waited under two seconds.

02:54 The p95 is three times the p50, and one in 20 waited even longer.

03:01 Total time is one wait. The other is the wait for the first words, the time to first token. Each streamAnswer span’s first-chunk time counts from the start of the answer call, after retrieval and the tool round.

03:16 So the script also reads the trace file and pairs each answer span with its log line by request ID. The log line is written when the request finishes, so its time minus its duration is the start.

03:30 The first words went out at the span’s start plus its first-chunk time. The gap is the hiker’s wait. Run it again. The p50 is 1,178 milliseconds, and the p95 is 3,215.

03:47 This wait has its own tail, mostly from rate-limit retries before the answer call. Long answers slow only the total. The causes differ, so you report both.

03:59 So report latency as percentiles: the p50 and the p95 of total time and of time to first token, never the average alone. With more traffic, add the p99 too.

04:13 On a day of 200 requests, only two sit above it. Run it on your own day from Price Each Request From Its Usage, the one with every trace kept. That gives you four numbers of your own.

04:27 In Exercise: Report Cost and Latency, you’ll report a day’s cost and latency from recorded traces, and find what makes the slowest requests slow.

Become a Member to join the conversation.