code review 8280046: net/http: new server Handler benchmarks (issue 8280046)

42 views
Skip to first unread message

brad...@golang.org

unread,
Apr 2, 2013, 6:11:20 PM4/2/13
to golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
Reviewers: golang-dev1,

Message:
Hello golan...@googlegroups.com,

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
net/http: new server Handler benchmarks

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

Please review this at https://codereview.appspot.com/8280046/

Affected files:
M src/pkg/net/http/serve_test.go


Index: src/pkg/net/http/serve_test.go
===================================================================
--- a/src/pkg/net/http/serve_test.go
+++ b/src/pkg/net/http/serve_test.go
@@ -1937,3 +1937,64 @@
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)
+ }
+}


pab...@google.com

unread,
Apr 2, 2013, 6:24:48 PM4/2/13
to brad...@golang.org, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com

Peter Buhr

unread,
Apr 2, 2013, 6:29:47 PM4/2/13
to Brad Fitzpatrick, golan...@googlegroups.com
It took longer because I wanted to run some of the tests, and than I did not know how to use
that particular code review tool, so it took some poking to figure out how to reply.


On Tue, Apr 2, 2013 at 3:24 PM, <pab...@google.com> wrote:
LGTM

https://codereview.appspot.com/8280046/

Peter Buhr

unread,
Apr 2, 2013, 6:32:48 PM4/2/13
to Brad Fitzpatrick, golan...@googlegroups.com
Ok, I now understand how to run the benchmarks.

I'll try to do the comparison with and without the new code in server.go

brad...@golang.org

unread,
Apr 2, 2013, 6:42:10 PM4/2/13
to brad...@golang.org, golan...@googlegroups.com, pab...@google.com, re...@codereview-hr.appspotmail.com
*** Submitted as
https://code.google.com/p/go/source/detail?r=52e3407d249f ***

net/http: new server Handler benchmarks

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

R=golang-dev, pabuhr
CC=golang-dev
https://codereview.appspot.com/8280046


https://codereview.appspot.com/8280046/
Reply all
Reply to author
Forward
0 new messages