| new file mode 100644 |
| index 000000000..eeeca55ac |
| --- /dev/null |
| +++ b/internal/perf/perf.go |
| @@ -0,0 +1,210 @@ |
| +// Package perf measures the console's requests against a time budget and |
| +// keeps what it measured: a bounded ring of recent samples, summarised |
| +// per route as p50 and p95. |
| +// |
| +// The design follows Rastrillo's perf package, without its labels, cold |
| +// start or startup span: the console has no cold-start budget. The |
| +// budget is judged on time to first byte, so a download or a stream that |
| +// starts quickly is not a failure however long it runs, and only a read |
| +// (GET or HEAD) has one. |
| +// |
| +// A sample's Screen is a route pattern, never a path. A path can carry a |
| +// token, and an over-budget warning is a log line that outlives the |
| +// request. |
| +package perf |
| + |
| +import ( |
| + "log/slog" |
| + "sort" |
| + "sync" |
| + "time" |
| +) |
| + |
| +// Capacity is how many samples a Recorder keeps: the most recent, in a |
| +// ring, so a busy process's memory is bounded by construction. |
| +const Capacity = 2048 |
| + |
| +// DefaultBudget is the first-byte budget for a GET or HEAD. |
| +const DefaultBudget = 150 * time.Millisecond |
| + |
| +// Sample is one request. Times are in milliseconds. |
| +type Sample struct { |
| + At time.Time `json:"at"` |
| + Screen string `json:"screen"` |
| + Method string `json:"method"` |
| + Status int `json:"status"` |
| + TTFB float64 `json:"ttfb_ms"` |
| + Total float64 `json:"total_ms"` |
| + Spans map[string]float64 `json:"spans_ms,omitempty"` |
| + // Calls counts the outbound HTTP requests the request made, by span |
| + // name. |
| + Calls map[string]int `json:"calls,omitempty"` |
| + Budget float64 `json:"budget_ms"` |
| + Over bool `json:"over_budget"` |
| +} |
| + |
| +// Group is one screen and method's requests. |
| +type Group struct { |
| + Screen string `json:"screen"` |
| + Method string `json:"method"` |
| + Requests int `json:"requests"` |
| + Over int `json:"over_budget"` |
| + P50 float64 `json:"p50_ms"` |
| + P95 float64 `json:"p95_ms"` |
| + Total95 float64 `json:"total_p95_ms"` |
| + // Calls is summed over the group's samples. |
| + Calls map[string]int `json:"calls,omitempty"` |
| +} |
| + |
| +// Recorder is the ring of recent samples. The zero value is ready; one |
| +// per process, shared by every request. |
| +type Recorder struct { |
| + mu sync.Mutex |
| + samples []Sample |
| + next int |
| + |
| + // background counts outbound requests made on a context no measured |
| + // request owns, since the process started. |
| + background map[string]int64 |
| + |
| + // Over-budget logging is throttled twice: once a minute per screen, |
| + // and 120 lines a minute in all. A slow dependency makes every page |
| + // slow at once, and the log is where someone goes to find out why, |
| + // so it must not be the thing that drowns. |
| + logWindow time.Time |
| + logCount int |
| + lastLog map[string]time.Time |
| +} |
| + |
| +// Snapshot returns the samples held, oldest first. |
| +func (c *Recorder) Snapshot() []Sample { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + out := make([]Sample, 0, len(c.samples)) |
| + if len(c.samples) == Capacity { |
| + out = append(out, c.samples[c.next:]...) |
| + return append(out, c.samples[:c.next]...) |
| + } |
| + return append(out, c.samples...) |
| +} |
| + |
| +// Groups summarises the samples held; see the package function. |
| +func (c *Recorder) Groups() []Group { return Groups(c.Snapshot()) } |
| + |
| +// Background returns how many outbound requests, by span name, were made |
| +// on a context no measured request owns (background passes, view-cache |
| +// loads), counted since the process started. It is a copy. |
| +func (c *Recorder) Background() map[string]int64 { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + out := make(map[string]int64, len(c.background)) |
| + for k, v := range c.background { |
| + out[k] = v |
| + } |
| + return out |
| +} |
| + |
| +func (c *Recorder) countBackground(name string) { |
| + c.mu.Lock() |
| + if c.background == nil { |
| + c.background = map[string]int64{} |
| + } |
| + c.background[name]++ |
| + c.mu.Unlock() |
| +} |
| + |
| +// Groups buckets samples by screen and method, with first-byte p50 and |
| +// p95 and total p95, slowest p95 first. Ties break by method, then |
| +// screen. |
| +func Groups(samples []Sample) []Group { |
| + type key struct{ screen, method string } |
| + buckets := map[key][]Sample{} |
| + for _, s := range samples { |
| + k := key{s.Screen, s.Method} |
| + buckets[k] = append(buckets[k], s) |
| + } |
| + out := make([]Group, 0, len(buckets)) |
| + for k, ss := range buckets { |
| + g := Group{Screen: k.screen, Method: k.method, Requests: len(ss)} |
| + ttfb := make([]float64, 0, len(ss)) |
| + total := make([]float64, 0, len(ss)) |
| + for _, s := range ss { |
| + if s.Over { |
| + g.Over++ |
| + } |
| + ttfb = append(ttfb, s.TTFB) |
| + total = append(total, s.Total) |
| + for name, n := range s.Calls { |
| + if g.Calls == nil { |
| + g.Calls = map[string]int{} |
| + } |
| + g.Calls[name] += n |
| + } |
| + } |
| + g.P50 = percentile(ttfb, 50) |
| + g.P95 = percentile(ttfb, 95) |
| + g.Total95 = percentile(total, 95) |
| + out = append(out, g) |
| + } |
| + sort.Slice(out, func(i, j int) bool { |
| + a, b := out[i], out[j] |
| + if a.P95 != b.P95 { |
| + return a.P95 > b.P95 |
| + } |
| + if a.Method != b.Method { |
| + return a.Method < b.Method |
| + } |
| + return a.Screen < b.Screen |
| + }) |
| + return out |
| +} |
| + |
| +// percentile is nearest-rank: the smallest value with at least p% of |
| +// the values at or below it. -1 for no values, so an empty group reads |
| +// as "no data" rather than as a suspiciously fast zero. |
| +func percentile(values []float64, p int) float64 { |
| + if len(values) == 0 { |
| + return -1 |
| + } |
| + sort.Float64s(values) |
| + return values[(len(values)*p+99)/100-1] |
| +} |
| + |
| +// add records s in the ring and, if it is over budget and the throttle |
| +// allows, logs it to log (nil logs nothing). |
| +func (c *Recorder) add(s Sample, log *slog.Logger) { |
| + c.mu.Lock() |
| + if len(c.samples) < Capacity { |
| + c.samples = append(c.samples, s) |
| + } else { |
| + c.samples[c.next] = s |
| + } |
| + c.next = (c.next + 1) % Capacity |
| + key := s.Method + "\x00" + s.Screen |
| + emit := s.Over && (c.lastLog == nil || s.At.Sub(c.lastLog[key]) >= time.Minute) |
| + if s.At.Sub(c.logWindow) >= time.Minute { |
| + c.logWindow = s.At |
| + c.logCount = 0 |
| + } |
| + emit = emit && c.logCount < 120 |
| + if emit { |
| + c.logCount++ |
| + if len(c.lastLog) >= Capacity { |
| + for k, at := range c.lastLog { |
| + if s.At.Sub(at) >= time.Minute { |
| + delete(c.lastLog, k) |
| + } |
| + } |
| + } |
| + if c.lastLog == nil { |
| + c.lastLog = map[string]time.Time{} |
| + } |
| + c.lastLog[key] = s.At |
| + } |
| + c.mu.Unlock() |
| + if emit && log != nil { |
| + log.Warn("request over performance budget", |
| + "screen", s.Screen, "method", s.Method, "status", s.Status, |
| + "ttfb_ms", s.TTFB, "total_ms", s.Total, "budget_ms", s.Budget, "spans_ms", s.Spans) |
| + } |
| +} |