[原]压力测试

栏目: 编程工具 · 发布时间: 7年前

内容简介:版权声明:本文为博主尹成联系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

[清华团队带你实战区块链开发]

( https://ke.qq.com/course/344443?tuin=3d17195d )

扫码获取海量视频及源码 QQ群:

721929980

[原]压力测试

以上所述就是小编给大家介绍的《[原]压力测试》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

R Cookbook

R Cookbook

Paul Teetor / O'Reilly Media / 2011-3-22 / USD 39.99

With more than 200 practical recipes, this book helps you perform data analysis with R quickly and efficiently. The R language provides everything you need to do statistical work, but its structure ca......一起来看看 《R Cookbook》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具