Skip to content

Instantly share code, notes, and snippets.

@vearutop
Created March 6, 2017 05:15
Show Gist options
  • Save vearutop/954958701df46162467565fca943a840 to your computer and use it in GitHub Desktop.
Save vearutop/954958701df46162467565fca943a840 to your computer and use it in GitHub Desktop.
Http dump performance
```
go test -bench=.
BenchmarkPlain-8 20000 75742 ns/op
BenchmarkDump-8 10000 133477 ns/op
PASS
ok tmp 3.648s
```
package main_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
)
var rb []byte = []byte(`SERVER RESPONSE SERVER RESPONSE SERVER RESPONSE SERVER RESPONSE SERVER RESPONSE SERVER RESPONSE SERVER RESPONSE`)
func BenchmarkPlain(b *testing.B) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(rb)
}))
for n := 0; n < b.N; n++ {
req, _ := http.NewRequest("GET", srv.URL, nil)
resp, _ := http.DefaultTransport.RoundTrip(req)
ioutil.ReadAll(resp.Body)
}
srv.Close()
}
func BenchmarkDump(b *testing.B) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(rb)
}))
for n := 0; n < b.N; n++ {
req, _ := http.NewRequest("GET", srv.URL, nil)
resp, _ := http.DefaultTransport.RoundTrip(req)
ioutil.ReadAll(resp.Body)
_, _ = httputil.DumpRequestOut(req, true)
_, _ = httputil.DumpResponse(resp, true)
}
srv.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment