Golang Unit Testing, performance testing, performance monitoring technology
The go language provides powerful testing tools, and the following example briefly describes the Go Test unit testing Go test-bench=. Performance test Go tool pprof Performance Monitor go Test unit Test
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
Writing test files
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, hold down the SHIFT key while you right-click and choose to open a command window here) to perform the test
Go test
To see more information
Go test-v
go test-bench=. Perform performance testing
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 CPU performance monitoring graph generated
Performance monitoring of Go using PPROF package for code includes:
Net/http/pprof
Runtime/pprof generating 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 *cpuprofile! = "" {
F, err: = OS. Create (*cpuprofile)
if err! = Nil {
log. Fatal (Err)
}
pprof. Startcpuprofile (f)
defer pprof. Stopcpuprofile ()
}
...
Reference LinksHttp://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html Https://blog.golang.org/profiling-go-programs