Golang测试类编写

Posted by zengchengjie on Wednesday, October 13, 2021

单元测试

Go 语言推荐测试文件和源代码文件放在一块,测试文件以 _test.go 结尾。

  • 测试用例名称一般命名为 Test 加上待测试的方法名。
  • 测试用的参数有且只有一个,在这里是 t *testing.T
  • 基准测试(benchmark)的参数是 *testing.B,TestMain 的参数是 *testing.M 类型。

如下所示:

func TestPrint(t *testing.T) {
	fmt.Println("this is a test")
}

执行成功后,在goland中显示如下

值得一提的是,在单元测试中,有时候会报一个"no tests were run"的错误,但是看似又不影响运行:

比如:

func TestPrint(t *testing.T) {
	fmt.Printf("this is a %s","test")
}

则会报错

不知何错,遂Google,看到Stack Overflow上有哥们说出了解决办法:在打印中,增加换行:

Instead of fmt.Printf(... in AddTwoNumbers try either fmt.Println(... or fmt.Printf("foo\n')

The absence of the newline in the output of your AddTwoNumbers method is is causing the format of the test execution outputs to not have each test in a new line. The test runner is not being able to interpret that a test was run. Adding that newline, keeps a clean output.

修改代码增加换行即可:

func TestPrint(t *testing.T) {
	//fmt.Printf("this is a %s","test")
	fmt.Printf("this is a %s\n","test")
	fmt.Println("this is a","test")
}

基准测试

TestMain

压测

压测工具Apache bench(ab测试)

官方链接 https://cloud.tencent.com/developer/article/1574108

Linux 下载

 yum -y install httpd-tools  //centos
 apt-get install apache2-utils //ubuntu
 ab -V #查看版本

使用命令:

 ab -c1000 -n100000 http://127.0.0.1/

向目标地址发送 100000次请求,并发数为1000

[root@localhost ~]# ab -c100 -n10000 http://127.0.0.1:9501/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests  //已经完成的请求数
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        EasySwoole   #服务器名
Server Hostname:        127.0.0.1  #请求的URL主机名
Server Port:            9501 #端口号

Document Path:          / #请求路径
Document Length:        44 bytes #响应数据长度

Concurrency Level:      100  #并发数,我们自己设置的-c参数
Time taken for tests:   0.629 seconds  #请求完成时间
Complete requests:      10000 #完成请求数
Failed requests:        0 #错误请求数
Write errors:           0 #写入错误次数
Total transferred:      2050000 bytes #请求长度总和
HTML transferred:       440000 bytes #html响应总长度(去除了响应头的长度)
Requests per second:    15909.13 [#/sec] (mean) #每秒处理的请求数
Time per request:       6.286 [ms] (mean) #用户平均请求等待时间
Time per request:       0.063 [ms] (mean, across all concurrent requests) #服务器平均处理时间
Transfer rate:          3184.93 [Kbytes/sec] received  #带宽传输速度

Connection Times (ms)  #连接处理时间
              min  mean[+/-sd] median   max
Connect:        0    1   1.3      1      51
Processing:     0    5   5.6      4      56
Waiting:        0    4   5.5      3      55
Total:          0    6   5.8      5      57

Percentage of the requests served within a certain time (ms) 
  50%      5  #50%的请求在5ms内返回
  66%      6
  75%      6
  80%      7
  90%      8
  95%     10
  98%     24
  99%     36  #99%的请求在36ms内返回
 100%     57 (longest request)
[root@localhost ~]#