More benchmarks

This commit is contained in:
Manu Mtz-Almeida 2015-05-30 16:36:14 +02:00
parent b7a15d3554
commit 709fde85d1
1 changed files with 49 additions and 4 deletions

View File

@ -8,6 +8,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"math/rand" "math/rand"
"net/http"
"net/http/httptest"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -282,18 +284,23 @@ var githubAPI = []route{
{"DELETE", "/user/keys/:id"}, {"DELETE", "/user/keys/:id"},
} }
func TestGithubAPI(t *testing.T) { func githubConfigRouter(router *Engine) {
router := New()
for _, route := range githubAPI { for _, route := range githubAPI {
router.Handle(route.method, route.path, func(c *Context) { 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 { for _, param := range c.Params {
output[param.Key] = param.Value output[param.Key] = param.Value
} }
c.JSON(200, output) c.JSON(200, output)
}) })
} }
}
func TestGithubAPI(t *testing.T) {
DefaultWriter = FakeWriter{}
router := Default()
githubConfigRouter(router)
for _, route := range githubAPI { for _, route := range githubAPI {
path, values := exampleFromPath(route.path) path, values := exampleFromPath(route.path)
@ -342,3 +349,41 @@ func exampleFromPath(path string) (string, Params) {
return output.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)
}
})
}