-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rate_test.go
121 lines (98 loc) · 3.19 KB
/
rate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package metrics
import (
// stdlib
"testing"
// 3p
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRateSampling(t *testing.T) {
// Initialize rates
mRate1 := Rate{}
mRate2 := Rate{}
// Add samples
mRate1.addSample(&MetricSample{Value: 1}, 50)
mRate1.addSample(&MetricSample{Value: 11}, 52.5)
mRate2.addSample(&MetricSample{Value: 1}, 60)
// First rate
series, err := mRate1.flush(60)
assert.Nil(t, err)
require.Len(t, series, 1)
require.Len(t, series[0].Points, 1)
assert.InEpsilon(t, 4., series[0].Points[0].Value, epsilon)
assert.EqualValues(t, 52.5, series[0].Points[0].Ts)
// Second rate (should return error)
_, err = mRate2.flush(60)
assert.NotNil(t, err)
}
func TestRateSamplingMultipleSamplesInSameFlush(t *testing.T) {
// Initialize rate
mRate := Rate{}
// Add samples
mRate.addSample(&MetricSample{Value: 1}, 50)
mRate.addSample(&MetricSample{Value: 2}, 55)
mRate.addSample(&MetricSample{Value: 4}, 61)
// Should compute rate based on the last 2 samples
series, err := mRate.flush(65)
assert.Nil(t, err)
require.Len(t, series, 1)
require.Len(t, series[0].Points, 1)
assert.InEpsilon(t, 2./6., series[0].Points[0].Value, epsilon)
assert.EqualValues(t, 61, series[0].Points[0].Ts)
}
func TestRateSamplingNoSampleForOneFlush(t *testing.T) {
// Initialize rate
mRate := Rate{}
// Add samples
mRate.addSample(&MetricSample{Value: 1}, 50)
mRate.addSample(&MetricSample{Value: 2}, 55)
// First flush: no error
_, err := mRate.flush(60)
assert.Nil(t, err)
// Second flush w/o sample: error
_, err = mRate.flush(60)
assert.NotNil(t, err)
// Third flush w/ sample
mRate.addSample(&MetricSample{Value: 4}, 60)
// Should compute rate based on the last 2 samples
series, err := mRate.flush(60)
assert.Nil(t, err)
require.Len(t, series, 1)
require.Len(t, series[0].Points, 1)
assert.InEpsilon(t, 2./5., series[0].Points[0].Value, epsilon)
assert.EqualValues(t, 60, series[0].Points[0].Ts)
}
func TestRateSamplingSamplesAtSameTimestamp(t *testing.T) {
// Initialize rate
mRate := Rate{}
// Add samples
mRate.addSample(&MetricSample{Value: 1}, 50)
mRate.addSample(&MetricSample{Value: 2}, 50)
series, err := mRate.flush(60)
assert.NotNil(t, err)
assert.Len(t, series, 0)
}
func TestRateSamplingNegativeRate(t *testing.T) {
// Initialize rate
mRate := Rate{}
// Add samples, with second value below first one
mRate.addSample(&MetricSample{Value: 2}, 50)
mRate.addSample(&MetricSample{Value: 1}, 55)
// Should return an error
series, err := mRate.flush(60)
assert.NotNil(t, err)
assert.Len(t, series, 0)
// Add a sample again, this time with positive diff
mRate.addSample(&MetricSample{Value: 3}, 62)
// Should compute rate based on the last 2 samples
series, err = mRate.flush(70)
assert.Nil(t, err)
require.Len(t, series, 1)
require.Len(t, series[0].Points, 1)
assert.InEpsilon(t, (3.-1.)/(62.-55.), series[0].Points[0].Value, epsilon)
assert.EqualValues(t, 62, series[0].Points[0].Ts)
}