From 54e61aa78aab4b49a1ba17bbba4a606111cd4274 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Wed, 26 Sep 2018 19:08:46 -0700 Subject: [PATCH] Use FieldList in basicstats to improve performance (#4741) --- plugins/aggregators/basicstats/basicstats.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/aggregators/basicstats/basicstats.go b/plugins/aggregators/basicstats/basicstats.go index 42b795ab6..701cd8a85 100644 --- a/plugins/aggregators/basicstats/basicstats.go +++ b/plugins/aggregators/basicstats/basicstats.go @@ -72,9 +72,9 @@ func (m *BasicStats) Add(in telegraf.Metric) { tags: in.Tags(), fields: make(map[string]basicstats), } - for k, v := range in.Fields() { - if fv, ok := convert(v); ok { - a.fields[k] = basicstats{ + for _, field := range in.FieldList() { + if fv, ok := convert(field.Value); ok { + a.fields[field.Key] = basicstats{ count: 1, min: fv, max: fv, @@ -86,11 +86,11 @@ func (m *BasicStats) Add(in telegraf.Metric) { } m.cache[id] = a } else { - for k, v := range in.Fields() { - if fv, ok := convert(v); ok { - if _, ok := m.cache[id].fields[k]; !ok { + for _, field := range in.FieldList() { + if fv, ok := convert(field.Value); ok { + if _, ok := m.cache[id].fields[field.Key]; !ok { // hit an uncached field of a cached metric - m.cache[id].fields[k] = basicstats{ + m.cache[id].fields[field.Key] = basicstats{ count: 1, min: fv, max: fv, @@ -101,7 +101,7 @@ func (m *BasicStats) Add(in telegraf.Metric) { continue } - tmp := m.cache[id].fields[k] + tmp := m.cache[id].fields[field.Key] //https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance //variable initialization x := fv @@ -126,7 +126,7 @@ func (m *BasicStats) Add(in telegraf.Metric) { //sum compute tmp.sum += fv //store final data - m.cache[id].fields[k] = tmp + m.cache[id].fields[field.Key] = tmp } } }