链客,专为开发者而生,有问必答!
此文章来自区块链技术社区,未经允许拒绝转载。
性能测试基本概念
基本概念
Benchmark: 性能测试
ns/op: 纳秒/每个操作,前面数值越小越快
命令
go test -c
go test -test.bench=. -test.cpuprofile=cpu.prof
go tool pprof bench.test cpu.prof
示例
建立一个包,创建一个test文件如,文件名为lib_test.go
package lib
import (
“testing”
“fmt”
)
const (
str = “hello gohpers!”
sep = “,”
)
func BenchmarkFmt(b *testing.B) {
for i := 0; i < b.N ; i++ {
_ = fmt.Sprintf("%s%s%s%s%s", str, sep, str, sep, str)
}
}
func BenchmarkPlus(b *testing.B) {
for i:=0; i < b.N; i++ {
_ = str + sep + str + sep + str