Improves performance tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 971c44c · Browse files · 2 files·+36 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+36 added−22 removed
- other:+36 added−22 removed
Provenance
- Commit
7186200- Tree
5673e9b- Author
Manu Mtz-Almeida <manu.valladolid@gmail.com>- Committer
Manu Mtz-Almeida <manu.valladolid@gmail.com>- Parent
971c44c
2 files changed+36 added−22 removed
modified:benchmarks_test.go+33 added-19 removed
1 package gin2 3 import (4- "bytes"4 "html/template"5 "net/http"7- "net/http/httptest"6 "testing"7 )8 11-type FakeWriter struct{}9+func newMockWriter() *mockWriter {10+ return &mockWriter{11+ http.Header{},12+ }13+}14 13-func (_ FakeWriter) Write(d []byte) (int, error) {14- return 0, nil15+type mockWriter struct {16+ headers http.Header17 }18 17-func (_ FakeWriter) WriteString(d string) (int, error) {18- return 0, nil19+func (m *mockWriter) Header() (h http.Header) {20+ return m.headers21 }22 23+func (m *mockWriter) Write(p []byte) (n int, err error) {24+ return len(p), nil25+}26+27+func (m *mockWriter) WriteString(s string) (n int, err error) {28+ return len(s), nil29+}30+31+func (m *mockWriter) WriteHeader(int) {}32+33 func runRequest(B *testing.B, r *Engine, method, path string) {34 // create fake request35 req, err := http.NewRequest(method, path, nil)36 if err != nil {37 panic(err)38 }27- w := httptest.NewRecorder()28-39+ w := newMockWriter()40 B.ReportAllocs()41 B.ResetTimer()42 for i := 0; i < B.N; i++ {43 r.ServeHTTP(w, req)6 unmodified lines50 runRequest(B, router, "GET", "/ping")51 }52 53 func BenchmarkManyHandlers(B *testing.B) {43- DefaultWriter = FakeWriter{}54+ DefaultWriter = newMockWriter()55 //router := Default()56 router := New()57 router.Use(Recovery(), Logger())58 router.Use(func(c *Context) {})1 unmodified line60 runRequest(B, router, "GET", "/ping")61 }62 63 func Benchmark5Params(B *testing.B) {53- DefaultWriter = new(bytes.Buffer)64+ DefaultWriter = newMockWriter()65 router := New()66 router.Use(func(c *Context) {})67 router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})68 runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")69 }70 71 func BenchmarkOneRouteJSON(B *testing.B) {72 router := New()62- data := H{63- "status": "ok",64- }73+ data := struct {74+ Status string `json:"status"`75+ }{"ok"}76 router.GET("/json", func(c *Context) {77 c.JSON(200, data)78 })79 runRequest(B, router, "GET", "/json")7 unmodified lines87 <html><body><h1>{{.}}</h1></body></html>`))88 router.SetHTMLTemplate(t)89 90 router.GET("/html", func(c *Context) {80- //c.Writer.Header()["Content-Type"] = htmlContentType81- //t.ExecuteTemplate(c.Writer, "index", "hola")82-91 c.HTML(200, "index", "hola")92 })93 runRequest(B, router, "GET", "/html")94 }5 unmodified lines100 })101 runRequest(B, router, "GET", "/text")102 }103 96-func BenchmarkManyRoutes(B *testing.B) {104+func BenchmarkManyRoutesFist(B *testing.B) {105 router := New()106 router.Any("/ping", func(c *Context) {})99- runRequest(B, router, "PUT", "/ping")107+ runRequest(B, router, "GET", "/ping")108 }109 110+func BenchmarkManyRoutesLast(B *testing.B) {111+ router := New()112+ router.Any("/ping", func(c *Context) {})113+ runRequest(B, router, "OPTIONS", "/ping")114+}115+116 func Benchmark404(B *testing.B) {117 router := New()118 router.Any("/something", func(c *Context) {})119 router.NoRoute(func(c *Context) {})17 unmodified linesmodified:githubapi_test.go+3 added-3 removed
296 unmodified lines297 }298 }299 300 func TestGithubAPI(t *testing.T) {301- DefaultWriter = FakeWriter{}301+ DefaultWriter = newMockWriter()302 router := Default()303 githubConfigRouter(router)304 305 for _, route := range githubAPI {50 unmodified lines356 runRequest(b, router, "GET", "/legacy/issues/search/:owner/:repository/:state/:keyword")357 }358 359 func BenchmarkParallelGithub(b *testing.B) {360- DefaultWriter = FakeWriter{}360+ DefaultWriter = newMockWriter()361 router := New()362 githubConfigRouter(router)363 364 req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)7 unmodified lines372 })373 }374 375 func BenchmarkParallelGithubDefault(b *testing.B) {376- DefaultWriter = FakeWriter{}376+ DefaultWriter = newMockWriter()377 router := Default()378 githubConfigRouter(router)379 380 req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)9 unmodified lines