github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/testutil/stress/histogram.go (about) 1 package stress 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type Histogram struct { 9 buckets []int64 10 counters map[int64]int64 11 min int64 12 max int64 13 total int64 14 } 15 16 func NewHistogram(buckets []int64) *Histogram { 17 return &Histogram{ 18 buckets: buckets, 19 counters: make(map[int64]int64), 20 } 21 } 22 23 func (h *Histogram) String() string { 24 builder := &strings.Builder{} 25 for _, b := range h.buckets { 26 builder.WriteString(fmt.Sprintf("%d\t%d\n", b, h.counters[b])) 27 } 28 builder.WriteString(fmt.Sprintf("min\t%d\n", h.min)) 29 builder.WriteString(fmt.Sprintf("max\t%d\n", h.max)) 30 builder.WriteString(fmt.Sprintf("total\t%d\n", h.total)) 31 return builder.String() 32 } 33 34 func (h *Histogram) Add(v int64) { 35 if h.min == 0 || v <= h.min { 36 h.min = v 37 } 38 if v > h.max { 39 h.max = v 40 } 41 h.total++ 42 for _, b := range h.buckets { 43 if v <= b { 44 h.counters[b]++ 45 } 46 } 47 } 48 49 func (h *Histogram) Clone() *Histogram { 50 buckets := make([]int64, len(h.buckets)) 51 copy(buckets, h.buckets) 52 53 counters := make(map[int64]int64) 54 for k, v := range h.counters { 55 counters[k] = v 56 } 57 58 return &Histogram{ 59 buckets: buckets, 60 counters: counters, 61 min: h.min, 62 max: h.max, 63 total: h.total, 64 } 65 }