内容简介:版权声明:本文为博主尹成联系QQ77025077,微信18510341407原创文章,欢迎转载侵权不究。 https://blog.csdn.net/yincheng01/article/details/84136139
版权声明:本文为博主尹成联系QQ77025077,微信18510341407原创文章,欢迎转载侵权不究。 https://blog.csdn.net/yincheng01/article/details/84136139
压力测试概述
压力测试用来检测函数(方法)的性能,和编写单元功能测试的方法类似,但需要注意以下几点:
- 文件名命名规则:xxx_test.go
- 函数名命名规则:func BenchXxx(b *testing.B),其中XXX可以是任意字母数字的组合,但是首字母不能是小写字母
- 函数内必须使用b.N进行轮询测试
- 函数内可以选择使用b.ReportAllocs()汇报内存开销
- 在GoLandIDE中你可以在待测包上右键,Run->gobentch xxx,以执行整包的压力测试,默认从上向下依次执行所有
- 终端执行当前包下的所有压力测试:
go test -bench=.
- 终端执行多次求平均值
go test -bench=. -count=3
定义待测的 工具 函数
这里给出了斐波那契数列的递归和非递归两种算法实现
//获取斐波那契数列第n项的递归实现
//1,1,2,3,5,8,13,21,34,55
func GetFibonacci1(n int) int {
if n == 0 || n == 1 {
return 1
} else {
return GetFibonacci1(n-1) + GetFibonacci1(n-2)
}
}
//获取斐波那契数列第n项的非递归实现
//1,1,2,3,5,8,13,21,34,55
func GetFibonacci2(n int) int {
x, y := 1, 1
for i := 0; i < n; i++ {
x, y = y, x+y
}
return x
}
定义测试用例
//导入测试工具包
import "testing"
//测试用例1:多次测试函数GetFibonacci1,获得平均执行时间
func BenchmarkGetFibonacci1(b *testing.B) {
b.Log("BenchmarkGetFibonacci1")
//汇报内存开销
b.ReportAllocs()
for i := 0; i < b.N; i++ {
GetFibonacci1(10)
}
}
//测试用例2:多次测试函数GetFibonacci2,获得平均执行时间
func BenchmarkGetFibonacci2(b *testing.B) {
b.Log("BenchmarkGetFibonacci2")
//汇报内存开销
b.ReportAllocs()
for i := 0; i < b.N; i++ {
GetFibonacci2(10)
}
}
执行结果
不难看出,算法2的执行效率6.55纳秒/次要远远优于算法1的503纳秒/次
定义待测试的结构体
//导入包
import (
"encoding/json"
"os"
"fmt"
)
//定义待测的结构体
type Student struct {
Name string
Age int
}
//将当前对象存入JSON文件
func (s *Student) Save() error {
dstFile, _ := os.OpenFile("d:/temp/student.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
defer dstFile.Close()
//fmt.Println("Save:s=",s)
encoder := json.NewEncoder(dstFile)
err := encoder.Encode(s)
if err != nil {
fmt.Println("保存失败,err=", err)
return err
}
//fmt.Println("保存成功!")
return nil
}
//读取JSON文件,转化为一个student对象,加载的结果存入s中
func (s *Student) Load() error {
srcFile, _ := os.OpenFile("d:/temp/student.json", os.O_RDONLY, 0666)
defer srcFile.Close()
decoder := json.NewDecoder(srcFile)
err := decoder.Decode(s)
if err != nil {
fmt.Println("加载失败,err=", err)
return err
}
//fmt.Println("加载成功!")
return nil
}
定义结构体函数的性能测试用例
//导入测试工具包
import "testing"
//测试用例1:多次测试学生的保存方法,获得平均执行时间
func BenchmarkStudentSave(b *testing.B) {
b.Log("BenchmarkStudentSave")
//汇报内存开销
b.ReportAllocs()
for i := 0; i < b.N; i++ {
student := &Student{"张全蛋", 20}
student.Save()
}
}
//测试用例2:多次测试学生的加载方法,获得平均执行时间
func BenchmarkStudentLoad(b *testing.B) {
b.Log("BenchmarkStudentLoad")
//汇报内存开销
b.ReportAllocs()
for i := 0; i < b.N; i++ {
student := &Student{}
student.Load()
}
}
执行效果
学院 Go 语言视频主页
https://edu.csdn.net/lecturer/1928以上所述就是小编给大家介绍的《[原]压力测试》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 性能测试vs压力测试vs负载测试
- 没有压力的“压力测试”:LSTM神经网络是如何预测焦虑的?
- jmeter 分布式压力测试
- 使用ab压力命令测试网站性能
- API 测试工具 Hitchhiker 0.6 发布,改进压力测试
- MixPHP/Yii/CodeIgniter 并发压力测试
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
零成本实现Web性能测试
温素剑 / 电子工业出版社 / 2012-2 / 59.00元
《零成本实现Web性能测试:基于Apache JMeter》是一本关于Web性能测试的实战书籍,读者朋友们在认真阅读完《零成本实现Web性能测试:基于Apache JMeter》后,相信能够将所学知识应用到生产实践中。《零成本实现Web性能测试:基于Apache JMeter》首先介绍基础的性能测试理论,接着详细介绍如何使用JMeter完成各种类型的性能测试。实战章节中作者以测试某大型保险公司电话......一起来看看 《零成本实现Web性能测试》 这本书的介绍吧!
SHA 加密
SHA 加密工具
UNIX 时间戳转换
UNIX 时间戳转换