stats

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

stats

Concurrent sliding-window averages and percentiles.

Stats is safe for concurrent use. Values older than the configured window are removed when the tracker is read or updated. Percentile accepts values from 0 through 100; it returns the element at floor((count-1) * percentile/100). Use Values, Len, or Merge to inspect and combine trackers.

Example

import (
    "time"

    "github.com/pliu/go-utils/stats"
)

latency := stats.NewStats(5 * time.Minute)
latency.Add(12)
latency.Add(20)
latency.Add(35)

average, ok := latency.Average()                         // 22.333..., true
percentiles, ok := latency.Percentile([]float64{50, 95}) // []int64{20, 20}, true

Performance

Values are held twice: in arrival order in a ring, so the oldest can be expired, and in sorted order in a sorted_list, so a percentile is an O(log n) rank lookup rather than a sort. Average is O(1) against a running sum.

The ring buffer stores measurements by value and reuses its slots as the window slides, so once it has grown to the working set, Add allocates only when the value is one the window does not already contain — and nothing at all in a steady state of repeating values.

Every method takes the same lock, including the read-only ones: expiry is driven lazily from whatever call happens to arrive, so reads mutate too and cannot share an RWMutex. Reads therefore serialize against concurrent Adds. Values copies the whole window, so prefer Percentile, Average, or Len unless the individual values are actually needed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stats

type Stats struct {
	// contains filtered or unexported fields
}

Stats tracks numeric values in a sliding window and can produce summaries.

Values are held twice: in insertion order by window, so the oldest can be expired, and in sorted order by values, so percentiles are a rank lookup rather than a sort.

func NewStats

func NewStats(windowSize time.Duration) *Stats

func NewStatsWithClock

func NewStatsWithClock(windowSize time.Duration, clk clock.Clock) *Stats

func (*Stats) Add

func (s *Stats) Add(value int64)

func (*Stats) Average

func (s *Stats) Average() (float64, bool)

func (*Stats) Len

func (s *Stats) Len() int

func (*Stats) Merge

func (s *Stats) Merge(other *Stats)

func (*Stats) Percentile

func (s *Stats) Percentile(percentiles []float64) ([]int64, bool)

func (*Stats) Values

func (s *Stats) Values() []int64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址