1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-24 13:48:56 +08:00
shirou_gopsutil/load/load_test.go

97 lines
1.7 KiB
Go
Raw Permalink Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2014-12-30 22:09:05 +09:00
package load
2014-04-18 16:34:47 +09:00
import (
2021-12-04 22:29:38 +01:00
"errors"
2014-05-12 11:51:08 +09:00
"fmt"
2014-04-18 16:34:47 +09:00
"testing"
"github.com/shirou/gopsutil/v4/internal/common"
2014-04-18 16:34:47 +09:00
)
func skipIfNotImplementedErr(t testing.TB, err error) {
2021-12-04 22:29:38 +01:00
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}
func TestAvg(t *testing.T) {
v, err := Avg()
skipIfNotImplementedErr(t, err)
2014-04-18 16:34:47 +09:00
if err != nil {
t.Errorf("error %v", err)
}
empty := &AvgStat{}
2014-05-12 11:51:08 +09:00
if v == empty {
2014-04-18 16:34:47 +09:00
t.Errorf("error load: %v", v)
}
t.Log(v)
2014-04-18 16:34:47 +09:00
}
2014-05-12 11:51:08 +09:00
func TestAvgStat_String(t *testing.T) {
v := AvgStat{
2014-05-12 11:51:08 +09: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)
}
t.Log(e)
2014-05-12 11:51:08 +09:00
}
func TestMisc(t *testing.T) {
v, err := Misc()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
empty := &MiscStat{}
if v == empty {
t.Errorf("error load: %v", v)
}
t.Log(v)
}
func TestMiscStatString(t *testing.T) {
v := MiscStat{
2019-05-05 10:29:20 +08:00
ProcsTotal: 4,
2020-07-02 11:40:58 +10:00
ProcsCreated: 5,
ProcsRunning: 1,
ProcsBlocked: 2,
Ctxt: 3,
}
2020-07-02 11:40:58 +10:00
e := `{"procsTotal":4,"procsCreated":5,"procsRunning":1,"procsBlocked":2,"ctxt":3}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("TestMiscString string is invalid: %v", v)
}
t.Log(e)
}
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)
}
})
}