]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: new server Handler benchmarks
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 2 Apr 2013 22:42:06 +0000 (15:42 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 2 Apr 2013 22:42:06 +0000 (15:42 -0700)
For all the Content-Type & Content-Length cases.

R=golang-dev, pabuhr
CC=golang-dev
https://golang.org/cl/8280046

src/pkg/net/http/serve_test.go

index a040f2738b2ddbef4da46cad1e4e90e811324991..102f489427acce599f8b33012fec008f3523f56c 100644 (file)
@@ -1937,3 +1937,64 @@ Host: golang.org
                b.Errorf("b.N=%d but handled %d", b.N, handled)
        }
 }
+
+const someResponse = "<html>some response</html>"
+
+// A Reponse that's just no bigger than 2KB, the buffer-before-chunking threshold.
+var response = bytes.Repeat([]byte(someResponse), 2<<10/len(someResponse))
+
+// Both Content-Type and Content-Length set. Should be no buffering.
+func BenchmarkServerHandlerTypeLen(b *testing.B) {
+       benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.Header().Set("Content-Type", "text/html")
+               w.Header().Set("Content-Length", strconv.Itoa(len(response)))
+               w.Write(response)
+       }))
+}
+
+// A Content-Type is set, but no length. No sniffing, but will count the Content-Length.
+func BenchmarkServerHandlerNoLen(b *testing.B) {
+       benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.Header().Set("Content-Type", "text/html")
+               w.Write(response)
+       }))
+}
+
+// A Content-Length is set, but the Content-Type will be sniffed.
+func BenchmarkServerHandlerNoType(b *testing.B) {
+       benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.Header().Set("Content-Length", strconv.Itoa(len(response)))
+               w.Write(response)
+       }))
+}
+
+// Neither a Content-Type or Content-Length, so sniffed and counted.
+func BenchmarkServerHandlerNoHeader(b *testing.B) {
+       benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.Write(response)
+       }))
+}
+
+func benchmarkHandler(b *testing.B, h Handler) {
+       b.ReportAllocs()
+       req := []byte(strings.Replace(`GET / HTTP/1.1
+Host: golang.org
+
+`, "\n", "\r\n", -1))
+       conn := &rwTestConn{
+               Reader: &repeatReader{content: req, count: b.N},
+               Writer: ioutil.Discard,
+               closec: make(chan bool, 1),
+       }
+       handled := 0
+       handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
+               handled++
+               h.ServeHTTP(rw, r)
+       })
+       ln := &oneConnListener{conn: conn}
+       go Serve(ln, handler)
+       <-conn.closec
+       if b.N != handled {
+               b.Errorf("b.N=%d but handled %d", b.N, handled)
+       }
+}