github.com/songzhibin97/gkit@v1.2.13/sys/cpu/cpu_test.go (about)

     1  package cpu
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_CPUUsage(t *testing.T) {
    12  	var stat Stat
    13  	ReadStat(&stat)
    14  	fmt.Println(stat)
    15  	time.Sleep(time.Millisecond * 1000)
    16  	for i := 0; i < 6; i++ {
    17  		time.Sleep(time.Millisecond * 500)
    18  		ReadStat(&stat)
    19  		if stat.Usage == 0 {
    20  			t.Fatalf("get cpu failed!cpu usage is zero!")
    21  		}
    22  		fmt.Println(stat)
    23  	}
    24  }
    25  
    26  func TestStat(t *testing.T) {
    27  	time.Sleep(time.Second * 2)
    28  	var s Stat
    29  	var i Info
    30  	ReadStat(&s)
    31  	i = GetInfo()
    32  
    33  	assert.NotZero(t, s.Usage)
    34  	assert.NotZero(t, i.Frequency)
    35  	assert.NotZero(t, i.Quota)
    36  }