1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-29 15:02:30 +00:00
Files
go-chasquid-smtp/internal/nettrace/histogram.go
T
Alberto Bertogli 0210c5fd92 Apply go fix to simplify using modern features
This patch contains the result of running `go fix ./...`, which applies
simplifications that make use of modern language features.

There are no logic or behaviour changes.
2026-10-02 18:34:25 +01:00

72 lines
1.2 KiB
Go

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 := range nBuckets {
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
}