2014-05-20 15:38:20 +08:00
|
|
|
package test
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-05-12 10:51:08 +08:00
|
|
|
"fmt"
|
2014-04-18 15:34:47 +08:00
|
|
|
"testing"
|
2014-05-20 15:38:20 +08:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCpu_times(t *testing.T) {
|
2014-05-20 15:38:20 +08:00
|
|
|
v, err := gopsutil.CPUTimes(false)
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Errorf("could not get CPUs ", err)
|
|
|
|
}
|
2014-05-20 15:38:20 +08:00
|
|
|
empty := gopsutil.CPUTimesStat{}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCpu_counts(t *testing.T) {
|
2014-05-20 15:38:20 +08:00
|
|
|
v, err := gopsutil.CPUCounts(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) {
|
2014-05-20 15:38:20 +08:00
|
|
|
v := gopsutil.CPUTimesStat{
|
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,"iowait":0,"irq":0,"softirq":0,"steal":0,"guest":0,"guest_nice":0,"stolen":0}`
|
|
|
|
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) {
|
2014-05-20 15:38:20 +08:00
|
|
|
v, err := gopsutil.CPUInfo()
|
2014-05-16 17:12:18 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|