Created
March 6, 2017 05:15
-
-
Save vearutop/954958701df46162467565fca943a840 to your computer and use it in GitHub Desktop.
Http dump performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
go test -bench=. | |
BenchmarkPlain-8 20000 75742 ns/op | |
BenchmarkDump-8 10000 133477 ns/op | |
PASS | |
ok tmp 3.648s | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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