github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/cpu/cpu_test.go (about)

     1  package cpu
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/system/common"
     6  
     7  	"os"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func skipIfNotImplementedErr(t *testing.T, err error) {
    14  	if err == common.ErrNotImplementedError {
    15  		t.Skip("not implemented")
    16  	}
    17  }
    18  
    19  func TestCpu_times(t *testing.T) {
    20  	v, err := Times(false)
    21  	skipIfNotImplementedErr(t, err)
    22  	if err != nil {
    23  		t.Errorf("error %v", err)
    24  	}
    25  	if len(v) == 0 {
    26  		t.Error("could not get CPUs ", err)
    27  	}
    28  	empty := TimesStat{}
    29  	for _, vv := range v {
    30  		if vv == empty {
    31  			t.Errorf("could not get CPU User: %v", vv)
    32  		}
    33  	}
    34  
    35  	// test sum of per cpu stats is within margin of error for cpu total stats
    36  	cpuTotal, err := Times(false)
    37  	skipIfNotImplementedErr(t, err)
    38  	if err != nil {
    39  		t.Errorf("error %v", err)
    40  	}
    41  	if len(cpuTotal) == 0 {
    42  		t.Error("could not get CPUs", err)
    43  	}
    44  	perCPU, err := Times(true)
    45  	skipIfNotImplementedErr(t, err)
    46  	if err != nil {
    47  		t.Errorf("error %v", err)
    48  	}
    49  	if len(perCPU) == 0 {
    50  		t.Error("could not get CPUs", err)
    51  	}
    52  	var perCPUUserTimeSum float64
    53  	var perCPUSystemTimeSum float64
    54  	var perCPUIdleTimeSum float64
    55  	for _, pc := range perCPU {
    56  		perCPUUserTimeSum += pc.User
    57  		perCPUSystemTimeSum += pc.System
    58  		perCPUIdleTimeSum += pc.Idle
    59  	}
    60  	t.Log(cpuTotal[0])
    61  
    62  }
    63  
    64  func TestCpu_counts(t *testing.T) {
    65  	v, err := Counts(true)
    66  	skipIfNotImplementedErr(t, err)
    67  	if err != nil {
    68  		t.Errorf("error %v", err)
    69  	}
    70  	if v == 0 {
    71  		t.Errorf("could not get logical CPU counts: %v", v)
    72  	}
    73  	t.Logf("logical cores: %d", v)
    74  	v, err = Counts(false)
    75  	skipIfNotImplementedErr(t, err)
    76  	if err != nil {
    77  		t.Errorf("error %v", err)
    78  	}
    79  	if v == 0 {
    80  		t.Errorf("could not get physical CPU counts: %v", v)
    81  	}
    82  	t.Logf("physical cores: %d", v)
    83  }
    84  
    85  func TestCPUTimeStat_String(t *testing.T) {
    86  	v := TimesStat{
    87  		CPU:    "cpu0",
    88  		User:   100.1,
    89  		System: 200.1,
    90  		Idle:   300.1,
    91  	}
    92  	e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0}`
    93  	if e != fmt.Sprintf("%v", v) {
    94  		t.Errorf("CPUTimesStat string is invalid: %v", v)
    95  	}
    96  }
    97  
    98  func TestCpuInfo(t *testing.T) {
    99  	v, err := Info()
   100  	skipIfNotImplementedErr(t, err)
   101  	if len(v) != 0 {
   102  		for _, vv := range v {
   103  			t.Logf("vendor: %v", vv)
   104  		}
   105  	}
   106  
   107  }
   108  
   109  func testCPUPercent(t *testing.T, percpu bool) {
   110  	numcpu := runtime.NumCPU()
   111  	testCount := 3
   112  
   113  	if runtime.GOOS != "windows" {
   114  		testCount = 100
   115  		v, err := Percent(time.Millisecond, percpu)
   116  		skipIfNotImplementedErr(t, err)
   117  		if err != nil {
   118  			t.Errorf("error %v", err)
   119  		}
   120  		// Skip CircleCI which CPU num is different
   121  		if os.Getenv("CIRCLECI") != "true" {
   122  			if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
   123  				t.Fatalf("wrong number of entries from CPUPercent: %v", v)
   124  			}
   125  		}
   126  	}
   127  	for i := 0; i < testCount; i++ {
   128  		duration := time.Duration(10) * time.Microsecond
   129  		v, err := Percent(duration, percpu)
   130  		skipIfNotImplementedErr(t, err)
   131  		if err != nil {
   132  			t.Errorf("error %v", err)
   133  		}
   134  		for _, percent := range v {
   135  			// Check for slightly greater then 100% to account for any rounding issues.
   136  			if percent < 0.0 || percent > 100.0001*float64(numcpu) {
   137  				t.Fatalf("CPUPercent value is invalid: %f", percent)
   138  			}
   139  		}
   140  	}
   141  }
   142  
   143  func testCPUPercentLastUsed(t *testing.T, percpu bool) {
   144  
   145  	numcpu := runtime.NumCPU()
   146  	testCount := 10
   147  
   148  	if runtime.GOOS != "windows" {
   149  		testCount = 2
   150  		v, err := Percent(time.Millisecond, percpu)
   151  		skipIfNotImplementedErr(t, err)
   152  		if err != nil {
   153  			t.Errorf("error %v", err)
   154  		}
   155  		// Skip CircleCI which CPU num is different
   156  		if os.Getenv("CIRCLECI") != "true" {
   157  			if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) {
   158  				t.Fatalf("wrong number of entries from CPUPercent: %v", v)
   159  			}
   160  		}
   161  	}
   162  	for i := 0; i < testCount; i++ {
   163  		v, err := Percent(0, percpu)
   164  		skipIfNotImplementedErr(t, err)
   165  		if err != nil {
   166  			t.Errorf("error %v", err)
   167  		}
   168  		time.Sleep(1 * time.Millisecond)
   169  		for _, percent := range v {
   170  			// Check for slightly greater then 100% to account for any rounding issues.
   171  			if percent < 0.0 || percent > 100.0001*float64(numcpu) {
   172  				t.Fatalf("CPUPercent value is invalid: %f", percent)
   173  			}
   174  		}
   175  	}
   176  
   177  }
   178  
   179  func TestCPUPercent(t *testing.T) {
   180  	testCPUPercent(t, false)
   181  }
   182  
   183  func TestCPUPercentPerCpu(t *testing.T) {
   184  	testCPUPercent(t, true)
   185  }
   186  
   187  func TestCPUPercentIntervalZero(t *testing.T) {
   188  	testCPUPercentLastUsed(t, false)
   189  }
   190  
   191  func TestCPUPercentIntervalZeroPerCPU(t *testing.T) {
   192  	testCPUPercentLastUsed(t, true)
   193  }