Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze benchmark in Go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to analyze the benchmark test in Go language. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

In the go language, it provides a test method for testing function performance (CPU and Memory), benchmark testing.

Benchmarking is mainly used to test the efficiency of CPU and memory to evaluate the performance of the code under test. Testers can optimize our code based on feedback from these performance metrics to improve performance problems.

This paper mainly introduces the use of benchmark and the analysis of performance indicators.

1. The usage rules are as follows:

1. The code file for the benchmark must end with _ test.go.

two。 Benchmark functions must start with Benchmark.

3. The benchmark function must accept a pointer to the testing.B type as the only argument.

4. Before the test is executed, you need to call b.ResetTimer (reset timer) to reset the time in order to filter out the time spent by the code before the test.

5. The test code needs to be written in a for loop, and the maximum value in the loop is B.N.

two。 Use case description:

The file structure is: benchmark.go, benchmark_test.go, test case writing, see the test code on the right.

3. Run the benchmark:

$go test-bench=. -benchtime=5s-benchmem-run=none

Parameter description:

-bench=. It means to run all the benchmark tests. It means all.

-benchtime=5s: indicates that the run time is 5s, and the default time is 1s.

-benchmem: indicates the metric that displays memory.

-run=none: cases that filters out unit tests and does not run UT.

Content analysis of the output:

Goos: darwin: indicates that the operating system is darwin.

Goarch: amd64: indicates that the architecture of the target platform is amd64.

BenchmarkHandleWithType-4:BenchmarkHandleWithType represents the name of the function to run; 4 represents the value of the corresponding GOMAXPROCS at run time.

10000000000: represents the value of b.N within 5s.

0.28ns / op: indicates that the function is executed once, and the time consumed is 0.28ns.

0B / op: indicates that 0B of memory is allocated for each operation.

0 allocs/op: this function is executed once, and memory is allocated once.

The above is the editor for you to share how to analyze the benchmark in the Go language, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report