Skip to content

Commit 72b1346

Browse files
author
Arthur White
committed
Add gowww/router
1 parent 734f0bc commit 72b1346

File tree

7 files changed

+120
-10
lines changed

7 files changed

+120
-10
lines changed

bench_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ func BenchmarkGorillaMux_Param(b *testing.B) {
174174
r, _ := http.NewRequest("GET", "/user/gordon", nil)
175175
benchRequest(b, router, r)
176176
}
177+
func BenchmarkGowwwRouter_Param(b *testing.B) {
178+
router := loadGowwwRouterSingle("GET", "/user/:name", http.HandlerFunc(httpHandlerFunc))
179+
180+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
181+
benchRequest(b, router, r)
182+
}
177183
func BenchmarkHttpRouter_Param(b *testing.B) {
178184
router := loadHttpRouterSingle("GET", "/user/:name", httpRouterHandle)
179185

@@ -361,6 +367,12 @@ func BenchmarkGorillaMux_Param5(b *testing.B) {
361367
r, _ := http.NewRequest("GET", fiveRoute, nil)
362368
benchRequest(b, router, r)
363369
}
370+
func BenchmarkGowwwRouter_Param5(b *testing.B) {
371+
router := loadGowwwRouterSingle("GET", fiveColon, http.HandlerFunc(httpHandlerFunc))
372+
373+
r, _ := http.NewRequest("GET", fiveRoute, nil)
374+
benchRequest(b, router, r)
375+
}
364376
func BenchmarkHttpRouter_Param5(b *testing.B) {
365377
router := loadHttpRouterSingle("GET", fiveColon, httpRouterHandle)
366378

@@ -547,6 +559,12 @@ func BenchmarkGorillaMux_Param20(b *testing.B) {
547559
r, _ := http.NewRequest("GET", twentyRoute, nil)
548560
benchRequest(b, router, r)
549561
}
562+
func BenchmarkGowwwRouter_Param20(b *testing.B) {
563+
router := loadGowwwRouterSingle("GET", twentyColon, http.HandlerFunc(httpHandlerFunc))
564+
565+
r, _ := http.NewRequest("GET", twentyRoute, nil)
566+
benchRequest(b, router, r)
567+
}
550568
func BenchmarkHttpRouter_Param20(b *testing.B) {
551569
router := loadHttpRouterSingle("GET", twentyColon, httpRouterHandle)
552570

@@ -735,6 +753,12 @@ func BenchmarkHttpRouter_ParamWrite(b *testing.B) {
735753
r, _ := http.NewRequest("GET", "/user/gordon", nil)
736754
benchRequest(b, router, r)
737755
}
756+
func BenchmarkGowwwRouter_ParamWrite(b *testing.B) {
757+
router := loadGowwwRouterSingle("GET", "/user/:name", http.HandlerFunc(gowwwRouterHandleWrite))
758+
759+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
760+
benchRequest(b, router, r)
761+
}
738762
func BenchmarkHttpTreeMux_ParamWrite(b *testing.B) {
739763
router := loadHttpTreeMuxSingle("GET", "/user/:name", httpTreeMuxHandlerWrite)
740764

github_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ var (
287287
githubGoJsonRest http.Handler
288288
githubGoRestful http.Handler
289289
githubGorillaMux http.Handler
290+
githubGowwwRouter http.Handler
290291
githubHttpRouter http.Handler
291292
githubHttpTreeMux http.Handler
292293
githubKocha http.Handler
@@ -347,6 +348,9 @@ func init() {
347348
calcMem("GorillaMux", func() {
348349
githubGorillaMux = loadGorillaMux(githubAPI)
349350
})
351+
calcMem("GowwwRouter", func() {
352+
githubGowwwRouter = loadGowwwRouter(githubAPI)
353+
})
350354
calcMem("HttpRouter", func() {
351355
githubHttpRouter = loadHttpRouter(githubAPI)
352356
})
@@ -456,6 +460,10 @@ func BenchmarkHttpRouter_GithubStatic(b *testing.B) {
456460
req, _ := http.NewRequest("GET", "/user/repos", nil)
457461
benchRequest(b, githubHttpRouter, req)
458462
}
463+
func BenchmarkGowwwRouter_GithubStatic(b *testing.B) {
464+
req, _ := http.NewRequest("GET", "/user/repos", nil)
465+
benchRequest(b, githubGowwwRouter, req)
466+
}
459467
func BenchmarkHttpTreeMux_GithubStatic(b *testing.B) {
460468
req, _ := http.NewRequest("GET", "/user/repos", nil)
461469
benchRequest(b, githubHttpTreeMux, req)
@@ -571,6 +579,10 @@ func BenchmarkGorillaMux_GithubParam(b *testing.B) {
571579
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
572580
benchRequest(b, githubGorillaMux, req)
573581
}
582+
func BenchmarkGowwwRouter_GithubParam(b *testing.B) {
583+
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
584+
benchRequest(b, githubGowwwRouter, req)
585+
}
574586
func BenchmarkHttpRouter_GithubParam(b *testing.B) {
575587
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
576588
benchRequest(b, githubHttpRouter, req)
@@ -677,6 +689,9 @@ func BenchmarkGoRestful_GithubAll(b *testing.B) {
677689
func BenchmarkGorillaMux_GithubAll(b *testing.B) {
678690
benchRoutes(b, githubGorillaMux, githubAPI)
679691
}
692+
func BenchmarkGowwwRouter_GithubAll(b *testing.B) {
693+
benchRoutes(b, githubGowwwRouter, githubAPI)
694+
}
680695
func BenchmarkHttpRouter_GithubAll(b *testing.B) {
681696
benchRoutes(b, githubHttpRouter, githubAPI)
682697
}

gplus_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
gplusGoJsonRest http.Handler
5050
gplusGoRestful http.Handler
5151
gplusGorillaMux http.Handler
52+
gplusGowwwRouter http.Handler
5253
gplusHttpRouter http.Handler
5354
gplusHttpTreeMux http.Handler
5455
gplusKocha http.Handler
@@ -109,6 +110,9 @@ func init() {
109110
calcMem("GorillaMux", func() {
110111
gplusGorillaMux = loadGorillaMux(gplusAPI)
111112
})
113+
calcMem("GowwwRouter", func() {
114+
gplusGowwwRouter = loadGowwwRouter(gplusAPI)
115+
})
112116
calcMem("HttpRouter", func() {
113117
gplusHttpRouter = loadHttpRouter(gplusAPI)
114118
})
@@ -214,6 +218,10 @@ func BenchmarkGorillaMux_GPlusStatic(b *testing.B) {
214218
req, _ := http.NewRequest("GET", "/people", nil)
215219
benchRequest(b, gplusGorillaMux, req)
216220
}
221+
func BenchmarkGowwwRouter_GPlusStatic(b *testing.B) {
222+
req, _ := http.NewRequest("GET", "/people", nil)
223+
benchRequest(b, gplusGowwwRouter, req)
224+
}
217225
func BenchmarkHttpRouter_GPlusStatic(b *testing.B) {
218226
req, _ := http.NewRequest("GET", "/people", nil)
219227
benchRequest(b, gplusHttpRouter, req)
@@ -333,6 +341,10 @@ func BenchmarkGorillaMux_GPlusParam(b *testing.B) {
333341
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
334342
benchRequest(b, gplusGorillaMux, req)
335343
}
344+
func BenchmarkGowwwRouter_GPlusParam(b *testing.B) {
345+
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
346+
benchRequest(b, gplusGowwwRouter, req)
347+
}
336348
func BenchmarkHttpRouter_GPlusParam(b *testing.B) {
337349
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
338350
benchRequest(b, gplusHttpRouter, req)
@@ -452,6 +464,10 @@ func BenchmarkGorillaMux_GPlus2Params(b *testing.B) {
452464
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
453465
benchRequest(b, gplusGorillaMux, req)
454466
}
467+
func BenchmarkGowwwRouter_GPlus2Params(b *testing.B) {
468+
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
469+
benchRequest(b, gplusGowwwRouter, req)
470+
}
455471
func BenchmarkHttpRouter_GPlus2Params(b *testing.B) {
456472
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
457473
benchRequest(b, gplusHttpRouter, req)
@@ -558,6 +574,9 @@ func BenchmarkGoRestful_GPlusAll(b *testing.B) {
558574
func BenchmarkGorillaMux_GPlusAll(b *testing.B) {
559575
benchRoutes(b, gplusGorillaMux, gplusAPI)
560576
}
577+
func BenchmarkGowwwRouter_GPlusAll(b *testing.B) {
578+
benchRoutes(b, gplusGowwwRouter, gplusAPI)
579+
}
561580
func BenchmarkHttpRouter_GPlusAll(b *testing.B) {
562581
benchRoutes(b, gplusHttpRouter, gplusAPI)
563582
}

parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
parseGoJsonRest http.Handler
7070
parseGoRestful http.Handler
7171
parseGorillaMux http.Handler
72+
parseGowwwRouter http.Handler
7273
parseHttpRouter http.Handler
7374
parseHttpTreeMux http.Handler
7475
parseKocha http.Handler
@@ -129,6 +130,9 @@ func init() {
129130
calcMem("GorillaMux", func() {
130131
parseGorillaMux = loadGorillaMux(parseAPI)
131132
})
133+
calcMem("GowwwRouter", func() {
134+
parseGowwwRouter = loadGowwwRouter(parseAPI)
135+
})
132136
calcMem("HttpRouter", func() {
133137
parseHttpRouter = loadHttpRouter(parseAPI)
134138
})
@@ -234,6 +238,10 @@ func BenchmarkGorillaMux_ParseStatic(b *testing.B) {
234238
req, _ := http.NewRequest("GET", "/1/users", nil)
235239
benchRequest(b, parseGorillaMux, req)
236240
}
241+
func BenchmarkGowwwRouter_ParseStatic(b *testing.B) {
242+
req, _ := http.NewRequest("GET", "/1/users", nil)
243+
benchRequest(b, parseGowwwRouter, req)
244+
}
237245
func BenchmarkHttpRouter_ParseStatic(b *testing.B) {
238246
req, _ := http.NewRequest("GET", "/1/users", nil)
239247
benchRequest(b, parseHttpRouter, req)
@@ -353,6 +361,10 @@ func BenchmarkGorillaMux_ParseParam(b *testing.B) {
353361
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
354362
benchRequest(b, parseGorillaMux, req)
355363
}
364+
func BenchmarkGowwwRouter_ParseParam(b *testing.B) {
365+
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
366+
benchRequest(b, parseGowwwRouter, req)
367+
}
356368
func BenchmarkHttpRouter_ParseParam(b *testing.B) {
357369
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
358370
benchRequest(b, parseHttpRouter, req)
@@ -472,6 +484,10 @@ func BenchmarkGorillaMux_Parse2Params(b *testing.B) {
472484
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
473485
benchRequest(b, parseGorillaMux, req)
474486
}
487+
func BenchmarkGowwwRouter_Parse2Params(b *testing.B) {
488+
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
489+
benchRequest(b, parseGowwwRouter, req)
490+
}
475491
func BenchmarkHttpRouter_Parse2Params(b *testing.B) {
476492
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
477493
benchRequest(b, parseHttpRouter, req)
@@ -578,6 +594,9 @@ func BenchmarkGoRestful_ParseAll(b *testing.B) {
578594
func BenchmarkGorillaMux_ParseAll(b *testing.B) {
579595
benchRoutes(b, parseGorillaMux, parseAPI)
580596
}
597+
func BenchmarkGowwwRouter_ParseAll(b *testing.B) {
598+
benchRoutes(b, parseGowwwRouter, parseAPI)
599+
}
581600
func BenchmarkHttpRouter_ParseAll(b *testing.B) {
582601
benchRoutes(b, parseHttpRouter, parseAPI)
583602
}

routers.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/go-zoo/bone"
3232
"github.com/gocraft/web"
3333
"github.com/gorilla/mux"
34+
gowwwrouter "github.com/gowww/router"
3435
"github.com/julienschmidt/httprouter"
3536
"github.com/labstack/echo"
3637
llog "github.com/lunny/log"
@@ -99,7 +100,7 @@ func init() {
99100
}
100101

101102
// Common
102-
func httpHandlerFunc(w http.ResponseWriter, r *http.Request) {}
103+
func httpHandlerFunc(_ http.ResponseWriter, _ *http.Request) {}
103104

104105
func httpHandlerFuncTest(w http.ResponseWriter, r *http.Request) {
105106
io.WriteString(w, r.RequestURI)
@@ -345,7 +346,7 @@ func echoHandlerTest(c echo.Context) error {
345346

346347
func loadEcho(routes []route) http.Handler {
347348
var h echo.HandlerFunc = echoHandler
348-
if loadTestHandler {
349+
if loadTestHandler {
349350
h = echoHandlerTest
350351
}
351352

@@ -724,6 +725,30 @@ func loadGorillaMuxSingle(method, path string, handler http.HandlerFunc) http.Ha
724725
return m
725726
}
726727

728+
// gowww/router
729+
func gowwwRouterHandleWrite(w http.ResponseWriter, r *http.Request) {
730+
io.WriteString(w, gowwwrouter.Parameter(r, "name"))
731+
}
732+
733+
func loadGowwwRouter(routes []route) http.Handler {
734+
h := httpHandlerFunc
735+
if loadTestHandler {
736+
h = httpHandlerFuncTest
737+
}
738+
739+
router := gowwwrouter.New()
740+
for _, route := range routes {
741+
router.Handle(route.method, route.path, http.HandlerFunc(h))
742+
}
743+
return router
744+
}
745+
746+
func loadGowwwRouterSingle(method, path string, handler http.Handler) http.Handler {
747+
router := gowwwrouter.New()
748+
router.Handle(method, path, handler)
749+
return router
750+
}
751+
727752
// HttpRouter
728753
func httpRouterHandle(_ http.ResponseWriter, _ *http.Request, _ httprouter.Params) {}
729754

routers_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
{"GoJsonRest", loadGoJsonRest},
2626
{"GoRestful", loadGoRestful},
2727
{"GorillaMux", loadGorillaMux},
28+
{"GowwwRouter", loadGowwwRouter},
2829
{"HttpRouter", loadHttpRouter},
2930
{"HttpTreeMux", loadHttpTreeMux},
3031
//{"Kocha", loadKocha},

static_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ var staticRoutes = []route{
4141
{"GET", "/share.png"},
4242
{"GET", "/sieve.gif"},
4343
{"GET", "/tos.html"},
44-
{"GET", "/articles/"},
44+
{"GET", "/articles"},
4545
{"GET", "/articles/go_command.html"},
4646
{"GET", "/articles/index.html"},
47-
{"GET", "/articles/wiki/"},
47+
{"GET", "/articles/wiki"},
4848
{"GET", "/articles/wiki/edit.html"},
4949
{"GET", "/articles/wiki/final-noclosure.go"},
5050
{"GET", "/articles/wiki/final-noerror.go"},
@@ -66,7 +66,7 @@ var staticRoutes = []route{
6666
{"GET", "/articles/wiki/test_Test.txt.good"},
6767
{"GET", "/articles/wiki/test_view.good"},
6868
{"GET", "/articles/wiki/view.html"},
69-
{"GET", "/codewalk/"},
69+
{"GET", "/codewalk"},
7070
{"GET", "/codewalk/codewalk.css"},
7171
{"GET", "/codewalk/codewalk.js"},
7272
{"GET", "/codewalk/codewalk.xml"},
@@ -78,10 +78,10 @@ var staticRoutes = []route{
7878
{"GET", "/codewalk/run"},
7979
{"GET", "/codewalk/sharemem.xml"},
8080
{"GET", "/codewalk/urlpoll.go"},
81-
{"GET", "/devel/"},
81+
{"GET", "/devel"},
8282
{"GET", "/devel/release.html"},
8383
{"GET", "/devel/weekly.html"},
84-
{"GET", "/gopher/"},
84+
{"GET", "/gopher"},
8585
{"GET", "/gopher/appenginegopher.jpg"},
8686
{"GET", "/gopher/appenginegophercolor.jpg"},
8787
{"GET", "/gopher/appenginelogo.gif"},
@@ -101,14 +101,14 @@ var staticRoutes = []route{
101101
{"GET", "/gopher/ref.png"},
102102
{"GET", "/gopher/run.png"},
103103
{"GET", "/gopher/talks.png"},
104-
{"GET", "/gopher/pencil/"},
104+
{"GET", "/gopher/pencil"},
105105
{"GET", "/gopher/pencil/gopherhat.jpg"},
106106
{"GET", "/gopher/pencil/gopherhelmet.jpg"},
107107
{"GET", "/gopher/pencil/gophermega.jpg"},
108108
{"GET", "/gopher/pencil/gopherrunning.jpg"},
109109
{"GET", "/gopher/pencil/gopherswim.jpg"},
110110
{"GET", "/gopher/pencil/gopherswrench.jpg"},
111-
{"GET", "/play/"},
111+
{"GET", "/play"},
112112
{"GET", "/play/fib.go"},
113113
{"GET", "/play/hello.go"},
114114
{"GET", "/play/life.go"},
@@ -117,7 +117,7 @@ var staticRoutes = []route{
117117
{"GET", "/play/sieve.go"},
118118
{"GET", "/play/solitaire.go"},
119119
{"GET", "/play/tree.go"},
120-
{"GET", "/progs/"},
120+
{"GET", "/progs"},
121121
{"GET", "/progs/cgo1.go"},
122122
{"GET", "/progs/cgo2.go"},
123123
{"GET", "/progs/cgo3.go"},
@@ -185,6 +185,7 @@ var (
185185
staticGoJsonRest http.Handler
186186
staticGoRestful http.Handler
187187
staticGorillaMux http.Handler
188+
staticGowwwRouter http.Handler
188189
staticHttpRouter http.Handler
189190
staticHttpTreeMux http.Handler
190191
staticKocha http.Handler
@@ -253,6 +254,9 @@ func init() {
253254
calcMem("GorillaMux", func() {
254255
staticGorillaMux = loadGorillaMux(staticRoutes)
255256
})
257+
calcMem("GowwwRouter", func() {
258+
staticGowwwRouter = loadGowwwRouter(staticRoutes)
259+
})
256260
calcMem("HttpRouter", func() {
257261
staticHttpRouter = loadHttpRouter(staticRoutes)
258262
})
@@ -349,6 +353,9 @@ func BenchmarkGoRestful_StaticAll(b *testing.B) {
349353
func BenchmarkGorillaMux_StaticAll(b *testing.B) {
350354
benchRoutes(b, staticGorillaMux, staticRoutes)
351355
}
356+
func BenchmarkGowwwRouter_StaticAll(b *testing.B) {
357+
benchRoutes(b, staticGowwwRouter, staticRoutes)
358+
}
352359
func BenchmarkHttpRouter_StaticAll(b *testing.B) {
353360
benchRoutes(b, staticHttpRouter, staticRoutes)
354361
}

0 commit comments

Comments
 (0)