Latency percentile package

You build a Go package that calculates nearest-rank percentiles and summary statistics from raw latency samples.

Ticket

Build the Go package internal/percentile in backend/internal/percentile/percentile.go. It turns []float64 latency samples into a nearest-rank percentile or a summary of the sample distribution.

Load-test reviews need p50, p95 and p99 alongside the mean to make tail latency visible when evaluating capacity. This optional package supports that review but does not block completion of Week 8.

The tests call this API directly:

go
package percentile

var ErrNoSamples error
var ErrBadPercentile error

type Stats struct {
    Count int
    Min, Max, Mean, P50, P95, P99 float64
}

func Nearest(samples []float64, p float64) (float64, error)

func Summarize(samples []float64) (Stats, error)

Acceptance criteria

  • For samples containing the values 1 through 100, Nearest returns 50 for p50, 90 for p90, 95 for p95, 99 for p99, 100 for p100 and 1 for p1.
  • For samples {15, 20, 35, 40, 50}, Nearest returns 20 for p30, 20 for p40, 35 for p50, 40 for p75 and 50 for p100.
  • Calling Nearest with samples {3, 1, 2} and p50 leaves the caller's slice in the original order {3, 1, 2}.
  • For a single sample {42}, Nearest returns 42 for each tested percentile: p1, p50, p99 and p100.
  • Calling Nearest(nil, 50) returns an error identifiable with errors.Is(err, ErrNoSamples). Calling Nearest with samples {1, 2} and p values 0, -1 or 100.5 returns an error identifiable with errors.Is(err, ErrBadPercentile).
  • Calling Summarize with samples containing the values 1 through 100 returns exactly {Count: 100, Min: 1, Max: 100, Mean: 50.5, P50: 50, P95: 95, P99: 99}. The returned Stats value supports direct comparison with ==.
  • Calling Summarize(nil) returns an error identifiable with errors.Is(err, ErrNoSamples).

Constraints

  • Use the nearest-rank rule: calculate the one-based rank as ceil(p / 100 * n), where n is the number of samples. Do not interpolate between values.
  • Return ErrBadPercentile for the tested invalid values 0, -1 and 100.5.
  • Return ErrNoSamples when either function receives a nil sample slice.
  • Preserve the caller's slice order when calculating a percentile.
  • Keep Stats comparable with ==.
  • Do not modify the provided tests.

Design direction

Keep the implementation in the internal/percentile package. Let Nearest own percentile validation and selection. Let Summarize calculate the basic statistics and obtain p50, p95 and p99 through the same percentile calculation.

Think about the trade-offs:

  • Sorting the input directly is less work than copying it, but changes data owned by the caller. A copied slice avoids that side effect.
  • Nearest-rank selection returns an observed sample. Interpolation produces a different convention, so use the method specified by the contract.
  • The mean describes the average latency. Percentiles describe positions in the distribution. Keep both in the summary because they reveal different behaviour.

Any layout that meets the contract passes.

How to check your work

Run the optional package tests from the backend directory:

bash
cd backend
make test W=8O3

Read the first failing assertion before changing anything. It identifies the percentile, input slice or summary value whose expected behaviour is not met.

Finished Exercise 8.15?

Optional extensions

3 items

  • 8.13Timestamped webhook signature packagenot completed
  • 8.14Mermaid sequence linternot completed
  • 8.15Latency percentile packagenot completed

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Nearest calculates percentiles using the nearest-rank method.
  2. Percentile boundaries and rank rounding match the tested values.
  3. Caller-owned sample order remains unchanged.
  4. Invalid inputs return the expected sentinel errors.
  5. Summarize returns count, minimum, maximum, mean, p50, p95 and p99.
  6. make test W=8O3 passes.

Commands

terminal
cd backend && make test W=8O3

Tests

  • TestW8O3_NearestRankOnOneToOneHundred in backend/internal/percentile/percentile_test.go
  • TestW8O3_NearestRankRoundsTheRankUp in backend/internal/percentile/percentile_test.go
  • TestW8O3_NearestDoesNotReorderTheCallersSlice in backend/internal/percentile/percentile_test.go
  • TestW8O3_ASingleSampleIsEveryPercentile in backend/internal/percentile/percentile_test.go
  • TestW8O3_BadInputIsRefused in backend/internal/percentile/percentile_test.go
  • TestW8O3_SummarizeReportsTheUsualLatencyNumbers in backend/internal/percentile/percentile_test.go
  • TestW8O3_SummarizeRefusesNoSamples in backend/internal/percentile/percentile_test.go