2016-09-08 14:22:10 +00:00
|
|
|
package minmax
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-09-20 13:15:23 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
)
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
var m1, _ = metric.New("m1",
|
2016-09-20 13:15:23 +00:00
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": int64(1),
|
|
|
|
"b": int64(1),
|
|
|
|
"c": int64(1),
|
|
|
|
"d": int64(1),
|
|
|
|
"e": int64(1),
|
|
|
|
"f": float64(2),
|
|
|
|
"g": float64(2),
|
|
|
|
"h": float64(2),
|
|
|
|
"i": float64(2),
|
|
|
|
"j": float64(3),
|
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
var m2, _ = metric.New("m1",
|
2016-09-20 13:15:23 +00:00
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": int64(1),
|
|
|
|
"b": int64(3),
|
|
|
|
"c": int64(3),
|
|
|
|
"d": int64(3),
|
|
|
|
"e": int64(3),
|
|
|
|
"f": float64(1),
|
|
|
|
"g": float64(1),
|
|
|
|
"h": float64(1),
|
|
|
|
"i": float64(1),
|
|
|
|
"j": float64(1),
|
|
|
|
"k": float64(200),
|
2018-06-30 02:07:08 +00:00
|
|
|
"l": uint64(200),
|
2016-09-20 13:15:23 +00:00
|
|
|
"ignoreme": "string",
|
|
|
|
"andme": true,
|
|
|
|
},
|
|
|
|
time.Now(),
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkApply(b *testing.B) {
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax := NewMinMax()
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax.Add(m1)
|
|
|
|
minmax.Add(m2)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 13:15:23 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
// Test two metrics getting added.
|
2016-09-20 13:15:23 +00:00
|
|
|
func TestMinMaxWithPeriod(t *testing.T) {
|
|
|
|
acc := testutil.Accumulator{}
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax := NewMinMax()
|
2016-09-20 13:15:23 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax.Add(m1)
|
|
|
|
minmax.Add(m2)
|
|
|
|
minmax.Push(&acc)
|
2016-09-20 13:15:23 +00:00
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"a_max": float64(1),
|
|
|
|
"a_min": float64(1),
|
|
|
|
"b_max": float64(3),
|
|
|
|
"b_min": float64(1),
|
|
|
|
"c_max": float64(3),
|
|
|
|
"c_min": float64(1),
|
|
|
|
"d_max": float64(3),
|
|
|
|
"d_min": float64(1),
|
|
|
|
"e_max": float64(3),
|
|
|
|
"e_min": float64(1),
|
|
|
|
"f_max": float64(2),
|
|
|
|
"f_min": float64(1),
|
|
|
|
"g_max": float64(2),
|
|
|
|
"g_min": float64(1),
|
|
|
|
"h_max": float64(2),
|
|
|
|
"h_min": float64(1),
|
|
|
|
"i_max": float64(2),
|
|
|
|
"i_min": float64(1),
|
|
|
|
"j_max": float64(3),
|
|
|
|
"j_min": float64(1),
|
|
|
|
"k_max": float64(200),
|
|
|
|
"k_min": float64(200),
|
2018-06-30 02:07:08 +00:00
|
|
|
"l_max": float64(200),
|
|
|
|
"l_min": float64(200),
|
2016-09-20 13:15:23 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "m1", expectedFields, expectedTags)
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
// Test two metrics getting added with a push/reset in between (simulates
|
|
|
|
// getting added in different periods.)
|
2016-09-20 13:15:23 +00:00
|
|
|
func TestMinMaxDifferentPeriods(t *testing.T) {
|
|
|
|
acc := testutil.Accumulator{}
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax := NewMinMax()
|
2016-09-20 13:15:23 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax.Add(m1)
|
|
|
|
minmax.Push(&acc)
|
2016-09-20 13:15:23 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"a_max": float64(1),
|
|
|
|
"a_min": float64(1),
|
|
|
|
"b_max": float64(1),
|
|
|
|
"b_min": float64(1),
|
|
|
|
"c_max": float64(1),
|
|
|
|
"c_min": float64(1),
|
|
|
|
"d_max": float64(1),
|
|
|
|
"d_min": float64(1),
|
|
|
|
"e_max": float64(1),
|
|
|
|
"e_min": float64(1),
|
|
|
|
"f_max": float64(2),
|
|
|
|
"f_min": float64(2),
|
|
|
|
"g_max": float64(2),
|
|
|
|
"g_min": float64(2),
|
|
|
|
"h_max": float64(2),
|
|
|
|
"h_min": float64(2),
|
|
|
|
"i_max": float64(2),
|
|
|
|
"i_min": float64(2),
|
|
|
|
"j_max": float64(3),
|
|
|
|
"j_min": float64(3),
|
|
|
|
}
|
|
|
|
expectedTags := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "m1", expectedFields, expectedTags)
|
|
|
|
|
|
|
|
acc.ClearMetrics()
|
2016-09-22 17:10:51 +00:00
|
|
|
minmax.Reset()
|
|
|
|
minmax.Add(m2)
|
|
|
|
minmax.Push(&acc)
|
2016-09-20 13:15:23 +00:00
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"a_max": float64(1),
|
|
|
|
"a_min": float64(1),
|
|
|
|
"b_max": float64(3),
|
|
|
|
"b_min": float64(3),
|
|
|
|
"c_max": float64(3),
|
|
|
|
"c_min": float64(3),
|
|
|
|
"d_max": float64(3),
|
|
|
|
"d_min": float64(3),
|
|
|
|
"e_max": float64(3),
|
|
|
|
"e_min": float64(3),
|
|
|
|
"f_max": float64(1),
|
|
|
|
"f_min": float64(1),
|
|
|
|
"g_max": float64(1),
|
|
|
|
"g_min": float64(1),
|
|
|
|
"h_max": float64(1),
|
|
|
|
"h_min": float64(1),
|
|
|
|
"i_max": float64(1),
|
|
|
|
"i_min": float64(1),
|
|
|
|
"j_max": float64(1),
|
|
|
|
"j_min": float64(1),
|
|
|
|
"k_max": float64(200),
|
|
|
|
"k_min": float64(200),
|
2018-06-30 02:07:08 +00:00
|
|
|
"l_max": float64(200),
|
|
|
|
"l_min": float64(200),
|
2016-09-20 13:15:23 +00:00
|
|
|
}
|
|
|
|
expectedTags = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "m1", expectedFields, expectedTags)
|
|
|
|
}
|