github.com/m3db/m3@v1.5.0/src/aggregator/aggregation/counter_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package aggregation 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/metrics/aggregation" 28 "github.com/m3db/m3/src/x/instrument" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestCounterDefaultAggregationType(t *testing.T) { 34 c := NewCounter(NewOptions(instrument.NewOptions())) 35 require.False(t, c.HasExpensiveAggregations) 36 for i := 1; i <= 100; i++ { 37 c.Update(time.Now(), int64(i), nil) 38 } 39 require.Equal(t, int64(5050), c.Sum()) 40 require.Equal(t, 5050.0, c.ValueOf(aggregation.Sum)) 41 require.Equal(t, 100.0, c.ValueOf(aggregation.Count)) 42 require.Equal(t, 50.5, c.ValueOf(aggregation.Mean)) 43 } 44 45 func TestCounterReturnsLastNonEmptyAnnotation(t *testing.T) { 46 c := NewCounter(NewOptions(instrument.NewOptions())) 47 c.Update(time.Now(), int64(1), []byte("first")) 48 c.Update(time.Now(), int64(2), []byte("second")) 49 c.Update(time.Now(), int64(3), nil) 50 51 require.Equal(t, []byte("second"), c.Annotation()) 52 } 53 54 func TestCounterCustomAggregationType(t *testing.T) { 55 opts := NewOptions(instrument.NewOptions()) 56 opts.HasExpensiveAggregations = true 57 58 c := NewCounter(opts) 59 require.True(t, c.HasExpensiveAggregations) 60 61 for i := 1; i <= 100; i++ { 62 c.Update(time.Now(), int64(i), nil) 63 } 64 require.Equal(t, int64(5050), c.Sum()) 65 for aggType := range aggregation.ValidTypes { 66 v := c.ValueOf(aggType) 67 switch aggType { 68 case aggregation.Min: 69 require.Equal(t, float64(1), v) 70 case aggregation.Max: 71 require.Equal(t, float64(100), v) 72 case aggregation.Mean: 73 require.Equal(t, 50.5, v) 74 case aggregation.Count: 75 require.Equal(t, float64(100), v) 76 case aggregation.Sum: 77 require.Equal(t, float64(5050), v) 78 case aggregation.SumSq: 79 require.Equal(t, float64(338350), v) 80 case aggregation.Stdev: 81 require.InDelta(t, 29.01149, v, 0.001) 82 default: 83 require.Equal(t, float64(0), v) 84 require.False(t, aggType.IsValidForCounter()) 85 } 86 } 87 }