Use FieldList in basicstats to improve performance (#4741)

This commit is contained in:
Daniel Nelson 2018-09-26 19:08:46 -07:00 committed by GitHub
parent 38e5e103ce
commit 54e61aa78a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -72,9 +72,9 @@ func (m *BasicStats) Add(in telegraf.Metric) {
tags: in.Tags(), tags: in.Tags(),
fields: make(map[string]basicstats), fields: make(map[string]basicstats),
} }
for k, v := range in.Fields() { for _, field := range in.FieldList() {
if fv, ok := convert(v); ok { if fv, ok := convert(field.Value); ok {
a.fields[k] = basicstats{ a.fields[field.Key] = basicstats{
count: 1, count: 1,
min: fv, min: fv,
max: fv, max: fv,
@ -86,11 +86,11 @@ func (m *BasicStats) Add(in telegraf.Metric) {
} }
m.cache[id] = a m.cache[id] = a
} else { } else {
for k, v := range in.Fields() { for _, field := range in.FieldList() {
if fv, ok := convert(v); ok { if fv, ok := convert(field.Value); ok {
if _, ok := m.cache[id].fields[k]; !ok { if _, ok := m.cache[id].fields[field.Key]; !ok {
// hit an uncached field of a cached metric // hit an uncached field of a cached metric
m.cache[id].fields[k] = basicstats{ m.cache[id].fields[field.Key] = basicstats{
count: 1, count: 1,
min: fv, min: fv,
max: fv, max: fv,
@ -101,7 +101,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
continue continue
} }
tmp := m.cache[id].fields[k] tmp := m.cache[id].fields[field.Key]
//https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance //https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance
//variable initialization //variable initialization
x := fv x := fv
@ -126,7 +126,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
//sum compute //sum compute
tmp.sum += fv tmp.sum += fv
//store final data //store final data
m.cache[id].fields[k] = tmp m.cache[id].fields[field.Key] = tmp
} }
} }
} }