Skip to content

Commit

Permalink
Set index limits in the grow funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed May 30, 2023
1 parent 6f7acd9 commit f63d2e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
31 changes: 22 additions & 9 deletions output/cloud/expv2/hdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,28 @@ func (h *histogram) addToBucket(v float64) {

index := resolveBucketIndex(v)

blen := len(h.Buckets)
if blen == 0 {
if len(h.Buckets) == 0 {
h.FirstNotZeroBucket = index
h.LastNotZeroBucket = index
h.Buckets = append(h.Buckets, 1)
return
}

if index < h.FirstNotZeroBucket {
// they grow the current Buckets slice if there isn't enough capacity.
//
// An example with growRight:
// With Buckets [4, 1] and index equals to 5
// then we expect a slice like [4,1,0,0,0,0]
// then the counter at 5th position will be incremented
// generating the final slice [4,1,0,0,0,1]
switch {
case index < h.FirstNotZeroBucket:
h.growLeft(index)
h.FirstNotZeroBucket = index
} else if index > h.LastNotZeroBucket {
case index > h.LastNotZeroBucket:
h.growRight(index)
h.LastNotZeroBucket = index
default:
h.Buckets[index-h.FirstNotZeroBucket]++
}
h.Buckets[index-h.FirstNotZeroBucket]++
}

func (h *histogram) growLeft(index uint32) {
Expand All @@ -143,12 +149,15 @@ func (h *histogram) growLeft(index uint32) {

// TODO: we may consider to swap by sub-groups
// e.g [4, 1] => [4, 1, 0, 0] => [0, 0, 4, 1]
// It requires benchmark if it is better than
// just copy
// It requires a benchmark if it is better than just copy it.

newBuckets := make([]uint32, newLen)
copy(newBuckets[h.FirstNotZeroBucket-index:], h.Buckets)
h.Buckets = newBuckets

// Update the stats
h.Buckets[0] = 1
h.FirstNotZeroBucket = index
}

// growRight expands the buckets slice
Expand All @@ -171,6 +180,10 @@ func (h *histogram) growRight(index uint32) {
copy(newBuckets, h.Buckets)
h.Buckets = newBuckets
}

// Update the stats
h.Buckets[len(h.Buckets)-1] = 1
h.LastNotZeroBucket = index
}

// histogramAsProto converts the histogram into the equivalent Protobuf version.
Expand Down
22 changes: 19 additions & 3 deletions output/cloud/expv2/hdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ func TestValueBacket(t *testing.T) {
func TestNewHistogramWithSimpleValue(t *testing.T) {
t.Parallel()

// Add a lower bucket index within slice capacity
// Zero as value
res := newHistogram()
res.addToBucket(0)
exp := histogram{
Buckets: []uint32{1},
FirstNotZeroBucket: 0,
LastNotZeroBucket: 0,
ExtraLowBucket: 0,
ExtraHighBucket: 0,
Max: 0,
Min: 0,
Sum: 0,
Count: 1,
}
require.Equal(t, exp, res)

// Add a lower bucket index within slice capacity
res = newHistogram()
res.addToBucket(8)
res.addToBucket(5)

exp := histogram{
exp = histogram{
Buckets: []uint32{1, 0, 0, 1},
FirstNotZeroBucket: 5,
LastNotZeroBucket: 8,
Expand Down Expand Up @@ -233,7 +249,7 @@ func TestHistogramGrowRight(t *testing.T) {
h.growRight(5)
assert.Len(t, h.Buckets, 6)
assert.Equal(t, uint32(101), h.Buckets[2])
assert.Equal(t, uint32(0), h.Buckets[5])
assert.Equal(t, uint32(1), h.Buckets[5])

// it is not possible to request an index smaller than
// the last already available index
Expand Down

0 comments on commit f63d2e6

Please sign in to comment.