Skip to content

Commit

Permalink
WIP - Keep running sum
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacey committed Feb 22, 2018
1 parent 85e7570 commit bbc2930
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugins/aggregators/basicstats/basicstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type basicstats struct {
count float64
min float64
max float64
sum float64
mean float64
M2 float64 //intermedia value for variance/stdev
}
Expand Down Expand Up @@ -78,6 +79,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
min: fv,
max: fv,
mean: fv,
sum: fv,
M2: 0.0,
}
}
Expand All @@ -93,6 +95,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
min: fv,
max: fv,
mean: fv,
sum: fv,
M2: 0.0,
}
continue
Expand Down Expand Up @@ -120,6 +123,8 @@ func (m *BasicStats) Add(in telegraf.Metric) {
} else if fv > tmp.max {
tmp.max = fv
}
//sum compute
tmp.sum += fv
//store final data
m.cache[id].fields[k] = tmp
}
Expand Down Expand Up @@ -148,7 +153,7 @@ func (m *BasicStats) Push(acc telegraf.Accumulator) {
fields[k+"_mean"] = v.mean
}
if config.sum {
fields[k+"_sum"] = (v.mean * v.count)
fields[k+"_sum"] = v.sum
}

//v.count always >=1
Expand Down
52 changes: 52 additions & 0 deletions plugins/aggregators/basicstats/basicstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,58 @@ func TestBasicStatsWithOnlySum(t *testing.T) {
acc.AssertContainsTaggedFields(t, "m1", expectedFields, expectedTags)
}

// Verify that sum doesn't suffer from floating point errors. Early
// implementations of sum were calulated from mean and count, which
// e.g. summed "1, 1, 5, 1" as "7.999999..." instead of 8.
func TestBasicStatsWithOnlySumFloatingPointErrata(t *testing.T) {

var sum1, _ = metric.New("m1",
map[string]string{},
map[string]interface{}{
"a": int64(1),
},
time.Now(),
)
var sum2, _ = metric.New("m1",
map[string]string{},
map[string]interface{}{
"a": int64(1),
},
time.Now(),
)
var sum3, _ = metric.New("m1",
map[string]string{},
map[string]interface{}{
"a": int64(5),
},
time.Now(),
)
var sum4, _ = metric.New("m1",
map[string]string{},
map[string]interface{}{
"a": int64(1),
},
time.Now(),
)

aggregator := NewBasicStats()
aggregator.Stats = []string{"sum"}

aggregator.Add(sum1)
aggregator.Add(sum2)
aggregator.Add(sum3)
aggregator.Add(sum4)

acc := testutil.Accumulator{}
aggregator.Push(&acc)

expectedFields := map[string]interface{}{
"a_sum": float64(8),
}
expectedTags := map[string]string{}
acc.AssertContainsTaggedFields(t, "m1", expectedFields, expectedTags)
}

// Test only aggregating variance
func TestBasicStatsWithOnlyVariance(t *testing.T) {

Expand Down

0 comments on commit bbc2930

Please sign in to comment.