2015-12-11 20:07:32 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2016-01-05 23:58:35 +00:00
|
|
|
"fmt"
|
2015-12-11 20:07:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-12-11 20:07:32 +00:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCPUStats(t *testing.T) {
|
|
|
|
var mps MockPS
|
|
|
|
defer mps.AssertExpectations(t)
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
cts := cpu.TimesStat{
|
2015-12-11 20:07:32 +00:00
|
|
|
CPU: "cpu0",
|
2016-12-05 08:35:59 +00:00
|
|
|
User: 8.8,
|
2015-12-11 20:07:32 +00:00
|
|
|
System: 8.2,
|
|
|
|
Idle: 80.1,
|
|
|
|
Nice: 1.3,
|
2016-12-05 08:35:59 +00:00
|
|
|
Iowait: 0.8389,
|
|
|
|
Irq: 0.6,
|
2015-12-11 20:07:32 +00:00
|
|
|
Softirq: 0.11,
|
|
|
|
Steal: 0.0511,
|
2016-12-05 08:35:59 +00:00
|
|
|
Guest: 3.1,
|
2015-12-11 20:07:32 +00:00
|
|
|
GuestNice: 0.324,
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
cts2 := cpu.TimesStat{
|
2015-12-11 20:07:32 +00:00
|
|
|
CPU: "cpu0",
|
2016-12-05 08:35:59 +00:00
|
|
|
User: 24.9, // increased by 16.1
|
2015-12-11 20:07:32 +00:00
|
|
|
System: 10.9, // increased by 2.7
|
2016-12-05 08:35:59 +00:00
|
|
|
Idle: 157.9798, // increased by 77.8798 (for total increase of 100)
|
|
|
|
Nice: 3.5, // increased by 2.2
|
|
|
|
Iowait: 0.929, // increased by 0.0901
|
|
|
|
Irq: 1.2, // increased by 0.6
|
2015-12-11 20:07:32 +00:00
|
|
|
Softirq: 0.31, // increased by 0.2
|
2016-12-05 08:35:59 +00:00
|
|
|
Steal: 0.2812, // increased by 0.2301
|
|
|
|
Guest: 11.4, // increased by 8.3
|
2015-12-11 20:07:32 +00:00
|
|
|
GuestNice: 2.524, // increased by 2.2
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
mps.On("CPUTimes").Return([]cpu.TimesStat{cts}, nil)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
cs := NewCPUStats(&mps)
|
|
|
|
|
|
|
|
cputags := map[string]string{
|
|
|
|
"cpu": "cpu0",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cs.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Computed values are checked with delta > 0 becasue of floating point arithmatic
|
|
|
|
// imprecision
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_user", 8.8, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_system", 8.2, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_idle", 80.1, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_nice", 1.3, 0, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_iowait", 0.8389, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_irq", 0.6, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_softirq", 0.11, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_steal", 0.0511, 0, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest", 3.1, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest_nice", 0.324, 0, cputags)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
mps2 := MockPS{}
|
2016-05-19 14:05:08 +00:00
|
|
|
mps2.On("CPUTimes").Return([]cpu.TimesStat{cts2}, nil)
|
2015-12-11 20:07:32 +00:00
|
|
|
cs.ps = &mps2
|
|
|
|
|
|
|
|
// Should have added cpu percentages too
|
|
|
|
err = cs.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_user", 24.9, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_system", 10.9, 0, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_idle", 157.9798, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_nice", 3.5, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_iowait", 0.929, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_irq", 1.2, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_softirq", 0.31, 0, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_steal", 0.2812, 0, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest", 11.4, 0, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "time_guest_nice", 2.524, 0, cputags)
|
|
|
|
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_user", 7.8, 0.0005, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_system", 2.7, 0.0005, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_idle", 77.8798, 0.0005, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_nice", 0, 0.0005, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_iowait", 0.0901, 0.0005, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_irq", 0.6, 0.0005, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_softirq", 0.2, 0.0005, cputags)
|
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_steal", 0.2301, 0.0005, cputags)
|
2016-12-05 08:35:59 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_guest", 8.3, 0.0005, cputags)
|
2016-01-05 23:58:35 +00:00
|
|
|
assertContainsTaggedFloat(t, &acc, "cpu", "usage_guest_nice", 2.2, 0.0005, cputags)
|
|
|
|
}
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-01-05 23:58:35 +00:00
|
|
|
// Asserts that a given accumulator contains a measurment of type float64 with
|
|
|
|
// specific tags within a certain distance of a given expected value. Asserts a failure
|
|
|
|
// if the measurement is of the wrong type, or if no matching measurements are found
|
|
|
|
//
|
|
|
|
// Paramaters:
|
|
|
|
// t *testing.T : Testing object to use
|
|
|
|
// acc testutil.Accumulator: Accumulator to examine
|
|
|
|
// measurement string : Name of the measurement to examine
|
|
|
|
// expectedValue float64 : Value to search for within the measurement
|
|
|
|
// delta float64 : Maximum acceptable distance of an accumulated value
|
|
|
|
// from the expectedValue parameter. Useful when
|
|
|
|
// floating-point arithmatic imprecision makes looking
|
|
|
|
// for an exact match impractical
|
|
|
|
// tags map[string]string : Tag set the found measurement must have. Set to nil to
|
|
|
|
// ignore the tag set.
|
|
|
|
func assertContainsTaggedFloat(
|
|
|
|
t *testing.T,
|
|
|
|
acc *testutil.Accumulator,
|
|
|
|
measurement string,
|
|
|
|
field string,
|
|
|
|
expectedValue float64,
|
|
|
|
delta float64,
|
|
|
|
tags map[string]string,
|
|
|
|
) {
|
|
|
|
var actualValue float64
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, pt := range acc.Metrics {
|
2016-01-05 23:58:35 +00:00
|
|
|
if pt.Measurement == measurement {
|
|
|
|
for fieldname, value := range pt.Fields {
|
|
|
|
if fieldname == field {
|
|
|
|
if value, ok := value.(float64); ok {
|
|
|
|
actualValue = value
|
|
|
|
if (value >= expectedValue-delta) && (value <= expectedValue+delta) {
|
|
|
|
// Found the point, return without failing
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64",
|
|
|
|
measurement))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf(
|
|
|
|
"Could not find measurement \"%s\" with requested tags within %f of %f, Actual: %f",
|
|
|
|
measurement, delta, expectedValue, actualValue)
|
|
|
|
assert.Fail(t, msg)
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|