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:
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,
Nearestreturns 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},Nearestreturns 20 for p30, 20 for p40, 35 for p50, 40 for p75 and 50 for p100. - Calling
Nearestwith samples{3, 1, 2}and p50 leaves the caller's slice in the original order{3, 1, 2}. - For a single sample
{42},Nearestreturns 42 for each tested percentile: p1, p50, p99 and p100. - Calling
Nearest(nil, 50)returns an error identifiable witherrors.Is(err, ErrNoSamples). CallingNearestwith samples{1, 2}and p values 0, -1 or 100.5 returns an error identifiable witherrors.Is(err, ErrBadPercentile). - Calling
Summarizewith 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 returnedStatsvalue supports direct comparison with==. - Calling
Summarize(nil)returns an error identifiable witherrors.Is(err, ErrNoSamples).
Constraints
- Use the nearest-rank rule: calculate the one-based rank as
ceil(p / 100 * n), wherenis the number of samples. Do not interpolate between values. - Return
ErrBadPercentilefor the tested invalid values 0, -1 and 100.5. - Return
ErrNoSampleswhen either function receives a nil sample slice. - Preserve the caller's slice order when calculating a percentile.
- Keep
Statscomparable 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:
cd backend
make test W=8O3Read the first failing assertion before changing anything. It identifies the percentile, input slice or summary value whose expected behaviour is not met.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
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.
Nearestcalculates percentiles using the nearest-rank method.- Percentile boundaries and rank rounding match the tested values.
- Caller-owned sample order remains unchanged.
- Invalid inputs return the expected sentinel errors.
Summarizereturns count, minimum, maximum, mean, p50, p95 and p99.make test W=8O3passes.
Commands
cd backend && make test W=8O3Tests
TestW8O3_NearestRankOnOneToOneHundredinbackend/internal/percentile/percentile_test.goTestW8O3_NearestRankRoundsTheRankUpinbackend/internal/percentile/percentile_test.goTestW8O3_NearestDoesNotReorderTheCallersSliceinbackend/internal/percentile/percentile_test.goTestW8O3_ASingleSampleIsEveryPercentileinbackend/internal/percentile/percentile_test.goTestW8O3_BadInputIsRefusedinbackend/internal/percentile/percentile_test.goTestW8O3_SummarizeReportsTheUsualLatencyNumbersinbackend/internal/percentile/percentile_test.goTestW8O3_SummarizeRefusesNoSamplesinbackend/internal/percentile/percentile_test.go