Go language HTTP test and program performance tuning

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The main thing to be told is the Http,websocket test and some ways to tune the Go program.
Divided into the following sections:
I. Httptest TEST PACKAGE
Two. Performance testing
Three. How to use parametric analysis and tuning procedures

Four. Real-time monitoring of tuning in operation


I. Httptest TEST PACKAGE
For HTTP and WebSocket testing, the Go Standard library has an HTTP test framework. Under the "Http/httptest" package.
Go1.5.1\go\src\net\http\httptest


How to use in the source directory to see examples, you can also take a look at this example:
Https://golang.org/src/net/http/request_test.go
There are all kinds of usages in it.

If you want to try it yourself. Https://golang.org/doc/articles/wiki/has a very complete example of the go Web.
Can be set up and run the test again.

For example post, the approximate test procedure is this:

Func testpost (t *testing. T) {req, err: = Newrequest ("POST", "Http://localhost:8080/edit/nn", strings. Newreader ("Body=xcl")) if err! = Nil {T.errorf ("%v", err)}defer req. Body.close () req. Header.set ("Content-type", "application/x-www-form-urlencoded; Param=value ") if Q: = req. Formvalue ("body"); Q! = "Xcl" {T.errorf (' req. Formvalue ("body") =%q, Want "xcl" ', Q)}}
two. Performance Testing

In addition to the traditional AB, there is a good tool for go writing called boom.
Example:
Boom-n 1000-c https://google.com
Command Description:

Usage:boom [Options ...] <url>options:-N number of requests to run. -c Number of requests to run concurrently.  Total number of requests cannot is smaller than the concurency level.  -Q rate limit, in seconds (QPS). -O Output type.      If none provided, a summary is printed. "CSV" is the only supported alternative.  Dumps the response metrics in comma-seperated values format.  -M HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.  -H Custom HTTP headers, name1:value1;name2:value2.  -T Timeout in Ms.  -A HTTP Accept header.  -D HTTP request body.  -T Content-type, defaults to "text/html".  -A Basic authentication, Username:password.  -X HTTP Proxy address as Host:port.  -readall consumes the entire request body.  -allow-insecure allow bad/expired tls/ssl certificates.  -disable-compression disable compression. -disable-keepalive disable keep-alive, prevents re-use of TCP connections between different HTTP requests.                        -cpus number of used CPU cores. (Default for current machine is 1 cores)
But this only works on HTTP interfaces, but like my kind of using custom protocols based on WebSocket.
Or you need to customize it yourself.
My current practice, the client test directly refer to Boom's source code architecture, changed to support the WebSocket protocol.
and add a custom protocol to do business logic simulation, it's good to use. But there's a place to be aware that if the concurrency
The connection is too large, the WebSocket TCP connection establishment may time out, and the wait time can be extended during customization.
The code is as follows:

  .... client, err: = Net. Dialtimeout ("TCP", ServerIP, Waitedial*time. Second) If err! = Nil {log. Printf ("[Testconnect] User (%s) net. Dialtimeout error:%s ", UserName, Err) Return}defer client. Close () config, _: = WebSocket. Newconfig (SERVERADDR, origin) WS, ERR: = WebSocket. Newclient (config, client) if err! = Nil {log. Printf ("[Testconnect] User (%s) failed to connect to server! Err:%s ", UserName, Err) return}   //....
three. How to use parametric analysis and tuning procedures
/* Tuning Program Example go build main.gomain.exe-cpuprofile=cpu.pprofgo tool pprof Main.exe cpu.pprofauthor:xcldate:2015-11-22*/ Package Mainimport ("Flag" "FMT" "OS" "Runtime/pprof") var cpuprofile = flag. String ("Cpuprofile", "", "Write CPU profile to file") Func main () {flag. Parse () if *cpuprofile! = "" {f, err: = OS. Create (*cpuprofile) if err! = Nil {fmt. Println ("Error:", Err)} pprof. Startcpuprofile (f) Defer pprof. Stopcpuprofile ()} t1 ()}func T1 () {for i: = 0; i < 10000; i++ {fmt. Sprintf ("%d", i)}}/*////////////////////////////////////////////////////e:\gotest\testing\testpprof3>go build Main.goe:\gotest\testing\testpprof3>main.exe-cpuprofile=cpu.pprofe:\gotest\testing\testpprof3>dir Drive E    The volume in the doc volume is the serial number of the 0e3d-2a1f E:\GOtest\testing\testpprof3 2015/11/22 18:39 <DIR>. 2015/11/22 18:39 <DIR>..        2015/11/22 18:39 168 cpu.pprof2015/11/22 18:38 2,826,752 main.exe2015/11/22 18:38     1,604 main.go 3 files 2,828,524 bytes 2 directories 15,171,936,256 Available Bytes E:\GOtest\testing\testppro  F3>go tool pprof Main.exe cpu.pprofentering interactive mode (type "help" for Commands) (pprof) top1010ms of 10ms Total        (100%) Showing top nodes out of one (cum >= 10ms) flat flat% sum% cum cum% 10ms 100% 100% 10ms 100% runtime.memmove 0 0% 100% 10ms 100% fmt. (*fmt). Integer 0 0% 100% 10ms 100% fmt. (*fmt). Pad 0 0% 100% 10ms 100% fmt. (*pp). doprintf 0 0% 100% 10ms 100% fmt. (*pp). FmtInt64 0 0% 100% 10ms 100% fmt. (*pp). Printarg 0 0% 100% 10ms 100% fmt.     Sprintf 0 0% 100% 10ms 100% main.main 0 0% 100% 10ms 100% main.t1 0 0% 100% 10ms 100% runtime.goexit (pprof) */
four. Real-time monitoring of tuning in Operation
In fact, this can be referenced under the source of Net/http/pprof/pprof.go
/* Tuning Program Example go build main.go view http://127.0.0.1:7081/debug/pprof/http://on the browser side  127.0.0.1:7081/debug/pprof/profileauthor:xcldate:2015-11-22*/package mainimport ("FMT" "Net/http" "Net/http/pprof" "OS" "Time") Func main () {go func () {if err: = Startadminhttp ("127.0.0.1:7081"); Err! = nil {os. Exit ( -1)}} () T1 ()}func startadminhttp (webaddr string) error {adminservemux: = http. Newservemux () adminservemux.handlefunc ("/debug/pprof/", Pprof. Index) Adminservemux.handlefunc ("/debug/pprof/cmdline", Pprof. CmdLine) Adminservemux.handlefunc ("/debug/pprof/profile", Pprof. Profile) Adminservemux.handlefunc ("/debug/pprof/symbol", Pprof. Symbol) Err: = http. Listenandserve (WEBADDR, Adminservemux) if err! = Nil {x: = FMT. Sprintf ("http. Listenadserve (\ "%s\") failed (%s) ", Webaddr, err. Error ()) fmt. PRINTLN (x) return err} return Nil}func T1 () {for i: = 0; i < 10000; i++ {fmt. Sprintf ("%d", I) time. Sleep (1 * time. Second)}} 
Another approach:
In the program run, in the command line window, execute "Go tool pprof URL ... "will also become the corresponding document.
In profile, for example, it waits for 30s and then generates the relevant document in \pprof\. You can then use the Pprof-related commands to tune.
C:\users\xcl>go tool pprof http://127.0.0.1:7081/debug/pprof/profileFetching profile from http://127.0.0.1:7081/ Debug/pprof/profileplease wait ... (30s) Saved profile in \pprof\pprof.127.0.0.1:7081.samples.cpu.001.pb.gzentering interactive mode (type ' help ' for commands) ( PPROF) (pprof) (pprof) top1010ms of 10ms Total (  100%)      flat  flat% sum% cum   cum%      10ms   100%   100%       10ms   100%  runtime.runqput         0     0%   100%       10ms   100%  Runtime.goready.func1         0     0%   100%       10ms   100%  runtime.ready         0     0%   100%       10ms   100%  runtime.starttheworldwithsema         0     0%   100%       10ms   100%  Runtime.systemstack (PPROF)

Reference Documentation:
https://golang.org/cmd/go/#hdr-description_of_testing_flags
Http://saml.rilspace.org/profiling-and-creating-call-graphs-for-go-programs-with-go-tool-pprof
Http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html



blog:http://blog.csdn.net/xcl168






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.