From 709fde85d16a88bffeb7446ad86f0677934a4048 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 30 May 2015 16:36:14 +0200 Subject: [PATCH] More benchmarks --- githubapi_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/githubapi_test.go b/githubapi_test.go index 59ff0d53..f30f0574 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -8,6 +8,8 @@ import ( "bytes" "fmt" "math/rand" + "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" @@ -282,18 +284,23 @@ var githubAPI = []route{ {"DELETE", "/user/keys/:id"}, } -func TestGithubAPI(t *testing.T) { - router := New() - +func githubConfigRouter(router *Engine) { for _, route := range githubAPI { router.Handle(route.method, route.path, func(c *Context) { - output := H{"status": "good"} + output := make(map[string]string, len(c.Params)+1) + output["status"] = "good" for _, param := range c.Params { output[param.Key] = param.Value } c.JSON(200, output) }) } +} + +func TestGithubAPI(t *testing.T) { + DefaultWriter = FakeWriter{} + router := Default() + githubConfigRouter(router) for _, route := range githubAPI { path, values := exampleFromPath(route.path) @@ -342,3 +349,41 @@ func exampleFromPath(path string) (string, Params) { return output.String(), params } + +func BenchmarkGithub(b *testing.B) { + router := New() + githubConfigRouter(router) + runRequest(b, router, "GET", "/legacy/issues/search/:owner/:repository/:state/:keyword") +} + +func BenchmarkParallelGithub(b *testing.B) { + DefaultWriter = FakeWriter{} + router := New() + githubConfigRouter(router) + + req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil) + + b.RunParallel(func(pb *testing.PB) { + // Each goroutine has its own bytes.Buffer. + for pb.Next() { + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + } + }) +} + +func BenchmarkParallelGithubDefault(b *testing.B) { + DefaultWriter = FakeWriter{} + router := Default() + githubConfigRouter(router) + + req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil) + + b.RunParallel(func(pb *testing.PB) { + // Each goroutine has its own bytes.Buffer. + for pb.Next() { + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + } + }) +}