github.com/dubbogo/gost@v1.14.0/runtime/sys_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxruntime
    19  
    20  import (
    21  	"runtime"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  import (
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestSysStat(t *testing.T) {
    31  	t.Logf("current os cpu number %d", GetCPUNum())
    32  	total, used, free, usedPercent := GetMemoryStat()
    33  	t.Logf("memory: limit %d bytes, used %d bytes, free %d bytes, usedPercent %f", total, used, free, usedPercent)
    34  	t.Logf("current prcess thread number %d", GetThreadNum())
    35  	go func() {
    36  		time.Sleep(10e9)
    37  	}()
    38  
    39  	grNum := GetGoroutineNum()
    40  	if grNum < 2 {
    41  		t.Errorf("current prcess goroutine number %d", grNum)
    42  	}
    43  
    44  	cpu, err := GetProcessCPUStat()
    45  	if err != nil {
    46  		t.Errorf("GetProcessCPUStat() = error %+v", err)
    47  	}
    48  	t.Logf("process cpu stat %v", cpu)
    49  
    50  	size := 100 * 1024 * 1024
    51  	arr := make([]byte, size)
    52  	for idx := range arr {
    53  		arr[idx] = byte(idx / 255)
    54  	}
    55  	memoryStat, err := GetProcessMemoryStat()
    56  	if err != nil {
    57  		t.Errorf("GetProcessMemoryStat() = error %+v", err)
    58  	}
    59  	// t.Logf("process memory usage stat %v", memoryStat)
    60  	if memoryStat <= uint64(size) {
    61  		t.Errorf("memory usage stat %d < %d", memoryStat, size)
    62  	}
    63  
    64  	memoryUsage, err := GetProcessMemoryPercent()
    65  	if err != nil {
    66  		t.Errorf("GetProcessMemoryPercent() = error %+v", err)
    67  	}
    68  	t.Logf("process memory usage percent %v", memoryUsage)
    69  
    70  	if IsCgroup() {
    71  		memoryLimit, err := GetCgroupMemoryLimit()
    72  		if err != nil {
    73  			t.Errorf("GetCgroupMemoryLimit() = error %+v", err)
    74  		}
    75  		t.Logf("CGroupMemoryLimit() = %d", memoryLimit)
    76  
    77  		memoryPercent, err := GetCgroupProcessMemoryPercent()
    78  		if err != nil {
    79  			t.Errorf("GetCgroupProcessMemoryPercent(ps:%d) = error %+v", CurrentPID, err)
    80  		}
    81  		t.Logf("GetCgroupProcessMemoryPercent(ps:%d) = %+v", CurrentPID, memoryPercent)
    82  	}
    83  }
    84  
    85  func Test_readLinesFromFile(t *testing.T) {
    86  	assert.Equal(t, readLinesFromFile("/dev/null"), []string{})
    87  }
    88  
    89  func Test_numCPU(t *testing.T) {
    90  	cpus, err := numCPU()
    91  	if err != nil {
    92  		assert.Equal(t, runtime.NumCPU(), cpus)
    93  		return
    94  	}
    95  
    96  	if !isContainer() {
    97  		assert.Equal(t, runtime.NumCPU(), cpus)
    98  		return
    99  	}
   100  
   101  	period, err := readUint(_cpuPeriodPath)
   102  	if err != nil {
   103  		assert.Equal(t, runtime.NumCPU(), cpus)
   104  	}
   105  
   106  	quota, err := readUint(_cpuQuotaPath)
   107  	if err != nil {
   108  		assert.Equal(t, runtime.NumCPU(), cpus)
   109  	}
   110  
   111  	assert.Equal(t, int(quota/period), cpus)
   112  }
   113  
   114  func Test_readUint(t *testing.T) {
   115  	uInt, err := readUint("/dev/null")
   116  	assert.Equal(t, uInt, uint64(0))
   117  	assert.True(t, err != nil)
   118  }