...
1 package load
2
3 import (
4 "errors"
5 "fmt"
6 "testing"
7
8 "github.com/shirou/gopsutil/internal/common"
9 )
10
11 func skipIfNotImplementedErr(t testing.TB, err error) {
12 if errors.Is(err, common.ErrNotImplementedError) {
13 t.Skip("not implemented")
14 }
15 }
16
17 func TestLoad(t *testing.T) {
18 v, err := Avg()
19 skipIfNotImplementedErr(t, err)
20 if err != nil {
21 t.Errorf("error %v", err)
22 }
23
24 empty := &AvgStat{}
25 if v == empty {
26 t.Errorf("error load: %v", v)
27 }
28 t.Log(v)
29 }
30
31 func TestLoadAvgStat_String(t *testing.T) {
32 v := AvgStat{
33 Load1: 10.1,
34 Load5: 20.1,
35 Load15: 30.1,
36 }
37 e := `{"load1":10.1,"load5":20.1,"load15":30.1}`
38 if e != fmt.Sprintf("%v", v) {
39 t.Errorf("LoadAvgStat string is invalid: %v", v)
40 }
41 t.Log(e)
42 }
43
44 func TestMisc(t *testing.T) {
45 v, err := Misc()
46 skipIfNotImplementedErr(t, err)
47 if err != nil {
48 t.Errorf("error %v", err)
49 }
50
51 empty := &MiscStat{}
52 if v == empty {
53 t.Errorf("error load: %v", v)
54 }
55 t.Log(v)
56 }
57
58 func TestMiscStatString(t *testing.T) {
59 v := MiscStat{
60 ProcsTotal: 4,
61 ProcsCreated: 5,
62 ProcsRunning: 1,
63 ProcsBlocked: 2,
64 Ctxt: 3,
65 }
66 e := `{"procsTotal":4,"procsCreated":5,"procsRunning":1,"procsBlocked":2,"ctxt":3}`
67 if e != fmt.Sprintf("%v", v) {
68 t.Errorf("TestMiscString string is invalid: %v", v)
69 }
70 t.Log(e)
71 }
72
73 func BenchmarkLoad(b *testing.B) {
74 loadAvg := func(t testing.TB) {
75 v, err := Avg()
76 skipIfNotImplementedErr(t, err)
77 if err != nil {
78 t.Errorf("error %v", err)
79 }
80 empty := &AvgStat{}
81 if v == empty {
82 t.Errorf("error load: %v", v)
83 }
84 }
85
86 b.Run("FirstCall", func(b *testing.B) {
87 loadAvg(b)
88 })
89
90 b.Run("SubsequentCalls", func(b *testing.B) {
91 for i := 0; i < b.N; i++ {
92 loadAvg(b)
93 }
94 })
95 }
96
View as plain text