Skip to content
Myr

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

2 files+36 added−22 removed

modified:benchmarks_test.go+33 added-19 removed
1package gin23import (4 "bytes"4 "html/template"5 "net/http"7 "net/http/httptest"6 "testing"7)811type FakeWriter struct{}9func newMockWriter() *mockWriter {10 return &mockWriter{11 http.Header{},12 }13}1413func (_ FakeWriter) Write(d []byte) (int, error) {14 return 0, nil15type mockWriter struct {16 headers http.Header17}1817func (_ FakeWriter) WriteString(d string) (int, error) {18 return 0, nil19func (m *mockWriter) Header() (h http.Header) {20 return m.headers21}2223func (m *mockWriter) Write(p []byte) (n int, err error) {24 return len(p), nil25}2627func (m *mockWriter) WriteString(s string) (n int, err error) {28 return len(s), nil29}3031func (m *mockWriter) WriteHeader(int) {}3233func 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()2839 w := newMockWriter()40 B.ReportAllocs()41 B.ResetTimer()42 for i := 0; i < B.N; i++ {43 r.ServeHTTP(w, req)
6 unmodified lines
50 runRequest(B, router, "GET", "/ping")51}5253func 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 line
60 runRequest(B, router, "GET", "/ping")61}6263func 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}7071func 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 lines
87 <html><body><h1>{{.}}</h1></body></html>`))88 router.SetHTMLTemplate(t)8990 router.GET("/html", func(c *Context) {80 //c.Writer.Header()["Content-Type"] = htmlContentType81 //t.ExecuteTemplate(c.Writer, "index", "hola")8291 c.HTML(200, "index", "hola")92 })93 runRequest(B, router, "GET", "/html")94}
5 unmodified lines
100 })101 runRequest(B, router, "GET", "/text")102}10396func BenchmarkManyRoutes(B *testing.B) {104func 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}109110func BenchmarkManyRoutesLast(B *testing.B) {111 router := New()112 router.Any("/ping", func(c *Context) {})113 runRequest(B, router, "OPTIONS", "/ping")114}115116func Benchmark404(B *testing.B) {117 router := New()118 router.Any("/something", func(c *Context) {})119 router.NoRoute(func(c *Context) {})
17 unmodified lines
modified:githubapi_test.go+3 added-3 removed
296 unmodified lines
297 }298}299300func TestGithubAPI(t *testing.T) {301 DefaultWriter = FakeWriter{}301 DefaultWriter = newMockWriter()302 router := Default()303 githubConfigRouter(router)304305 for _, route := range githubAPI {
50 unmodified lines
356 runRequest(b, router, "GET", "/legacy/issues/search/:owner/:repository/:state/:keyword")357}358359func BenchmarkParallelGithub(b *testing.B) {360 DefaultWriter = FakeWriter{}360 DefaultWriter = newMockWriter()361 router := New()362 githubConfigRouter(router)363364 req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)
7 unmodified lines
372 })373}374375func BenchmarkParallelGithubDefault(b *testing.B) {376 DefaultWriter = FakeWriter{}376 DefaultWriter = newMockWriter()377 router := Default()378 githubConfigRouter(router)379380 req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)
9 unmodified lines