mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
nettrace: Add a new tracing library
This commit introduces a new tracing library, that replaces golang.org/x/net/trace, and supports (amongts other thing) nested traces. This is a minimal change, future patches will make use of the new functionality.
This commit is contained in:
71
internal/nettrace/histogram.go
Normal file
71
internal/nettrace/histogram.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package nettrace
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type histogram struct {
|
||||
count [nBuckets]uint64
|
||||
|
||||
totalQ uint64
|
||||
totalT time.Duration
|
||||
min time.Duration
|
||||
max time.Duration
|
||||
}
|
||||
|
||||
func (h *histogram) Add(bucket int, latency time.Duration) {
|
||||
if h.totalQ == 0 || h.min > latency {
|
||||
h.min = latency
|
||||
}
|
||||
if h.max < latency {
|
||||
h.max = latency
|
||||
}
|
||||
|
||||
h.count[bucket]++
|
||||
h.totalQ++
|
||||
h.totalT += latency
|
||||
}
|
||||
|
||||
type histSnapshot struct {
|
||||
Counts map[time.Duration]line
|
||||
Count uint64
|
||||
Avg, Min, Max time.Duration
|
||||
}
|
||||
|
||||
type line struct {
|
||||
Start time.Duration
|
||||
BucketIdx int
|
||||
Count uint64
|
||||
Percent float32
|
||||
CumPct float32
|
||||
}
|
||||
|
||||
func (h *histogram) Snapshot() *histSnapshot {
|
||||
s := &histSnapshot{
|
||||
Counts: map[time.Duration]line{},
|
||||
Count: h.totalQ,
|
||||
Min: h.min,
|
||||
Max: h.max,
|
||||
}
|
||||
|
||||
if h.totalQ > 0 {
|
||||
s.Avg = time.Duration(uint64(h.totalT) / h.totalQ)
|
||||
}
|
||||
|
||||
var cumCount uint64
|
||||
for i := 0; i < nBuckets; i++ {
|
||||
cumCount += h.count[i]
|
||||
l := line{
|
||||
Start: buckets[i],
|
||||
BucketIdx: i,
|
||||
Count: h.count[i],
|
||||
}
|
||||
if h.totalQ > 0 {
|
||||
l.Percent = float32(h.count[i]) / float32(h.totalQ) * 100
|
||||
l.CumPct = float32(cumCount) / float32(h.totalQ) * 100
|
||||
}
|
||||
s.Counts[buckets[i]] = l
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user