This is a Goroutine-safe component for latency statistics, implemented using buckets to accommodate a large amount of latency data.
- Duration: Extension of the
time.Duration
, representing time with units, allowing easy conversion between different time units and optimizing for the best unit. - Bucket: Bucket recorder offering various bucket allocation methods, such as automatic allocator, fixed unit allocator, and custom sequence allocator.
- Snapshot: Snapshot feature supporting retrieval of current statistical averages, maximums, minimums, and percentile values at any time.
- High performance: with optimizations for GC and concurrency, minimizing performance overhead to the maximum extent. Benchmark
- Goroutine-safe.
Import the latency package:
go get github.com/jhue58/latency
Use the provided default reporter:
r := report.NewLateReporter(buckets.NewBucketsRecorder())
wg := sync.WaitGroup{}
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
start, end := r.Alloc()
start()
time.Sleep(time.Duration(rand.Intn(600)) * time.Millisecond) // Simulate time-consuming operation
end()
}()
}
wg.Wait()
fmt.Println(r.Report())
Directly use the recorder (recommended):
r := buckets.NewBucketsRecorder()
wg := sync.WaitGroup{}
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r.Record(duration.NewDuartionWithUnit(float64(rand.Intn(600)),duration.Ms)) // Simulate latency 0~600ms
}()
}
wg.Wait()
sp := recorder.RecordedSnapshot{}
r.Snapshot(sp)
mean := sp.Mean()
fmt.Println("mean: ", mean.String())
The bucket allocation method is the automatic allocator. BenchmarkCode
goos: linux
goarch: amd64
pkg: github.com/jhue58/latency/buckets
cpu: AMD Ryzen 7 5800H with Radeon Graphics
BenchmarkRecord
BenchmarkRecord/Record
BenchmarkRecord/Record-16 27292686 46.07 ns/op
BenchmarkRecord/RecordParallel
BenchmarkRecord/RecordParallel-16 100000000 10.56 ns/op
goos: linux
goarch: amd64
pkg: github.com/jhue58/latency/buckets
cpu: AMD Ryzen 7 5800H with Radeon Graphics
BenchmarkSnapshot
BenchmarkSnapshot/Snapshot
BenchmarkSnapshot/Snapshot-16 8595 122031 ns/op
BenchmarkSnapshot/SnapshotParallel
BenchmarkSnapshot/SnapshotParallel-16 21300 53997 ns/op
=== RUN TestBucketsRecorder_MemUse
buckets_test.go:97: Befor Mem Used:811008 byte
buckets_test.go:104: Recorded count: 100000000
buckets_test.go:106: After Mem Used:1204224 byte
buckets_test.go:107: Mem Used:393216 byte
For more advanced examples, refer to examples.