2014-12-30 22:09:05 +09:00
|
|
|
package cpu
|
2014-04-18 16:34:47 +09:00
|
|
|
|
|
|
|
import (
|
2014-05-12 11:51:08 +09:00
|
|
|
"fmt"
|
2014-11-02 04:05:52 +01:00
|
|
|
"runtime"
|
2014-04-18 16:34:47 +09:00
|
|
|
"testing"
|
2014-11-02 04:05:52 +01:00
|
|
|
"time"
|
2014-04-18 16:34:47 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCpu_times(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v, err := CPUTimes(false)
|
2014-04-18 16:34:47 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
2014-08-15 19:09:43 +02:00
|
|
|
t.Error("could not get CPUs ", err)
|
2014-04-18 16:34:47 +09:00
|
|
|
}
|
2014-05-23 10:31:47 +09:00
|
|
|
empty := CPUTimesStat{}
|
2014-04-18 16:34:47 +09:00
|
|
|
for _, vv := range v {
|
2014-05-12 11:51:08 +09:00
|
|
|
if vv == empty {
|
2014-04-18 16:34:47 +09:00
|
|
|
t.Errorf("could not get CPU User: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCpu_counts(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v, err := CPUCounts(true)
|
2014-04-18 16:34:47 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if v == 0 {
|
|
|
|
t.Errorf("could not get CPU counts: %v", v)
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 11:51:08 +09:00
|
|
|
|
|
|
|
func TestCPUTimeStat_String(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v := CPUTimesStat{
|
2014-05-12 11:51:08 +09:00
|
|
|
CPU: "cpu0",
|
|
|
|
User: 100.1,
|
|
|
|
System: 200.1,
|
|
|
|
Idle: 300.1,
|
|
|
|
}
|
2015-02-13 23:11:27 +09:00
|
|
|
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,"guest_nice":0.0,"stolen":0.0}`
|
2014-05-12 11:51:08 +09:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("CPUTimesStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 18:12:18 +09:00
|
|
|
|
|
|
|
func TestCpuInfo(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v, err := CPUInfo()
|
2014-05-16 18:12:18 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2015-02-15 21:25:33 +09:00
|
|
|
if len(v) == 0 {
|
|
|
|
t.Errorf("could not get CPU Info")
|
|
|
|
}
|
2014-05-16 18:12:18 +09:00
|
|
|
for _, vv := range v {
|
2014-05-16 18:39:17 +09:00
|
|
|
if vv.ModelName == "" {
|
2014-05-16 18:12:18 +09:00
|
|
|
t.Errorf("could not get CPU Info: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 04:05:52 +01:00
|
|
|
|
|
|
|
func testCPUPercent(t *testing.T, percpu bool) {
|
|
|
|
numcpu := runtime.NumCPU()
|
2015-03-11 23:00:06 +09:00
|
|
|
testCount := 3
|
|
|
|
|
2015-03-07 21:52:43 +09:00
|
|
|
if runtime.GOOS != "windows" {
|
2015-03-11 23:00:06 +09:00
|
|
|
testCount = 100
|
2015-03-07 21:52:43 +09:00
|
|
|
v, err := CPUPercent(time.Millisecond, percpu)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
|
|
|
|
t.Fatalf("wrong number of entries from CPUPercent: %v", v)
|
|
|
|
}
|
2014-11-02 04:05:52 +01:00
|
|
|
}
|
2015-03-11 23:00:06 +09:00
|
|
|
for i := 0; i < testCount; i++ {
|
2014-11-12 11:17:55 +09:00
|
|
|
duration := time.Duration(10) * time.Microsecond
|
|
|
|
v, err := CPUPercent(duration, percpu)
|
2014-11-02 04:05:52 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
for _, percent := range v {
|
2015-10-18 20:53:01 -07:00
|
|
|
// Check for slightly greater then 100% to account for any rounding issues.
|
|
|
|
if percent < 0.0 || percent > 100.0001*float64(numcpu) {
|
2014-11-02 04:05:52 +01:00
|
|
|
t.Fatalf("CPUPercent value is invalid: %f", percent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCPUPercent(t *testing.T) {
|
|
|
|
testCPUPercent(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCPUPercentPerCpu(t *testing.T) {
|
|
|
|
testCPUPercent(t, true)
|
|
|
|
}
|