diff --git a/bench_test.go b/bench_test.go index 52e8c630..4383f411 100644 --- a/bench_test.go +++ b/bench_test.go @@ -174,6 +174,12 @@ func BenchmarkGorillaMux_Param(b *testing.B) { r, _ := http.NewRequest("GET", "/user/gordon", nil) benchRequest(b, router, r) } +func BenchmarkGoUtilRouter_Param(b *testing.B) { + router := loadGoUtilRouterSingle("GET", "/user/:name", httpHandlerFunc) + + r, _ := http.NewRequest("GET", "/user/gordon", nil) + benchRequest(b, router, r) +} func BenchmarkHttpRouter_Param(b *testing.B) { router := loadHttpRouterSingle("GET", "/user/:name", httpRouterHandle) @@ -361,6 +367,12 @@ func BenchmarkGorillaMux_Param5(b *testing.B) { r, _ := http.NewRequest("GET", fiveRoute, nil) benchRequest(b, router, r) } +func BenchmarkGoUtilRouter_Param5(b *testing.B) { + router := loadGoUtilRouterSingle("GET", fiveColon, httpHandlerFunc) + + r, _ := http.NewRequest("GET", fiveRoute, nil) + benchRequest(b, router, r) +} func BenchmarkHttpRouter_Param5(b *testing.B) { router := loadHttpRouterSingle("GET", fiveColon, httpRouterHandle) @@ -547,6 +559,12 @@ func BenchmarkGorillaMux_Param20(b *testing.B) { r, _ := http.NewRequest("GET", twentyRoute, nil) benchRequest(b, router, r) } +func BenchmarkGoUtilRouter_Param20(b *testing.B) { + router := loadGoUtilRouterSingle("GET", twentyColon, httpHandlerFunc) + + r, _ := http.NewRequest("GET", twentyRoute, nil) + benchRequest(b, router, r) +} func BenchmarkHttpRouter_Param20(b *testing.B) { router := loadHttpRouterSingle("GET", twentyColon, httpRouterHandle) @@ -729,6 +747,12 @@ func BenchmarkGorillaMux_ParamWrite(b *testing.B) { r, _ := http.NewRequest("GET", "/user/gordon", nil) benchRequest(b, router, r) } +func BenchmarkGoUtilRouter_ParamWrite(b *testing.B) { + router := loadGoUtilRouterSingle("GET", "/user/:name", GoUtilRouterHandlerWrite) + + r, _ := http.NewRequest("GET", "/user/gordon", nil) + benchRequest(b, router, r) +} func BenchmarkHttpRouter_ParamWrite(b *testing.B) { router := loadHttpRouterSingle("GET", "/user/:name", httpRouterHandleWrite) diff --git a/github_test.go b/github_test.go index bb3f379e..0d397122 100644 --- a/github_test.go +++ b/github_test.go @@ -274,34 +274,35 @@ var githubAPI = []route{ } var ( - githubAce http.Handler - githubBear http.Handler - githubBeego http.Handler - githubBone http.Handler - githubDenco http.Handler - githubEcho http.Handler - githubGin http.Handler - githubGocraftWeb http.Handler - githubGoji http.Handler - githubGojiv2 http.Handler - githubGoJsonRest http.Handler - githubGoRestful http.Handler - githubGorillaMux http.Handler - githubHttpRouter http.Handler - githubHttpTreeMux http.Handler - githubKocha http.Handler - githubLARS http.Handler - githubMacaron http.Handler - githubMartini http.Handler - githubPat http.Handler - githubPossum http.Handler - githubR2router http.Handler - githubRevel http.Handler - githubRivet http.Handler - githubTango http.Handler - githubTigerTonic http.Handler - githubTraffic http.Handler - githubVulcan http.Handler + githubAce http.Handler + githubBear http.Handler + githubBeego http.Handler + githubBone http.Handler + githubDenco http.Handler + githubEcho http.Handler + githubGin http.Handler + githubGocraftWeb http.Handler + githubGoji http.Handler + githubGojiv2 http.Handler + githubGoJsonRest http.Handler + githubGoRestful http.Handler + githubGorillaMux http.Handler + githubGoUtilRouter http.Handler + githubHttpRouter http.Handler + githubHttpTreeMux http.Handler + githubKocha http.Handler + githubLARS http.Handler + githubMacaron http.Handler + githubMartini http.Handler + githubPat http.Handler + githubPossum http.Handler + githubR2router http.Handler + githubRevel http.Handler + githubRivet http.Handler + githubTango http.Handler + githubTigerTonic http.Handler + githubTraffic http.Handler + githubVulcan http.Handler // githubZeus http.Handler ) @@ -347,6 +348,9 @@ func init() { calcMem("GorillaMux", func() { githubGorillaMux = loadGorillaMux(githubAPI) }) + calcMem("GoUtilRouter", func() { + githubGoUtilRouter = loadGoUtilRouter(githubAPI) + }) calcMem("HttpRouter", func() { githubHttpRouter = loadHttpRouter(githubAPI) }) @@ -452,6 +456,10 @@ func BenchmarkGorillaMux_GithubStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/user/repos", nil) benchRequest(b, githubGorillaMux, req) } +func BenchmarkGoUtilRouter_GithubStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/user/repos", nil) + benchRequest(b, githubGoUtilRouter, req) +} func BenchmarkHttpRouter_GithubStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/user/repos", nil) benchRequest(b, githubHttpRouter, req) @@ -571,6 +579,10 @@ func BenchmarkGorillaMux_GithubParam(b *testing.B) { req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) benchRequest(b, githubGorillaMux, req) } +func BenchmarkGoUtilRouter_GithubParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) + benchRequest(b, githubGoUtilRouter, req) +} func BenchmarkHttpRouter_GithubParam(b *testing.B) { req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) benchRequest(b, githubHttpRouter, req) @@ -677,6 +689,9 @@ func BenchmarkGoRestful_GithubAll(b *testing.B) { func BenchmarkGorillaMux_GithubAll(b *testing.B) { benchRoutes(b, githubGorillaMux, githubAPI) } +func BenchmarkGoUtilRouter_GithubAll(b *testing.B) { + benchRoutes(b, githubGoUtilRouter, githubAPI) +} func BenchmarkHttpRouter_GithubAll(b *testing.B) { benchRoutes(b, githubHttpRouter, githubAPI) } diff --git a/gplus_test.go b/gplus_test.go index 85344207..87199868 100644 --- a/gplus_test.go +++ b/gplus_test.go @@ -36,34 +36,35 @@ var gplusAPI = []route{ } var ( - gplusAce http.Handler - gplusBear http.Handler - gplusBeego http.Handler - gplusBone http.Handler - gplusDenco http.Handler - gplusEcho http.Handler - gplusGin http.Handler - gplusGocraftWeb http.Handler - gplusGoji http.Handler - gplusGojiv2 http.Handler - gplusGoJsonRest http.Handler - gplusGoRestful http.Handler - gplusGorillaMux http.Handler - gplusHttpRouter http.Handler - gplusHttpTreeMux http.Handler - gplusKocha http.Handler - gplusLARS http.Handler - gplusMacaron http.Handler - gplusMartini http.Handler - gplusPat http.Handler - gplusPossum http.Handler - gplusR2router http.Handler - gplusRevel http.Handler - gplusRivet http.Handler - gplusTango http.Handler - gplusTigerTonic http.Handler - gplusTraffic http.Handler - gplusVulcan http.Handler + gplusAce http.Handler + gplusBear http.Handler + gplusBeego http.Handler + gplusBone http.Handler + gplusDenco http.Handler + gplusEcho http.Handler + gplusGin http.Handler + gplusGocraftWeb http.Handler + gplusGoji http.Handler + gplusGojiv2 http.Handler + gplusGoJsonRest http.Handler + gplusGoRestful http.Handler + gplusGorillaMux http.Handler + gplusGoUtilRouter http.Handler + gplusHttpRouter http.Handler + gplusHttpTreeMux http.Handler + gplusKocha http.Handler + gplusLARS http.Handler + gplusMacaron http.Handler + gplusMartini http.Handler + gplusPat http.Handler + gplusPossum http.Handler + gplusR2router http.Handler + gplusRevel http.Handler + gplusRivet http.Handler + gplusTango http.Handler + gplusTigerTonic http.Handler + gplusTraffic http.Handler + gplusVulcan http.Handler // gplusZeus http.Handler ) @@ -109,6 +110,9 @@ func init() { calcMem("GorillaMux", func() { gplusGorillaMux = loadGorillaMux(gplusAPI) }) + calcMem("GoUtilRouter", func() { + gplusGoUtilRouter = loadGoUtilRouter(gplusAPI) + }) calcMem("HttpRouter", func() { gplusHttpRouter = loadHttpRouter(gplusAPI) }) @@ -214,6 +218,10 @@ func BenchmarkGorillaMux_GPlusStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/people", nil) benchRequest(b, gplusGorillaMux, req) } +func BenchmarkGoUtilRouter_GPlusStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/people", nil) + benchRequest(b, gplusGoUtilRouter, req) +} func BenchmarkHttpRouter_GPlusStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/people", nil) benchRequest(b, gplusHttpRouter, req) @@ -333,6 +341,10 @@ func BenchmarkGorillaMux_GPlusParam(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) benchRequest(b, gplusGorillaMux, req) } +func BenchmarkGoUtilRouter_GPlusParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) + benchRequest(b, gplusGoUtilRouter, req) +} func BenchmarkHttpRouter_GPlusParam(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) benchRequest(b, gplusHttpRouter, req) @@ -452,6 +464,10 @@ func BenchmarkGorillaMux_GPlus2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) benchRequest(b, gplusGorillaMux, req) } +func BenchmarkGoUtilRouter_GPlus2Params(b *testing.B) { + req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) + benchRequest(b, gplusGoUtilRouter, req) +} func BenchmarkHttpRouter_GPlus2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) benchRequest(b, gplusHttpRouter, req) @@ -558,6 +574,9 @@ func BenchmarkGoRestful_GPlusAll(b *testing.B) { func BenchmarkGorillaMux_GPlusAll(b *testing.B) { benchRoutes(b, gplusGorillaMux, gplusAPI) } +func BenchmarkGoUtilRouter_GPlusAll(b *testing.B) { + benchRoutes(b, gplusGoUtilRouter, gplusAPI) +} func BenchmarkHttpRouter_GPlusAll(b *testing.B) { benchRoutes(b, gplusHttpRouter, gplusAPI) } diff --git a/parse_test.go b/parse_test.go index 80058da5..9d237254 100644 --- a/parse_test.go +++ b/parse_test.go @@ -56,34 +56,35 @@ var parseAPI = []route{ } var ( - parseAce http.Handler - parseBear http.Handler - parseBeego http.Handler - parseBone http.Handler - parseDenco http.Handler - parseEcho http.Handler - parseGin http.Handler - parseGocraftWeb http.Handler - parseGoji http.Handler - parseGojiv2 http.Handler - parseGoJsonRest http.Handler - parseGoRestful http.Handler - parseGorillaMux http.Handler - parseHttpRouter http.Handler - parseHttpTreeMux http.Handler - parseKocha http.Handler - parseLARS http.Handler - parseMacaron http.Handler - parseMartini http.Handler - parsePat http.Handler - parsePossum http.Handler - parseR2router http.Handler - parseRevel http.Handler - parseRivet http.Handler - parseTango http.Handler - parseTigerTonic http.Handler - parseTraffic http.Handler - parseVulcan http.Handler + parseAce http.Handler + parseBear http.Handler + parseBeego http.Handler + parseBone http.Handler + parseDenco http.Handler + parseEcho http.Handler + parseGin http.Handler + parseGocraftWeb http.Handler + parseGoji http.Handler + parseGojiv2 http.Handler + parseGoJsonRest http.Handler + parseGoRestful http.Handler + parseGorillaMux http.Handler + parseGoUtilRouter http.Handler + parseHttpRouter http.Handler + parseHttpTreeMux http.Handler + parseKocha http.Handler + parseLARS http.Handler + parseMacaron http.Handler + parseMartini http.Handler + parsePat http.Handler + parsePossum http.Handler + parseR2router http.Handler + parseRevel http.Handler + parseRivet http.Handler + parseTango http.Handler + parseTigerTonic http.Handler + parseTraffic http.Handler + parseVulcan http.Handler // parseZeus http.Handler ) @@ -129,6 +130,9 @@ func init() { calcMem("GorillaMux", func() { parseGorillaMux = loadGorillaMux(parseAPI) }) + calcMem("GoUtilRouter", func() { + parseGoUtilRouter = loadGoUtilRouter(parseAPI) + }) calcMem("HttpRouter", func() { parseHttpRouter = loadHttpRouter(parseAPI) }) @@ -234,6 +238,10 @@ func BenchmarkGorillaMux_ParseStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/1/users", nil) benchRequest(b, parseGorillaMux, req) } +func BenchmarkGoUtilRouter_ParseStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/users", nil) + benchRequest(b, parseGoUtilRouter, req) +} func BenchmarkHttpRouter_ParseStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/1/users", nil) benchRequest(b, parseHttpRouter, req) @@ -353,6 +361,10 @@ func BenchmarkGorillaMux_ParseParam(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go", nil) benchRequest(b, parseGorillaMux, req) } +func BenchmarkGoUtilRouter_ParseParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/classes/go", nil) + benchRequest(b, parseGoUtilRouter, req) +} func BenchmarkHttpRouter_ParseParam(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go", nil) benchRequest(b, parseHttpRouter, req) @@ -472,6 +484,10 @@ func BenchmarkGorillaMux_Parse2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) benchRequest(b, parseGorillaMux, req) } +func BenchmarkGoUtilRouter_Parse2Params(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) + benchRequest(b, parseGoUtilRouter, req) +} func BenchmarkHttpRouter_Parse2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) benchRequest(b, parseHttpRouter, req) @@ -578,6 +594,9 @@ func BenchmarkGoRestful_ParseAll(b *testing.B) { func BenchmarkGorillaMux_ParseAll(b *testing.B) { benchRoutes(b, parseGorillaMux, parseAPI) } +func BenchmarkGoUtilRouter_ParseAll(b *testing.B) { + benchRoutes(b, parseGoUtilRouter, parseAPI) +} func BenchmarkHttpRouter_ParseAll(b *testing.B) { benchRoutes(b, parseHttpRouter, parseAPI) } diff --git a/routers.go b/routers.go index f9c57cf2..9f3c22da 100644 --- a/routers.go +++ b/routers.go @@ -28,6 +28,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-macaron/macaron" "github.com/go-martini/martini" + goutilrouter "github.com/go-util/router" "github.com/go-zoo/bone" "github.com/gocraft/web" "github.com/gorilla/mux" @@ -345,7 +346,7 @@ func echoHandlerTest(c echo.Context) error { func loadEcho(routes []route) http.Handler { var h echo.HandlerFunc = echoHandler - if loadTestHandler { + if loadTestHandler { h = echoHandlerTest } @@ -724,6 +725,31 @@ func loadGorillaMuxSingle(method, path string, handler http.HandlerFunc) http.Ha return m } +// GoUtilRouter +func GoUtilRouterHandlerWrite(w http.ResponseWriter, r *http.Request) { + params := r.URL.Query() + io.WriteString(w, params.Get("name")) +} + +func loadGoUtilRouter(routes []route) http.Handler { + h := httpHandlerFunc + if loadTestHandler { + h = httpHandlerFuncTest + } + + router := goutilrouter.NewRouter() + for _, route := range routes { + router.Handle(route.method, route.path, h) + } + return router +} + +func loadGoUtilRouterSingle(method, path string, handler http.HandlerFunc) http.Handler { + router := goutilrouter.NewRouter() + router.Handle(method, path, handler) + return router +} + // HttpRouter func httpRouterHandle(_ http.ResponseWriter, _ *http.Request, _ httprouter.Params) {} diff --git a/routers_test.go b/routers_test.go index 81e7bdd8..439b6383 100644 --- a/routers_test.go +++ b/routers_test.go @@ -25,6 +25,7 @@ var ( {"GoJsonRest", loadGoJsonRest}, {"GoRestful", loadGoRestful}, {"GorillaMux", loadGorillaMux}, + {"GoUtilRouter", loadGoUtilRouter}, {"HttpRouter", loadHttpRouter}, {"HttpTreeMux", loadHttpTreeMux}, //{"Kocha", loadKocha}, diff --git a/static_test.go b/static_test.go index d3417658..7ff2b350 100644 --- a/static_test.go +++ b/static_test.go @@ -172,34 +172,35 @@ var staticRoutes = []route{ var ( staticHttpServeMux http.Handler - staticAce http.Handler - staticBear http.Handler - staticBeego http.Handler - staticBone http.Handler - staticDenco http.Handler - staticEcho http.Handler - staticGin http.Handler - staticGocraftWeb http.Handler - staticGoji http.Handler - staticGojiv2 http.Handler - staticGoJsonRest http.Handler - staticGoRestful http.Handler - staticGorillaMux http.Handler - staticHttpRouter http.Handler - staticHttpTreeMux http.Handler - staticKocha http.Handler - staticLARS http.Handler - staticMacaron http.Handler - staticMartini http.Handler - staticPat http.Handler - staticPossum http.Handler - staticR2router http.Handler - staticRevel http.Handler - staticRivet http.Handler - staticTango http.Handler - staticTigerTonic http.Handler - staticTraffic http.Handler - staticVulcan http.Handler + staticAce http.Handler + staticBear http.Handler + staticBeego http.Handler + staticBone http.Handler + staticDenco http.Handler + staticEcho http.Handler + staticGin http.Handler + staticGocraftWeb http.Handler + staticGoji http.Handler + staticGojiv2 http.Handler + staticGoJsonRest http.Handler + staticGoRestful http.Handler + staticGorillaMux http.Handler + staticGoUtilRouter http.Handler + staticHttpRouter http.Handler + staticHttpTreeMux http.Handler + staticKocha http.Handler + staticLARS http.Handler + staticMacaron http.Handler + staticMartini http.Handler + staticPat http.Handler + staticPossum http.Handler + staticR2router http.Handler + staticRevel http.Handler + staticRivet http.Handler + staticTango http.Handler + staticTigerTonic http.Handler + staticTraffic http.Handler + staticVulcan http.Handler // staticZeus http.Handler ) @@ -253,6 +254,9 @@ func init() { calcMem("GorillaMux", func() { staticGorillaMux = loadGorillaMux(staticRoutes) }) + calcMem("GoUtilRouter", func() { + staticGoUtilRouter = loadGoUtilRouter(staticRoutes) + }) calcMem("HttpRouter", func() { staticHttpRouter = loadHttpRouter(staticRoutes) }) @@ -349,6 +353,9 @@ func BenchmarkGoRestful_StaticAll(b *testing.B) { func BenchmarkGorillaMux_StaticAll(b *testing.B) { benchRoutes(b, staticGorillaMux, staticRoutes) } +func BenchmarkGoUtilRouter_StaticAll(b *testing.B) { + benchRoutes(b, staticGoUtilRouter, staticRoutes) +} func BenchmarkHttpRouter_StaticAll(b *testing.B) { benchRoutes(b, staticHttpRouter, staticRoutes) }