From 7186200309fb4a9e45c06378bb5fe1befe8df737 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 4 Jun 2015 12:50:52 +0200 Subject: [PATCH] Improves performance tests --- benchmarks_test.go | 54 +++++++++++++++++++++++++++++----------------- githubapi_test.go | 6 +++--- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/benchmarks_test.go b/benchmarks_test.go index 95349b7..c65ad59 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -1,31 +1,42 @@ package gin import ( - "bytes" "html/template" "net/http" - "net/http/httptest" "testing" ) -type FakeWriter struct{} - -func (_ FakeWriter) Write(d []byte) (int, error) { - return 0, nil +func newMockWriter() *mockWriter { + return &mockWriter{ + http.Header{}, + } } -func (_ FakeWriter) WriteString(d string) (int, error) { - return 0, nil +type mockWriter struct { + headers http.Header } +func (m *mockWriter) Header() (h http.Header) { + return m.headers +} + +func (m *mockWriter) Write(p []byte) (n int, err error) { + return len(p), nil +} + +func (m *mockWriter) WriteString(s string) (n int, err error) { + return len(s), nil +} + +func (m *mockWriter) WriteHeader(int) {} + func runRequest(B *testing.B, r *Engine, method, path string) { // create fake request req, err := http.NewRequest(method, path, nil) if err != nil { panic(err) } - w := httptest.NewRecorder() - + w := newMockWriter() B.ReportAllocs() B.ResetTimer() for i := 0; i < B.N; i++ { @@ -40,7 +51,7 @@ func BenchmarkOneRoute(B *testing.B) { } func BenchmarkManyHandlers(B *testing.B) { - DefaultWriter = FakeWriter{} + DefaultWriter = newMockWriter() //router := Default() router := New() router.Use(Recovery(), Logger()) @@ -50,7 +61,7 @@ func BenchmarkManyHandlers(B *testing.B) { } func Benchmark5Params(B *testing.B) { - DefaultWriter = new(bytes.Buffer) + DefaultWriter = newMockWriter() router := New() router.Use(func(c *Context) {}) router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {}) @@ -59,9 +70,9 @@ func Benchmark5Params(B *testing.B) { func BenchmarkOneRouteJSON(B *testing.B) { router := New() - data := H{ - "status": "ok", - } + data := struct { + Status string `json:"status"` + }{"ok"} router.GET("/json", func(c *Context) { c.JSON(200, data) }) @@ -77,9 +88,6 @@ func BenchmarkOneRouteHTML(B *testing.B) { router.SetHTMLTemplate(t) router.GET("/html", func(c *Context) { - //c.Writer.Header()["Content-Type"] = htmlContentType - //t.ExecuteTemplate(c.Writer, "index", "hola") - c.HTML(200, "index", "hola") }) runRequest(B, router, "GET", "/html") @@ -93,10 +101,16 @@ func BenchmarkOneRouteString(B *testing.B) { runRequest(B, router, "GET", "/text") } -func BenchmarkManyRoutes(B *testing.B) { +func BenchmarkManyRoutesFist(B *testing.B) { router := New() router.Any("/ping", func(c *Context) {}) - runRequest(B, router, "PUT", "/ping") + runRequest(B, router, "GET", "/ping") +} + +func BenchmarkManyRoutesLast(B *testing.B) { + router := New() + router.Any("/ping", func(c *Context) {}) + runRequest(B, router, "OPTIONS", "/ping") } func Benchmark404(B *testing.B) { diff --git a/githubapi_test.go b/githubapi_test.go index f30f057..2227fa6 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -298,7 +298,7 @@ func githubConfigRouter(router *Engine) { } func TestGithubAPI(t *testing.T) { - DefaultWriter = FakeWriter{} + DefaultWriter = newMockWriter() router := Default() githubConfigRouter(router) @@ -357,7 +357,7 @@ func BenchmarkGithub(b *testing.B) { } func BenchmarkParallelGithub(b *testing.B) { - DefaultWriter = FakeWriter{} + DefaultWriter = newMockWriter() router := New() githubConfigRouter(router) @@ -373,7 +373,7 @@ func BenchmarkParallelGithub(b *testing.B) { } func BenchmarkParallelGithubDefault(b *testing.B) { - DefaultWriter = FakeWriter{} + DefaultWriter = newMockWriter() router := Default() githubConfigRouter(router)