Commit 6ca1fa62 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: new server Handler benchmarks

For all the Content-Type & Content-Length cases.

R=golang-dev, pabuhr
CC=golang-dev
https://golang.org/cl/8280046
parent 389093fe
...@@ -1937,3 +1937,64 @@ Host: golang.org ...@@ -1937,3 +1937,64 @@ Host: golang.org
b.Errorf("b.N=%d but handled %d", b.N, handled) 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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment