diff --git a/README.md b/README.md index 57bb7eb6..97b860f9 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Of course the tested routers can be used for any kind of HTTP request → handle * [Denco](https://github.com/naoina/denco) * [Gocraft Web](https://github.com/gocraft/web) * [Goji](https://github.com/zenazn/goji/) + * [Golossus](https://github.com/golossus/routing) * [Gorilla Mux](http://www.gorillatoolkit.org/pkg/mux) * [http.ServeMux](http://golang.org/pkg/net/http/#ServeMux) * [HttpRouter](https://github.com/julienschmidt/httprouter) diff --git a/bench_test.go b/bench_test.go index a875ee85..27f4c17b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -188,6 +188,12 @@ func BenchmarkGoJsonRest_Param(b *testing.B) { r, _ := http.NewRequest("GET", "/user/gordon", nil) benchRequest(b, router, r) } +func BenchmarkGolossus_Param(b *testing.B) { + router := loadGolossusSingle("GET", "/user/{name}", httpHandlerFunc) + + r, _ := http.NewRequest("GET", "/user/gordon", nil) + benchRequest(b, router, r) +} func BenchmarkGoRestful_Param(b *testing.B) { router := loadGoRestfulSingle("GET", "/user/{name}", goRestfulHandler) @@ -398,6 +404,12 @@ func BenchmarkGoJsonRest_Param5(b *testing.B) { r, _ := http.NewRequest("GET", fiveRoute, nil) benchRequest(b, handler, r) } +func BenchmarkGolossus_Param5(b *testing.B) { + handler := loadGolossusSingle("GET", fiveBrace, httpHandlerFunc) + + r, _ := http.NewRequest("GET", fiveRoute, nil) + benchRequest(b, handler, r) +} func BenchmarkGoRestful_Param5(b *testing.B) { router := loadGoRestfulSingle("GET", fiveBrace, goRestfulHandler) @@ -608,6 +620,12 @@ func BenchmarkGoJsonRest_Param20(b *testing.B) { r, _ := http.NewRequest("GET", twentyRoute, nil) benchRequest(b, handler, r) } +func BenchmarkGolossus_Param20(b *testing.B) { + handler := loadGolossusSingle("GET", twentyBrace, httpHandlerFunc) + + r, _ := http.NewRequest("GET", twentyRoute, nil) + benchRequest(b, handler, r) +} func BenchmarkGoRestful_Param20(b *testing.B) { handler := loadGoRestfulSingle("GET", twentyBrace, goRestfulHandler) @@ -814,6 +832,12 @@ func BenchmarkGoJsonRest_ParamWrite(b *testing.B) { r, _ := http.NewRequest("GET", "/user/gordon", nil) benchRequest(b, handler, r) } +func BenchmarkGolossus_ParamWrite(b *testing.B) { + handler := loadGolossusSingle("GET", "/user/{name}", golossusHandlerWrite) + + r, _ := http.NewRequest("GET", "/user/gordon", nil) + benchRequest(b, handler, r) +} func BenchmarkGoRestful_ParamWrite(b *testing.B) { handler := loadGoRestfulSingle("GET", "/user/{name}", goRestfulHandlerWrite) diff --git a/github_test.go b/github_test.go index 6f483cd7..25518d63 100644 --- a/github_test.go +++ b/github_test.go @@ -288,6 +288,7 @@ var ( githubGoji http.Handler githubGojiv2 http.Handler githubGoJsonRest http.Handler + githubGolossus http.Handler githubGoRestful http.Handler githubGorillaMux http.Handler githubGowwwRouter http.Handler @@ -354,6 +355,9 @@ func init() { calcMem("GoJsonRest", func() { githubGoJsonRest = loadGoJsonRest(githubAPI) }) + calcMem("Golossus", func() { + githubGolossus = loadGolossus(githubAPI) + }) calcMem("GoRestful", func() { githubGoRestful = loadGoRestful(githubAPI) }) @@ -472,6 +476,10 @@ func BenchmarkGoRestful_GithubStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/user/repos", nil) benchRequest(b, githubGoRestful, req) } +func BenchmarkGolossus_GithubStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/user/repos", nil) + benchRequest(b, githubGolossus, req) +} func BenchmarkGoJsonRest_GithubStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/user/repos", nil) benchRequest(b, githubGoJsonRest, req) @@ -608,6 +616,10 @@ func BenchmarkGoJsonRest_GithubParam(b *testing.B) { req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) benchRequest(b, githubGoJsonRest, req) } +func BenchmarkGolossus_GithubParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) + benchRequest(b, githubGolossus, req) +} func BenchmarkGoRestful_GithubParam(b *testing.B) { req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil) benchRequest(b, githubGoRestful, req) @@ -730,6 +742,9 @@ func BenchmarkGojiv2_GithubAll(b *testing.B) { func BenchmarkGoJsonRest_GithubAll(b *testing.B) { benchRoutes(b, githubGoJsonRest, githubAPI) } +func BenchmarkGolossus_GithubAll(b *testing.B) { + benchRoutes(b, githubGolossus, githubAPI) +} func BenchmarkGoRestful_GithubAll(b *testing.B) { benchRoutes(b, githubGoRestful, githubAPI) } diff --git a/go.mod b/go.mod index b911842f..99e20f85 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/go-playground/lars v4.0.1+incompatible github.com/go-zoo/bone v1.3.0 github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b + github.com/golossus/routing v1.0.0 github.com/gorilla/mux v1.7.3 github.com/gorilla/sessions v1.2.0 // indirect github.com/gorilla/websocket v1.4.1 // indirect diff --git a/go.sum b/go.sum index 258bc827..c5653140 100644 --- a/go.sum +++ b/go.sum @@ -92,6 +92,8 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golossus/routing v1.0.0 h1:T8sPJmCtivMcKkp7F50OiRZrcqUqAJ6m9Xaw0Vf2ewk= +github.com/golossus/routing v1.0.0/go.mod h1:WmyBNlhhC8Gqb1sGOmRKxgeBCE0/Uw6WCdBrR0z+ZAM= github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= diff --git a/gplus_test.go b/gplus_test.go index b11699ab..dac9ea49 100644 --- a/gplus_test.go +++ b/gplus_test.go @@ -50,6 +50,7 @@ var ( gplusGoji http.Handler gplusGojiv2 http.Handler gplusGoJsonRest http.Handler + gplusGolossus http.Handler gplusGoRestful http.Handler gplusGorillaMux http.Handler gplusGowwwRouter http.Handler @@ -116,6 +117,9 @@ func init() { calcMem("GoJsonRest", func() { gplusGoJsonRest = loadGoJsonRest(gplusAPI) }) + calcMem("Golossus", func() { + gplusGolossus = loadGolossus(gplusAPI) + }) calcMem("GoRestful", func() { gplusGoRestful = loadGoRestful(gplusAPI) }) @@ -234,6 +238,10 @@ func BenchmarkGoJsonRest_GPlusStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/people", nil) benchRequest(b, gplusGoJsonRest, req) } +func BenchmarkGolossus_GPlusStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/people", nil) + benchRequest(b, gplusGolossus, req) +} func BenchmarkGoRestful_GPlusStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/people", nil) benchRequest(b, gplusGoRestful, req) @@ -370,6 +378,10 @@ func BenchmarkGoJsonRest_GPlusParam(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) benchRequest(b, gplusGoJsonRest, req) } +func BenchmarkGolossus_GPlusParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) + benchRequest(b, gplusGolossus, req) +} func BenchmarkGoRestful_GPlusParam(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil) benchRequest(b, gplusGoRestful, req) @@ -506,6 +518,10 @@ func BenchmarkGoJsonRest_GPlus2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) benchRequest(b, gplusGoJsonRest, req) } +func BenchmarkGolossus_GPlus2Params(b *testing.B) { + req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) + benchRequest(b, gplusGolossus, req) +} func BenchmarkGoRestful_GPlus2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil) benchRequest(b, gplusGoRestful, req) @@ -628,6 +644,9 @@ func BenchmarkGojiv2_GPlusAll(b *testing.B) { func BenchmarkGoJsonRest_GPlusAll(b *testing.B) { benchRoutes(b, gplusGoJsonRest, gplusAPI) } +func BenchmarkGolossus_GPlusAll(b *testing.B) { + benchRoutes(b, gplusGolossus, gplusAPI) +} func BenchmarkGoRestful_GPlusAll(b *testing.B) { benchRoutes(b, gplusGoRestful, gplusAPI) } diff --git a/parse_test.go b/parse_test.go index 6d5e5dc1..7f9168ed 100644 --- a/parse_test.go +++ b/parse_test.go @@ -70,6 +70,7 @@ var ( parseGoji http.Handler parseGojiv2 http.Handler parseGoJsonRest http.Handler + parseGolossus http.Handler parseGoRestful http.Handler parseGorillaMux http.Handler parseGowwwRouter http.Handler @@ -133,6 +134,9 @@ func init() { calcMem("GoJsonRest", func() { parseGoJsonRest = loadGoJsonRest(parseAPI) }) + calcMem("Golossus", func() { + parseGolossus = loadGolossus(parseAPI) + }) calcMem("GoRestful", func() { parseGoRestful = loadGoRestful(parseAPI) }) @@ -251,6 +255,10 @@ func BenchmarkGoJsonRest_ParseStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/1/users", nil) benchRequest(b, parseGoJsonRest, req) } +func BenchmarkGolossus_ParseStatic(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/users", nil) + benchRequest(b, parseGolossus, req) +} func BenchmarkGoRestful_ParseStatic(b *testing.B) { req, _ := http.NewRequest("GET", "/1/users", nil) benchRequest(b, parseGoRestful, req) @@ -387,6 +395,10 @@ func BenchmarkGoJsonRest_ParseParam(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go", nil) benchRequest(b, parseGoJsonRest, req) } +func BenchmarkGolossus_ParseParam(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/classes/go", nil) + benchRequest(b, parseGolossus, req) +} func BenchmarkGoRestful_ParseParam(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go", nil) benchRequest(b, parseGoRestful, req) @@ -523,6 +535,10 @@ func BenchmarkGoJsonRest_Parse2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) benchRequest(b, parseGoJsonRest, req) } +func BenchmarkGolossus_Parse2Params(b *testing.B) { + req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) + benchRequest(b, parseGolossus, req) +} func BenchmarkGoRestful_Parse2Params(b *testing.B) { req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil) benchRequest(b, parseGoRestful, req) @@ -645,6 +661,9 @@ func BenchmarkGojiv2_ParseAll(b *testing.B) { func BenchmarkGoJsonRest_ParseAll(b *testing.B) { benchRoutes(b, parseGoJsonRest, parseAPI) } +func BenchmarkGolossus_ParseAll(b *testing.B) { + benchRoutes(b, parseGolossus, parseAPI) +} func BenchmarkGoRestful_ParseAll(b *testing.B) { benchRoutes(b, parseGoRestful, parseAPI) } diff --git a/routers.go b/routers.go index c94f8fed..2775ff84 100644 --- a/routers.go +++ b/routers.go @@ -32,6 +32,7 @@ import ( "github.com/go-martini/martini" "github.com/go-zoo/bone" "github.com/gocraft/web" + golossus "github.com/golossus/routing" "github.com/gorilla/mux" gowwwrouter "github.com/gowww/router" "github.com/julienschmidt/httprouter" @@ -778,6 +779,40 @@ func loadGoJsonRestSingle(method, path string, hfunc rest.HandlerFunc) http.Hand return api.MakeHandler() } +//Golossus +func golossusHandlerWrite(w http.ResponseWriter, r *http.Request) { + params := golossus.GetURLParameters(r) + name, _ := params.GetByName("name") + io.WriteString(w, name) +} + +func loadGolossus(routes []route) http.Handler { + h := httpHandlerFunc + if loadTestHandler { + h = httpHandlerFuncTest + } + + re := regexp.MustCompile(":([^/]*)") + router := golossus.NewRouter() + for _, route := range routes { + err := router.Register( + route.method, + re.ReplaceAllString(route.path, "{$1}"), + h, + ) + if err != nil { + fmt.Println(err) + } + } + return &router +} + +func loadGolossusSingle(method, path string, handle http.HandlerFunc) http.Handler { + router := golossus.NewRouter() + router.Register(method, path, handle) + return &router +} + // go-restful func goRestfulHandler(r *restful.Request, w *restful.Response) {} diff --git a/routers_test.go b/routers_test.go index ba49233e..d6beb898 100644 --- a/routers_test.go +++ b/routers_test.go @@ -26,6 +26,7 @@ var ( {"Goji", loadGoji}, {"Gojiv2", loadGojiv2}, {"GoJsonRest", loadGoJsonRest}, + {"Golossus", loadGolossus}, {"GoRestful", loadGoRestful}, {"GorillaMux", loadGorillaMux}, {"GowwwRouter", loadGowwwRouter}, diff --git a/static_test.go b/static_test.go index c11823f9..f6b1d5a6 100644 --- a/static_test.go +++ b/static_test.go @@ -186,6 +186,7 @@ var ( staticGoji http.Handler staticGojiv2 http.Handler staticGoJsonRest http.Handler + staticGolossus http.Handler staticGoRestful http.Handler staticGorillaMux http.Handler staticGowwwRouter http.Handler @@ -260,6 +261,9 @@ func init() { calcMem("GoJsonRest", func() { staticGoJsonRest = loadGoJsonRest(staticRoutes) }) + calcMem("Golossus", func() { + staticGolossus = loadGolossus(staticRoutes) + }) calcMem("GoRestful", func() { staticGoRestful = loadGoRestful(staticRoutes) }) @@ -368,6 +372,9 @@ func BenchmarkGojiv2_StaticAll(b *testing.B) { func BenchmarkGoJsonRest_StaticAll(b *testing.B) { benchRoutes(b, staticGoJsonRest, staticRoutes) } +func BenchmarkGolossus_StaticAll(b *testing.B) { + benchRoutes(b, staticGolossus, staticRoutes) +} func BenchmarkGoRestful_StaticAll(b *testing.B) { benchRoutes(b, staticGoRestful, staticRoutes) }