package router import ( "fmt" "github.com/franckcuny/web-request" "net/http" "net/url" "testing" ) func testSimpleRoute(req *request.Request, resp *request.Response) error { return nil } func BenchmarkSimple(b *testing.B) { b.StopTimer() router := BuildRouter() // 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() } } } }