This is a creation in Article, where the information may have evolved or changed.
Golang unit testing, performance testing, performance monitoring technology
The go language provides powerful testing tools, with an example of a brief introduction
- Go Test Unit Testing
- Go test-bench=. Performance testing
- Go tool pprof Performance monitoring
Go Test Unit Testing
For example, the package helper under the Util.go unit test, util.go mainly contains two functions, one is to SHA1 the string encryption, one is to verify the mobile phone number
If we want to test to verify that the mobile phone number function is correct, we can create a new Util_test.go file under the package, the general name is _test.go ( for the test file main filename), test the function with the test function (t *testing. T) Form
Util_test.go
Then the CMD window executes (note that the directory is switched to the current directory, and if it is a Windows system, press and hold the SHIFT key while you right-click to open a command window here)
Go test
To see more information
Go test-v
Go test-bench=. Performing a performance test
Writing Performance test files
Performance test file names are generally the same as test files, unlike the benchmark function (b *testing) when writing performance test functions. B) Form
Perform performance tests
Executes in the command-line window of the current directory
Go test-bench=.
224599 nanoseconds per time, 10,000 executions, total 2.38 seconds
Generate CPU Performance Monitor diagram
Performance monitoring of Go using PPROF package for code includes:
Net/http/pprof
Runtime/pprof
- Generate a Web server performance monitoring diagram
If your go program is a Web server that starts with an HTTP package, you want to see the status of your Web server. This is the time to choose Net/http/pprof. You just need to introduce the package _ "Net/http/pprof"
Go run main.go can use http://localhost:8080/debug/pprof/directly in the browser to see the status of the current Web services, including CPU usage and memory usage, such as
If you want to show graphically, then first install Graphviz (graphical tools for Go call generation timing diagram, official website download address Graphviz, domestic download address Baidu-graphviz), want to generate CPU status analysis diagram, and then open a command window execution
Go tool pprof http://localhost:8080/debug/pprof/profile
Will enter the 30-second profile collection time, in this event is constantly refreshed click on the Http://localhost:8080/wechat browser page, as far as possible to let CPU consumption performance data generation.
Then enter the Web command at the cursor blink, if prompted "Dot not found," the reason may be that you do not install Graphviz, if you have installed a prompt error, you need to add Graphviz Bin directory to the path path
The. SVG graphic will then be generated and will automatically start the default browser open
- Generate generic Application performance monitoring diagram
If your go program is just an application, such as calculating factorial, then you can't use the NET/HTTP/PPROF package, you need to use the runtime/pprof. The specific approach is to use the pprof. Startcpuprofile and Pprof.stopcpuprofile, for specific use please refer to the official blog Https://blog.golang.org/profiling-go-programs
var cpuprofile = flag.String("cpuprofile""""write cpu profile to file")func main() { flag.Parse() if"" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } ...
Reference links
- Http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html
- Https://blog.golang.org/profiling-go-programs