system_test.go 866 Bytes
Newer Older
liming6's avatar
liming6 committed
1
2
3
4
package utils

import (
	"testing"
liming6's avatar
liming6 committed
5
6
7
	"time"

	"github.com/shirou/gopsutil/v4/cpu"
8
	"github.com/shirou/gopsutil/v4/process"
liming6's avatar
liming6 committed
9
10
11
12
13
14
15
16
17
)

func TestGetSysUsers(t *testing.T) {
	a, e := cpu.Percent(0, false)
	if e != nil {
		t.Error(e)
	}
	t.Logf("%+v", a)
}
liming6's avatar
liming6 committed
18
19

func TestGetSysInfo(t *testing.T) {
20
	start := time.Now()
liming6's avatar
liming6 committed
21
	info, err := GetSysInfo()
22
	d := time.Since(start)
liming6's avatar
liming6 committed
23
24
25
	if err != nil {
		t.Error(err)
	}
26
	t.Logf("%d: %+v", d.Microseconds(), *info)
liming6's avatar
liming6 committed
27
28
29
30
31
32
33
34
35
36
37
38
39
}

func BenchmarkGetSysInfo(b *testing.B) {
	result := make([]float64, b.N)
	for i := 0; i < b.N; i++ {
		info, err := GetSysInfo()
		if err != nil {
			b.Error(err)
		}
		result[i] = info.CPUPercent
	}
	b.Logf("%+v", result)
}
40
41
42
43
44
45
46
47

func TestCPUTime(t *testing.T) {
	p, _ := process.NewProcess(1)
	tt, _ := p.Times()
	t.Logf("%s", tt.String())
	tt1 := time.Duration(int((tt.User + tt.System))) * time.Second
	t.Logf("%s", tt1.String())
}