diff options
author | Franck Cuny <franck@lumberjaph.net> | 2013-04-27 09:53:03 -0700 |
---|---|---|
committer | Franck Cuny <franck@lumberjaph.net> | 2013-04-27 09:53:03 -0700 |
commit | 09822817eb84f8d8545e659cd900a16c5f0a9fd9 (patch) | |
tree | 2fac9f8b9863d87faf98df297a1084c210aab45d | |
parent | I can't even spell correctly the name of my project. (diff) | |
download | path-router-09822817eb84f8d8545e659cd900a16c5f0a9fd9.tar.gz |
Add benchmark for the router.
This is for future performances' regressions.
-rw-r--r-- | router_benchmark_test.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/router_benchmark_test.go b/router_benchmark_test.go new file mode 100644 index 0000000..6c328ce --- /dev/null +++ b/router_benchmark_test.go @@ -0,0 +1,61 @@ +package mooh + +import ( + "testing" + "net/url" + "net/http" + "fmt" +) + +func testSimpleRoute(req *Request) (Response, error) { + resp := Response{} + return resp, nil +} + +func BenchmarkSimple(b *testing.B) { + b.StopTimer() + router := BuildDispatcher() + + // Most of the test is a rip from https://github.com/ant0ine/go-json-rest/ + // author Antoine Imbert + // simulate the routes of a real but reasonable app. + // 6 + 10 * (5 + 2) + 1 = 77 routes + routePaths := []string{ + "/", + "/signin", + "/signout", + "/profile", + "/settings", + "/upload/*file", + } + + for i := 0; i < 10; i++ { + for j := 0; j < 5; j++ { + routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id/property%d", i, j)) + } + routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id", i)) + routePaths = append(routePaths, fmt.Sprintf("/resource%d", i)) + } + routePaths = append(routePaths, "/*") + + for _, p := range routePaths { + router.AddRoute(&Route{Path: p, Method: "GET", Code: testSimpleRoute}) + } + + requests := []*http.Request{ + &http.Request{URL: &url.URL{Path: "/"}, Method: "GET"}, + &http.Request{URL: &url.URL{Path: "/resource9/123"}, Method: "GET"}, + &http.Request{URL: &url.URL{Path: "/resource9/123/property1"}, Method: "GET"}, + &http.Request{URL: &url.URL{Path: "/doesnotexists"}, Method: "GET"}, + } + b.StartTimer() + + for i := 0; i < b.N; i++ { + for _, r := range requests{ + m, _ := router.Match(r) + if m == nil && r.URL.Path != "/doesnotexists" { + b.Fatal() + } + } + } +} |