Skip to content

Commit

Permalink
convert custom range to vmrange map
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Chubatiuk authored and Andrii Chubatiuk committed Mar 6, 2024
1 parent 4d19f45 commit 8bb04d2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ type Histogram struct {
sum float64
}

// ConvertToVMRange distributes input value in a range (lower, upper] to a vmRange static buckets
func ConvertToVMRange(output map[string]float64, value, lower, upper float64) {
if lower > upper {
return
}
bucketRangesOnce.Do(initBucketRanges)
lowerBucketIdx := int((math.Log10(lower) - e10Min) * bucketsPerDecimal)
upperBucketIdx := int((math.Log10(upper) - e10Min) * bucketsPerDecimal)
rangeDistance := upper - lower
for i := lowerBucketIdx; i <= upperBucketIdx; i++ {
lowerInRange := bucketBounds[i]
if lowerInRange < lower {
lowerInRange = lower
}
upperInRange := bucketBounds[i+1]
if upperInRange > upper {
upperInRange = upper
}
multiplier := (upperInRange - lowerInRange) / rangeDistance
v := math.Round(value * multiplier)
if v > 0 {
output[getVMRange(i)] += v
}
}
}

// Reset resets the given histogram.
func (h *Histogram) Reset() {
h.mu.Lock()
Expand Down Expand Up @@ -185,18 +211,21 @@ func initBucketRanges() {
v := math.Pow10(e10Min)
start := fmt.Sprintf("%.3e", v)
for i := 0; i < bucketsCount; i++ {
bucketBounds[i] = v
v *= bucketMultiplier
end := fmt.Sprintf("%.3e", v)
bucketRanges[i] = start + "..." + end
start = end
}
bucketBounds[len(bucketBounds)-1] = v
}

var (
lowerBucketRange = fmt.Sprintf("0...%.3e", math.Pow10(e10Min))
upperBucketRange = fmt.Sprintf("%.3e...+Inf", math.Pow10(e10Max))

bucketRanges [bucketsCount]string
bucketBounds [bucketsCount]float64
bucketRangesOnce sync.Once
)

Expand Down

0 comments on commit 8bb04d2

Please sign in to comment.