github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/os_linux_test.go (about)

     1  // +build linux
     2  
     3  package kgo
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/stretchr/testify/assert"
     8  	"io/ioutil"
     9  	"net"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestOS_Linux_IsLinux(t *testing.T) {
    16  	res := KOS.IsLinux()
    17  	assert.True(t, res)
    18  }
    19  
    20  func BenchmarkOS_Linux_IsLinux(b *testing.B) {
    21  	b.ResetTimer()
    22  	for i := 0; i < b.N; i++ {
    23  		KOS.IsLinux()
    24  	}
    25  }
    26  
    27  func TestOS_Linux_MemoryUsage(t *testing.T) {
    28  	var used, free, total uint64
    29  
    30  	// 虚拟内存
    31  	used, free, total = KOS.MemoryUsage(true)
    32  	assert.Greater(t, int(used), 1)
    33  	assert.Greater(t, int(free), 1)
    34  	assert.Greater(t, int(total), 1)
    35  
    36  	// 真实物理内存
    37  	used, free, total = KOS.MemoryUsage(false)
    38  	assert.Greater(t, int(used), 1)
    39  	assert.Greater(t, int(free), 1)
    40  	assert.Greater(t, int(total), 1)
    41  }
    42  
    43  func BenchmarkOS_Linux_MemoryUsage_Virtual(b *testing.B) {
    44  	b.ResetTimer()
    45  	for i := 0; i < b.N; i++ {
    46  		KOS.MemoryUsage(true)
    47  	}
    48  }
    49  
    50  func BenchmarkOS_Linux_MemoryUsage_Physic(b *testing.B) {
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		KOS.MemoryUsage(false)
    54  	}
    55  }
    56  
    57  func TestOS_Linux_CpuUsage(t *testing.T) {
    58  	var user, idle, total uint64
    59  	user, idle, total = KOS.CpuUsage()
    60  	assert.Greater(t, int(user), 1)
    61  	assert.Greater(t, int(idle), 1)
    62  	assert.Greater(t, int(total), 1)
    63  }
    64  
    65  func BenchmarkOS_Linux_CpuUsage(b *testing.B) {
    66  	b.ResetTimer()
    67  	for i := 0; i < b.N; i++ {
    68  		_, _, _ = KOS.CpuUsage()
    69  	}
    70  }
    71  
    72  func TestOS_Linux_DiskUsage(t *testing.T) {
    73  	var used, free, total uint64
    74  	used, free, total = KOS.DiskUsage("/")
    75  	assert.Greater(t, int(used), 1)
    76  	assert.Greater(t, int(free), 1)
    77  	assert.Greater(t, int(total), 1)
    78  }
    79  
    80  func BenchmarkOS_Linux_DiskUsage(b *testing.B) {
    81  	b.ResetTimer()
    82  	for i := 0; i < b.N; i++ {
    83  		_, _, _ = KOS.DiskUsage("/")
    84  	}
    85  }
    86  
    87  func TestOS_Linux_Uptime(t *testing.T) {
    88  	res, err := KOS.Uptime()
    89  	assert.Greater(t, int(res), 1)
    90  	assert.Nil(t, err)
    91  }
    92  
    93  func BenchmarkOS_Linux_Uptime(b *testing.B) {
    94  	b.ResetTimer()
    95  	for i := 0; i < b.N; i++ {
    96  		_, _ = KOS.Uptime()
    97  	}
    98  }
    99  
   100  func TestOS_Linux_GetBiosInfo(t *testing.T) {
   101  	res := KOS.GetBiosInfo()
   102  	assert.NotNil(t, res)
   103  }
   104  
   105  func BenchmarkOS_Linux_GetBiosInfo(b *testing.B) {
   106  	b.ResetTimer()
   107  	for i := 0; i < b.N; i++ {
   108  		KOS.GetBiosInfo()
   109  	}
   110  }
   111  
   112  func TestOS_Linux_GetBoardInfo(t *testing.T) {
   113  	res := KOS.GetBoardInfo()
   114  	assert.NotNil(t, res)
   115  }
   116  
   117  func BenchmarkOS_Linux_GetBoardInfo(b *testing.B) {
   118  	b.ResetTimer()
   119  	for i := 0; i < b.N; i++ {
   120  		KOS.GetBoardInfo()
   121  	}
   122  }
   123  
   124  func TestOS_Linux_GetCpuInfo(t *testing.T) {
   125  	res := KOS.GetCpuInfo()
   126  	assert.NotNil(t, res)
   127  	assert.NotEmpty(t, res.Vendor)
   128  	assert.NotEmpty(t, res.Model)
   129  }
   130  
   131  func BenchmarkOS_Linux_GetCpuInfo(b *testing.B) {
   132  	b.ResetTimer()
   133  	for i := 0; i < b.N; i++ {
   134  		KOS.GetCpuInfo()
   135  	}
   136  }
   137  
   138  func TestOS_Linux_GetProcessExecPath(t *testing.T) {
   139  	var res string
   140  
   141  	pid := os.Getpid()
   142  	res = KOS.GetProcessExecPath(pid)
   143  	assert.NotEmpty(t, res)
   144  }
   145  
   146  func BenchmarkOS_Linux_GetProcessExecPath(b *testing.B) {
   147  	b.ResetTimer()
   148  	pid := os.Getpid()
   149  	for i := 0; i < b.N; i++ {
   150  		KOS.GetProcessExecPath(pid)
   151  	}
   152  }
   153  
   154  func TestOS_Linux_GetPidByPort(t *testing.T) {
   155  	time.AfterFunc(time.Millisecond*250, func() {
   156  		res := KOS.GetPidByPort(8899)
   157  		assert.Greater(t, res, 1)
   158  
   159  		KOS.GetPidByPort(80)
   160  	})
   161  
   162  	//发送消息
   163  	time.AfterFunc(time.Millisecond*500, func() {
   164  		conn, err := net.Dial("tcp", ":8899")
   165  		assert.Nil(t, err)
   166  
   167  		defer func() {
   168  			_ = conn.Close()
   169  		}()
   170  
   171  		_, err = fmt.Fprintf(conn, helloEng)
   172  		assert.Nil(t, err)
   173  	})
   174  
   175  	//开启监听端口
   176  	l, err := net.Listen("tcp", ":8899")
   177  	assert.Nil(t, err)
   178  	defer func() {
   179  		_ = l.Close()
   180  	}()
   181  
   182  	for {
   183  		conn, err := l.Accept()
   184  		if err != nil {
   185  			return
   186  		}
   187  		defer func() {
   188  			_ = conn.Close()
   189  		}()
   190  
   191  		//接收
   192  		buf, err := ioutil.ReadAll(conn)
   193  		assert.Nil(t, err)
   194  
   195  		msg := string(buf[:])
   196  		assert.Equal(t, msg, helloEng)
   197  		return
   198  	}
   199  }
   200  
   201  func BenchmarkOS_Linux_GetPidByPort(b *testing.B) {
   202  	b.ResetTimer()
   203  	for i := 0; i < b.N; i++ {
   204  		KOS.GetPidByPort(8899)
   205  	}
   206  }