github.com/moontrade/nogc@v0.1.7/net/http/parser_test.go (about)

     1  package httpparser
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"unsafe"
     7  )
     8  
     9  func TestParseRequest(t *testing.T) {
    10  	d := []byte(data)
    11  	r := AllocRequest(32)
    12  	r.input = uintptr(unsafe.Pointer(&d[0]))
    13  	r.inputLen = uintptr(len(d))
    14  	ParseRequest(r)
    15  	fmt.Println("method: ", r.Method())
    16  	fmt.Println("path: ", r.Path())
    17  	fmt.Println("headers: count", r.NumHeaders())
    18  	for i := 0; i < r.NumHeaders(); i++ {
    19  		header := r.Header(i)
    20  		fmt.Println("\t", header.Name.String(), "->", header.Value.String())
    21  	}
    22  }
    23  
    24  func BenchmarkParseRequest(b *testing.B) {
    25  	run := func(name, data string) {
    26  		b.Run(name, func(b *testing.B) {
    27  			d := []byte(data)
    28  			r := AllocRequest(32)
    29  			defer r.Free()
    30  			r.input = uintptr(unsafe.Pointer(&d[0]))
    31  			r.inputLen = uintptr(len(d))
    32  
    33  			b.ResetTimer()
    34  			b.ReportAllocs()
    35  			for i := 0; i < b.N; i++ {
    36  				ParseRequest(r)
    37  			}
    38  		})
    39  	}
    40  
    41  	run("GET simple", "GET / HTTP/1.0\r\n\r\n")
    42  	run("GET parse 2 headers", "GET /hoge HTTP/1.1\r\nHost: example.com\r\nCookie: \r\n\r\n")
    43  	run("GET common web request", "GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n"+
    44  		"Host: www.kittyhell.com\r\n"+
    45  		"User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 "+
    46  		"Pathtraq/0.9\r\n"+
    47  		"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"+
    48  		"Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n"+
    49  		"Accept-Encoding: gzip,deflate\r\n"+
    50  		"Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n"+
    51  		"Keep-Alive: 115\r\n"+
    52  		"Connection: keep-alive\r\n"+
    53  		"Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; "+
    54  		"__utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; "+
    55  		"__utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n"+
    56  		"\r\n")
    57  }
    58  
    59  const data2 = "GET /hoge HTTP/1.1\r\nHost: example.com\r\nCookie: \r\n\r\n"
    60  
    61  const data = "GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n" +
    62  	"Host: www.kittyhell.com\r\n" +
    63  	"User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 " +
    64  	"Pathtraq/0.9\r\n" +
    65  	"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
    66  	"Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n" +
    67  	"Accept-Encoding: gzip,deflate\r\n" +
    68  	"Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n" +
    69  	"Keep-Alive: 115\r\n" +
    70  	"Connection: keep-alive\r\n" +
    71  	"Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; " +
    72  	"__utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; " +
    73  	"__utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n" +
    74  	"\r\n"