File: load_test.go

package info (click to toggle)
golang-github-shirou-gopsutil 4.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,824 kB
  • sloc: makefile: 76; ansic: 19; sh: 11
file content (96 lines) | stat: -rw-r--r-- 1,725 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: BSD-3-Clause
package load

import (
	"errors"
	"fmt"
	"testing"

	"github.com/shirou/gopsutil/v4/internal/common"
)

func skipIfNotImplementedErr(t testing.TB, err error) {
	if errors.Is(err, common.ErrNotImplementedError) {
		t.Skip("not implemented")
	}
}

func TestAvg(t *testing.T) {
	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)
	}
	t.Log(v)
}

func TestAvgStat_String(t *testing.T) {
	v := AvgStat{
		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)
}

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{
		ProcsTotal:   4,
		ProcsCreated: 5,
		ProcsRunning: 1,
		ProcsBlocked: 2,
		Ctxt:         3,
	}
	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)
		}
	})
}