shirou_gopsutil/cpu/cpu_test.go

176 lines
4.0 KiB
Go
Raw Normal View History

2014-12-30 21:09:05 +08:00
package cpu
2014-04-18 15:34:47 +08:00
import (
2014-05-12 10:51:08 +08:00
"fmt"
"os"
2014-11-02 11:05:52 +08:00
"runtime"
2014-04-18 15:34:47 +08:00
"testing"
2014-11-02 11:05:52 +08:00
"time"
"github.com/stretchr/testify/assert"
2014-04-18 15:34:47 +08:00
)
func TestCpu_times(t *testing.T) {
v, err := Times(false)
2014-04-18 15:34:47 +08:00
if err != nil {
t.Errorf("error %v", err)
}
if len(v) == 0 {
t.Error("could not get CPUs ", err)
2014-04-18 15:34:47 +08:00
}
empty := TimesStat{}
2014-04-18 15:34:47 +08:00
for _, vv := range v {
2014-05-12 10:51:08 +08:00
if vv == empty {
2014-04-18 15:34:47 +08:00
t.Errorf("could not get CPU User: %v", vv)
}
}
// test sum of per cpu stats is within margin of error for cpu total stats
cpuTotal, err := Times(false)
if err != nil {
t.Errorf("error %v", err)
}
if len(cpuTotal) == 0 {
t.Error("could not get CPUs ", err)
}
perCPU, err := Times(true)
if err != nil {
t.Errorf("error %v", err)
}
if len(perCPU) == 0 {
t.Error("could not get CPUs ", err)
}
var perCPUUserTimeSum float64
var perCPUSystemTimeSum float64
var perCPUIdleTimeSum float64
for _, pc := range perCPU {
perCPUUserTimeSum += pc.User
perCPUSystemTimeSum += pc.System
perCPUIdleTimeSum += pc.Idle
}
margin := 2.0
assert.InEpsilon(t, cpuTotal[0].User, perCPUUserTimeSum, margin)
assert.InEpsilon(t, cpuTotal[0].System, perCPUSystemTimeSum, margin)
assert.InEpsilon(t, cpuTotal[0].Idle, perCPUIdleTimeSum, margin)
2014-04-18 15:34:47 +08:00
}
func TestCpu_counts(t *testing.T) {
v, err := Counts(true)
2014-04-18 15:34:47 +08:00
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get CPU counts: %v", v)
}
}
2014-05-12 10:51:08 +08:00
func TestCPUTimeStat_String(t *testing.T) {
v := TimesStat{
2014-05-12 10:51:08 +08:00
CPU: "cpu0",
User: 100.1,
System: 200.1,
Idle: 300.1,
}
e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0}`
2014-05-12 10:51:08 +08:00
if e != fmt.Sprintf("%v", v) {
t.Errorf("CPUTimesStat string is invalid: %v", v)
}
}
2014-05-16 17:12:18 +08:00
func TestCpuInfo(t *testing.T) {
v, err := Info()
2014-05-16 17:12:18 +08:00
if err != nil {
t.Errorf("error %v", err)
}
if len(v) == 0 {
t.Errorf("could not get CPU Info")
}
2014-05-16 17:12:18 +08:00
for _, vv := range v {
2014-05-16 17:39:17 +08:00
if vv.ModelName == "" {
2014-05-16 17:12:18 +08:00
t.Errorf("could not get CPU Info: %v", vv)
}
}
}
2014-11-02 11:05:52 +08:00
func testCPUPercent(t *testing.T, percpu bool) {
numcpu := runtime.NumCPU()
testCount := 3
if runtime.GOOS != "windows" {
testCount = 100
v, err := Percent(time.Millisecond, percpu)
if err != nil {
t.Errorf("error %v", err)
}
// Skip CircleCI which CPU num is different
if os.Getenv("CIRCLECI") != "true" {
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
t.Fatalf("wrong number of entries from CPUPercent: %v", v)
}
}
2014-11-02 11:05:52 +08:00
}
for i := 0; i < testCount; i++ {
duration := time.Duration(10) * time.Microsecond
v, err := Percent(duration, percpu)
2014-11-02 11:05:52 +08:00
if err != nil {
t.Errorf("error %v", err)
}
for _, percent := range v {
// Check for slightly greater then 100% to account for any rounding issues.
if percent < 0.0 || percent > 100.0001*float64(numcpu) {
2014-11-02 11:05:52 +08:00
t.Fatalf("CPUPercent value is invalid: %f", percent)
}
}
}
}
func testCPUPercentLastUsed(t *testing.T, percpu bool) {
numcpu := runtime.NumCPU()
testCount := 10
if runtime.GOOS != "windows" {
testCount = 2
v, err := Percent(time.Millisecond, percpu)
if err != nil {
t.Errorf("error %v", err)
}
// Skip CircleCI which CPU num is different
if os.Getenv("CIRCLECI") != "true" {
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
t.Fatalf("wrong number of entries from CPUPercent: %v", v)
}
}
}
for i := 0; i < testCount; i++ {
v, err := Percent(0, percpu)
if err != nil {
t.Errorf("error %v", err)
}
time.Sleep(1 * time.Millisecond)
for _, percent := range v {
// Check for slightly greater then 100% to account for any rounding issues.
if percent < 0.0 || percent > 100.0001*float64(numcpu) {
t.Fatalf("CPUPercent value is invalid: %f", percent)
}
}
}
}
2014-11-02 11:05:52 +08:00
func TestCPUPercent(t *testing.T) {
testCPUPercent(t, false)
}
func TestCPUPercentPerCpu(t *testing.T) {
testCPUPercent(t, true)
}
func TestCPUPercentIntervalZero(t *testing.T) {
testCPUPercentLastUsed(t, false)
}
func TestCPUPercentIntervalZeroPerCPU(t *testing.T) {
testCPUPercentLastUsed(t, true)
}