如何在基准函数中使用 "testing.Benchmark"
How can I use "testing.Benchmark" in a Benchmark function
我的测试文件
func BenchmarkA(b *testing.B) {
res := testing.Benchmark(BenchB)
if res.N <= 25{
b.Fatalf("fail!")
}
func BenchB(b *testing.B) {
for i := 0; i <= b.N; i++{
url := "****"
contentType := "application/x-www-form-urlencoded"
data := strings.NewReader("****")
_, err := http.Post(url, contentType, data)
if err != nil {
b.Fatal("fail ")
}
}
}
当我运行
go test -bench=./router_test.go
我得到一个
fatal error: all goroutines are asleep - deadlock!
如果我使用“package main”和“func main{}”,程序可以运行成功。
接下来我该做什么? (原谅我糟糕的英语)
我觉得有个方法不是特别好
func TestA(t *testing.T) {
res := testing.Benchmark(BenchB)
if res.N <= 25{
t.Fatalf("fail!")
}
基准测试 -> 单元测试
我的测试文件
func BenchmarkA(b *testing.B) {
res := testing.Benchmark(BenchB)
if res.N <= 25{
b.Fatalf("fail!")
}
func BenchB(b *testing.B) {
for i := 0; i <= b.N; i++{
url := "****"
contentType := "application/x-www-form-urlencoded"
data := strings.NewReader("****")
_, err := http.Post(url, contentType, data)
if err != nil {
b.Fatal("fail ")
}
}
}
当我运行
go test -bench=./router_test.go
我得到一个
fatal error: all goroutines are asleep - deadlock!
如果我使用“package main”和“func main{}”,程序可以运行成功。
接下来我该做什么? (原谅我糟糕的英语)
我觉得有个方法不是特别好
func TestA(t *testing.T) {
res := testing.Benchmark(BenchB)
if res.N <= 25{
t.Fatalf("fail!")
}
基准测试 -> 单元测试