]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add BenchmarkReadRequest
authorDave Cheney <dave@cheney.net>
Fri, 8 Feb 2013 20:04:07 +0000 (07:04 +1100)
committerDave Cheney <dave@cheney.net>
Fri, 8 Feb 2013 20:04:07 +0000 (07:04 +1100)
Add benchmark for request parsing. Fixture data is taken from https://github.com/felixge/node-http-perf

% go version
go version devel +28966b7b2f0c Thu Feb 07 20:26:12 2013 -0800 linux/amd64

% go test -run=nil -bench=ReadRequest -benchtime=10s
PASS
BenchmarkReadRequest     2000000   9900 ns/op   61.71 MB/s   3148 B/op   32 allocs/op
ok      net/http        12.180s

R=golang-dev, bradfitz, minux.ma, haimuiba
CC=golang-dev
https://golang.org/cl/7313048

src/pkg/net/http/request_test.go

index bd757920b7808fc96b24923afe18d7730ad819cb..189184795e93c9bcf467df35bfb7684e43e1c086 100644 (file)
@@ -401,3 +401,52 @@ Content-Disposition: form-data; name="textb"
 ` + textbValue + `
 --MyBoundary--
 `
+
+func benchmarkReadRequest(b *testing.B, request string) {
+       b.SetBytes(int64(len(request)))
+       r := bufio.NewReader(&infiniteReader{buf: []byte(request)})
+       b.ReportAllocs()
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               _, err := ReadRequest(r)
+               if err != nil {
+                       b.Fatalf("failed to read request: %v", err)
+               }
+       }
+}
+
+// infiniteReader satisfies Read requests as if the contents of buf
+// loop indefinitely.
+type infiniteReader struct {
+       buf    []byte
+       offset int
+}
+
+func (r *infiniteReader) Read(b []byte) (int, error) {
+       n := copy(b, r.buf[r.offset:])
+       r.offset = (r.offset + n) % len(r.buf)
+       return n, nil
+}
+
+func min(a, b int) int {
+       if a > b {
+               return b
+       }
+       return a
+}
+
+func BenchmarkReadRequest(b *testing.B) {
+       // https://github.com/felixge/node-http-perf/blob/master/fixtures/get.http
+       const request = `GET / HTTP/1.1
+Host: localhost:8080
+Connection: keep-alive
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
+Accept-Encoding: gzip,deflate,sdch
+Accept-Language: en-US,en;q=0.8
+Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
+Cookie: __utma=1.1978842379.1323102373.1323102373.1323102373.1; EPi:NumberOfVisits=1,2012-02-28T13:42:18; CrmSession=5b707226b9563e1bc69084d07a107c98; plushContainerWidth=100%25; plushNoTopMenu=0; hudson_auto_refresh=false
+
+`
+       benchmarkReadRequest(b, strings.Replace(request, "\n", "\r\n", -1))
+}