您的位置:首页 > 娱乐 > 八卦 > 申请微信公众号_长春建网站一般多少钱_全自动推广引流软件_搜索引擎排名机制

申请微信公众号_长春建网站一般多少钱_全自动推广引流软件_搜索引擎排名机制

2025/3/5 4:26:29 来源:https://blog.csdn.net/m0_52316372/article/details/145607927  浏览:    关键词:申请微信公众号_长春建网站一般多少钱_全自动推广引流软件_搜索引擎排名机制
申请微信公众号_长春建网站一般多少钱_全自动推广引流软件_搜索引擎排名机制

This article is also posted on my blog, feel free to check the latest revision: Go Common Test

The test methods in Go mainly include three types: unit test, benchmark test, and example test.

First of all:

  1. You should import the testing package first.
  2. In the package directory, all source code files with the suffix *_test.go are part of the go test test, and will not be compiled into the final executable file by go build.

Unit Test

The basic format/signature is:

func TestXxx(t *testing.T) {// ...
}

Put the _test.go and .go in the same dir, and run go test -v to execute the test detailed, the go test -cover to check the coverage of the test.

Benchmark test

The benchmark test will not be excuted by default, you need to use go test -bench=Xxx to execute the benchmark test.

The basic format/signature is:

func BenchmarkXxx(b *testing.B) {for i := 0; i < b.N; i++ {// ...}
}

The b.N is the number of times the test is run, and the b.N is automatically adjusted by the go test tool.

    BenchmarkSplit-8        10000000               215 ns/op
  • The -8 means the test is run on the 8 GOMAXPROCS
  • The 10000000 means the test is run 10000000 times.
  • The 215 ns/op means the test takes 215 nanoseconds average per operation.

go test -bench=Xxx -benchmem can check the memory allocation and the number of allocations per operation.

    BenchmarkSplit-8        10000000               215 ns/op             112 B/op          3 allocs/op
  • The 112 B/op means the test allocates 112 bytes per operation.
  • The 3 allocs/op means the test allocates 3 times per operation.

Performance comparison

// note: the n is the parameter of Fib not the b.N, the b.N is the number of times the test is run. it will be automatically adjusted by the go test tool.
func benchmarkFib(b *testing.B, n int) {for i := 0; i < b.N; i++ {Fib(n)}
}func BenchmarkFib1(b *testing.B)  { benchmarkFib(b, 1) }
func BenchmarkFib2(b *testing.B)  { benchmarkFib(b, 2) }
func BenchmarkFib3(b *testing.B)  { benchmarkFib(b, 3) }

go test -bench=. will execute the benchmark test for all functions with the prefix Benchmark.

func BenchmarkSplit(b *testing.B) {time.Sleep(5 * time.Second) // some time-consuming operationsb.ResetTimer()              // reset the timer(ignore the time above)for i := 0; i < b.N; i++ {// some time-consuming operations}
}// you can also use this methodfunc BenchmarkSplit(b *testing.B) {b.StopTimer()// some time-consuming operationsb.StartTimer()for i := 0; i < b.N; i++ {// some time-consuming operations}
}

Parallel test

Signature:

func BenchmarkSplitParallel(b *testing.B) {// b.SetParallelism(1) // set the number of CPU to useb.RunParallel(func(pb *testing.PB) {for pb.Next() {// some time-consuming operations}})
}

Example test

Signature:

func ExampleName() {// No parameters and no return
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com