Skip to content

Commit

Permalink
Test polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Jul 7, 2023
1 parent e7bee24 commit 976a8ff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
4 changes: 0 additions & 4 deletions output/cloud/expv2/hdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ func newHistogram() *histogram {
// If the value is lower or higher than the trackable limits
// then it is counted into specific buckets. All the stats are also updated accordingly.
func (h *histogram) addToBucket(v float64) {
// if h.Count == 0 {
// h.Max, h.Min = v, v
// } else {
if v > h.Max {
h.Max = v
}
if v < h.Min {
h.Min = v
}
// }

h.Count++
h.Sum += v
Expand Down
47 changes: 26 additions & 21 deletions output/cloud/expv2/hdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestHistogramAddWithSimpleValues(t *testing.T) {
for i, tc := range cases {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
h := newHistogram()
for _, v := range tc.vals {
h.Add(v)
Expand All @@ -131,9 +132,9 @@ func TestHistogramAddWithSimpleValues(t *testing.T) {
func TestHistogramAddWithUntrackables(t *testing.T) {
t.Parallel()

res := newHistogram()
h := newHistogram()
for _, v := range []float64{5, -3.14, 2 * 1e9, 1} {
res.Add(v)
h.Add(v)
}

exp := &histogram{
Expand All @@ -146,15 +147,15 @@ func TestHistogramAddWithUntrackables(t *testing.T) {
Sum: 2*1e9 + 5 + 1 - 3.14,
Count: 4,
}
assert.Equal(t, exp, res)
assert.Equal(t, exp, h)
}

func TestHistogramAddWithMultipleOccurances(t *testing.T) {
t.Parallel()

res := newHistogram()
h := newHistogram()
for _, v := range []float64{51.8, 103.6, 103.6, 103.6, 103.6} {
res.Add(v)
h.Add(v)
}

exp := &histogram{
Expand All @@ -167,14 +168,14 @@ func TestHistogramAddWithMultipleOccurances(t *testing.T) {
Sum: 466.20000000000005,
Count: 5,
}
assert.Equal(t, exp, res)
assert.Equal(t, exp, h)
}

func TestHistogramAddWithNegativeNum(t *testing.T) {
t.Parallel()

res := newHistogram()
res.Add(-2.42314)
h := newHistogram()
h.Add(-2.42314)

exp := &histogram{
Max: -2.42314,
Expand All @@ -185,14 +186,14 @@ func TestHistogramAddWithNegativeNum(t *testing.T) {
Sum: -2.42314,
Count: 1,
}
assert.Equal(t, exp, res)
assert.Equal(t, exp, h)
}

func TestHistogramAddWithMultipleNegativeNums(t *testing.T) {
t.Parallel()
res := newHistogram()
h := newHistogram()
for _, v := range []float64{-0.001, -0.001, -0.001} {
res.Add(v)
h.Add(v)
}

exp := &histogram{
Expand All @@ -204,13 +205,13 @@ func TestHistogramAddWithMultipleNegativeNums(t *testing.T) {
Sum: -0.003,
Count: 3,
}
assert.Equal(t, exp, res)
assert.Equal(t, exp, h)
}

func TestNewHistoramWithNoVals(t *testing.T) {
t.Parallel()

res := newHistogram()
h := newHistogram()
exp := &histogram{
Buckets: map[uint32]uint32{},
ExtraLowBucket: 0,
Expand All @@ -219,7 +220,7 @@ func TestNewHistoramWithNoVals(t *testing.T) {
Min: math.MaxFloat64,
Sum: 0,
}
assert.Equal(t, exp, res)
assert.Equal(t, exp, h)
}

func TestHistogramAsProto(t *testing.T) {
Expand Down Expand Up @@ -315,12 +316,16 @@ func TestHistogramAsProto(t *testing.T) {
},
}

for _, tc := range cases {
h := newHistogram()
for _, v := range tc.vals {
h.Add(v)
}
tc.exp.Time = &timestamppb.Timestamp{Seconds: 1}
assert.Equal(t, tc.exp, histogramAsProto(h, time.Unix(1, 0).UnixNano()), tc.name)
for i, tc := range cases {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
h := newHistogram()
for _, v := range tc.vals {
h.Add(v)
}
tc.exp.Time = &timestamppb.Timestamp{Seconds: 1}
assert.Equal(t, tc.exp, histogramAsProto(h, time.Unix(1, 0).UnixNano()), tc.name)
})
}
}
11 changes: 7 additions & 4 deletions output/cloud/expv2/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ func TestNewSink(t *testing.T) {
{metrics.Counter, &counter{}},
{metrics.Gauge, &gauge{}},
{metrics.Rate, &rate{}},
{metrics.Trend, &histogram{
Buckets: map[uint32]uint32{},
Max: -math.MaxFloat64,
Min: math.MaxFloat64},
{
metrics.Trend,
&histogram{
Buckets: map[uint32]uint32{},
Max: -math.MaxFloat64,
Min: math.MaxFloat64,
},
},
}
for _, tc := range tests {
Expand Down

0 comments on commit 976a8ff

Please sign in to comment.