preface
Benchmarking measures the performance of a program under a fixed workload, and the Go language also provides benchmarks that can support benchmarking.
Method of use
Here’s an example of benchmark code to see how it can be used:
func Benchmark_test(b *testing.B) {
for i := 0; i < b.N ; i++ {
s := make([]int.0)
for i := 0; i < 10000; i++ {
s = append(s, i)
}
}
}
Copy the code
- The file for benchmarking must be in
*_test.go
Is the same suffix as the name of the test file, for exampleabc_test.go
- Methods that participate in Benchmark performance tests must be in the
Benchmark
Is a prefix, for exampleBenchmarkABC()
- Participating benchmark functions must accept a pointer
Benchmark
Type as the only argument,*testing.B
- The benchmark function cannot have a return value
b.ResetTimer
The reset timer is called to restart the timer, ignoring some of the preparation in the test functionb.N
Is provided by the benchmark framework and represents the number of loops, since the test code needs to be called repeatedly to assess performance
Commands and Parameters
Run the go test [parameter] command, for example, go test-bench =.-benchmem.
parameter | meaning |
---|---|
-bench regexp | Performance test, support expression to test function filter. |
-bench | . Tests all benchmark functions. If you specify a name, only the specific test method is executed, not all |
-benchmem | Displays memory allocation statistics for test functions during performance tests |
-count n | Run tests and perform how much of this, by default once |
-run regexp | Run only specific test functions, for example -run ABC tests only test functions that contain ABC in the function name |
-timeout t | If the test time exceeds t, panic, 10 minutes by default |
-v | Displays test details, and also displays logs for the Log and Logf methods |
The test results
After the command is executed, the performance test result is displayed as follows
$ go test-bench=. -benchmem goos: darwin goarch: amd64 pkg: program/benchmark cpu: Intel(R) Core(TM) i7-9750H CPU @2.60ghz benchmark_test-12 7439091 152.0 NS /op 248 B/op 5 ALlocs/OP PASS OK Promgram/benchmark 1.304 sCopy the code
The above results were analyzed one by one:
Results the item | meaning |
---|---|
Benchmark_test-12 | Benchmark_testIs the name of the test function– 12The value of GOMAXPROCS (number of threads) is 12 |
7439091 | That means it’s been executed7439091Time, that is,b.NThe value of the |
152.0 ns/op | That’s the average cost per operation152.0 nanoseconds |
248B/op | Indicates that each operation is applied248ByteMemory request |
5 allocs/op | Indicates that each operation is applied5Time memory |
Performance Comparison Example
An example of a numeric conversion string is used to compare and analyze the performance tests.
//Sprintf
func BenchmarkSprintf(b *testing.B) {
num := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%d", num)
}
}
//Format
func BenchmarkFormat(b *testing.B) {
num := int64(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.FormatInt(num, 10)}}//Itoa
func BenchmarkItoa(b *testing.B) {
num := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.Itoa(num)
}
}
Copy the code
Go test-bench =.-benchmem
% go test-bench=. -benchmem goos: darwin goarch: amd64 pkg: program/benchmark cpu: Intel(R) Core(TM) I7-9750H CPU @2.60ghz benchmarkSprintF-12 16364854 63.70ns /op 2 B/ OP 1 ALLOCs/OP benchmarkformat-12 493325650 2.380 NS /op 0 B/op 0 ALlocs /op BenchmarkItoa-12 481683436 2.503 NS /op 0 B/op 0 ALlocs /op PASS OK The program/benchmark 4.007 sCopy the code
It can be found that BenchmarkSprintf method takes the longest time, BenchmarkFormat is the fastest, and BenchmarkItoa is also fast. The difference is that fmt.sprintf () performs a memory allocation of 1 allocs/op.
Combined with pPROF analysis
parameter | meaning | Order sample |
---|---|---|
-cpuprofile [file] | Output CPU performance files | go test -bench=. -benchmem -cpuprofile=cpu.out |
-memprofile [file] | Output the MEM memory performance file | go test -bench=. -benchmem -memprofile=cpu.out |
You can run the Go Tool pprof [file] command to view the generated CPU and memory files. Then, you can run the list [file] command in pprof to view the CPU and memory time
## memory status(pprof) list BenchmarkArrayAppend Total: 36.49 GB ROUTINE = = = = = = = = = = = = = = = = = = = = = = = = the program/benchmark BenchmarkArrayAppendin/ Users/guanjian/workspace/go/program/benchmark/benchmark_test. Go 11.98 GB 11.98 GB (flat, Cum) 32.83% of Total.. 7://Array.. 8:func BenchmarkArrayAppend(b *testing.b) {.. 9:for i := 0; i < b.N; i++ {
. . 10: var arr []int
. . 11: fori := 0; i < 10000; I ++ {11.98GB 11.98GB 12: arr = appEnd (arr, I).. 13:}.. 14:}.. 15:}Copy the code
# # the CPU(pprof) list BenchmarkArrayAppend Total: 8.86 s ROUTINE = = = = = = = = = = = = = = = = = = = = = = = = the program/benchmark BenchmarkArrayAppendin/ Users/guanjian/workspace/go/program/benchmark/benchmark_test. Go 10 ms 640 ms (flat, cum). 7.22% of the Total. 6: . . 7://Array . . 8:func BenchmarkArrayAppend(b *testing.B) { . . 9:for i := 0; i < b.N; i++ {
. . 10: var arr []int
10ms 10ms 11: for i := 0; i < 10000; i++ {
. 630ms 12: arr = append(arr, i)
. . 13: }
. . 14: }
. . 15:}
Copy the code
conclusion
Go provides benchmark performance testing tools, provides reports and analyses of functions using memory and CPU, and can also obtain better analysis reports with the help of Pprof. If you want to conduct in-depth analysis, you can also use GDB introduced previously to perform link tracking of underlying code. And decompile the code to see how much performance is wasted.
reference
Go Benchmark Performance test Unit test Details about how to use the GO Benchmark performance test