2014-12-30 21:09:05 +08:00
|
|
|
package load
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2021-12-05 05:29:38 +08:00
|
|
|
"errors"
|
2014-05-12 10:51:08 +08:00
|
|
|
"fmt"
|
2014-04-18 15:34:47 +08:00
|
|
|
"testing"
|
2020-08-30 00:30:18 +08:00
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2020-11-02 21:51:13 +08:00
|
|
|
func skipIfNotImplementedErr(t testing.TB, err error) {
|
2021-12-05 05:29:38 +08:00
|
|
|
if errors.Is(err, common.ErrNotImplementedError) {
|
2020-08-30 00:30:18 +08:00
|
|
|
t.Skip("not implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 15:34:47 +08:00
|
|
|
func TestLoad(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := Avg()
|
2020-08-30 00:30:18 +08:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
empty := &AvgStat{}
|
2014-05-12 10:51:08 +08:00
|
|
|
if v == empty {
|
2014-04-18 15:34:47 +08:00
|
|
|
t.Errorf("error load: %v", v)
|
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
t.Log(v)
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
|
|
|
|
func TestLoadAvgStat_String(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := AvgStat{
|
2014-05-12 10:51:08 +08:00
|
|
|
Load1: 10.1,
|
|
|
|
Load5: 20.1,
|
|
|
|
Load15: 30.1,
|
|
|
|
}
|
|
|
|
e := `{"load1":10.1,"load5":20.1,"load15":30.1}`
|
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("LoadAvgStat string is invalid: %v", v)
|
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
t.Log(e)
|
2014-05-12 10:51:08 +08:00
|
|
|
}
|
2016-02-20 21:52:16 +08:00
|
|
|
|
|
|
|
func TestMisc(t *testing.T) {
|
|
|
|
v, err := Misc()
|
2020-08-30 00:30:18 +08:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2016-02-20 21:52:16 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
empty := &MiscStat{}
|
|
|
|
if v == empty {
|
|
|
|
t.Errorf("error load: %v", v)
|
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
t.Log(v)
|
2016-02-20 21:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiscStatString(t *testing.T) {
|
|
|
|
v := MiscStat{
|
2019-05-05 10:29:20 +08:00
|
|
|
ProcsTotal: 4,
|
2020-07-02 09:40:58 +08:00
|
|
|
ProcsCreated: 5,
|
2016-02-20 21:52:16 +08:00
|
|
|
ProcsRunning: 1,
|
|
|
|
ProcsBlocked: 2,
|
|
|
|
Ctxt: 3,
|
|
|
|
}
|
2020-07-02 09:40:58 +08:00
|
|
|
e := `{"procsTotal":4,"procsCreated":5,"procsRunning":1,"procsBlocked":2,"ctxt":3}`
|
2016-02-20 21:52:16 +08:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("TestMiscString string is invalid: %v", v)
|
|
|
|
}
|
2017-10-04 21:34:47 +08:00
|
|
|
t.Log(e)
|
2016-02-20 21:52:16 +08:00
|
|
|
}
|
2020-11-02 21:51:13 +08:00
|
|
|
|
|
|
|
func BenchmarkLoad(b *testing.B) {
|
|
|
|
loadAvg := func(t testing.TB) {
|
|
|
|
v, err := Avg()
|
|
|
|
skipIfNotImplementedErr(t, err)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
empty := &AvgStat{}
|
|
|
|
if v == empty {
|
|
|
|
t.Errorf("error load: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Run("FirstCall", func(b *testing.B) {
|
|
|
|
loadAvg(b)
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("SubsequentCalls", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
loadAvg(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|