Skip to content

Commit f92d681

Browse files
committed
Merge pull request #24 from vanng822/master
Adding R2router to benchmark
2 parents 7b1fba7 + dcbf18a commit f92d681

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Of course the tested routers can be used for any kind of HTTP request → handle
2121
* [Kocha-urlrouter](https://github.com/naoina/kocha-urlrouter)
2222
* [Martini](https://github.com/go-martini/martini)
2323
* [Pat](https://github.com/bmizerany/pat)
24+
* [R2router](https://github.com/vanng822/r2router)
2425
* [TigerTonic](https://github.com/rcrowley/go-tigertonic)
2526
* [Traffic](https://github.com/pilu/traffic)
2627

bench_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func BenchmarkPat_Param(b *testing.B) {
177177
r, _ := http.NewRequest("GET", "/user/gordon", nil)
178178
benchRequest(b, router, r)
179179
}
180+
181+
func BenchmarkR2router_Param(b *testing.B) {
182+
router := loadR2routerSingle("GET", "/user/:name", r2routerHandler)
183+
184+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
185+
benchRequest(b, router, r)
186+
}
187+
180188
func BenchmarkRevel_Param(b *testing.B) {
181189
router := loadRevelSingle("GET", "/user/:name", "RevelController.Handle")
182190

@@ -337,6 +345,14 @@ func BenchmarkPat_Param5(b *testing.B) {
337345
r, _ := http.NewRequest("GET", fiveRoute, nil)
338346
benchRequest(b, router, r)
339347
}
348+
349+
func BenchmarkR2router_Param5(b *testing.B) {
350+
router := loadR2routerSingle("GET", fiveColon, r2routerHandler)
351+
352+
r, _ := http.NewRequest("GET", fiveRoute, nil)
353+
benchRequest(b, router, r)
354+
}
355+
340356
func BenchmarkRevel_Param5(b *testing.B) {
341357
router := loadRevelSingle("GET", fiveColon, "RevelController.Handle")
342358

@@ -497,6 +513,14 @@ func BenchmarkPat_Param20(b *testing.B) {
497513
r, _ := http.NewRequest("GET", twentyRoute, nil)
498514
benchRequest(b, router, r)
499515
}
516+
517+
func BenchmarkR2router_Param20(b *testing.B) {
518+
router := loadR2routerSingle("GET", twentyColon, r2routerHandler)
519+
520+
r, _ := http.NewRequest("GET", twentyRoute, nil)
521+
benchRequest(b, router, r)
522+
}
523+
500524
func BenchmarkRevel_Param20(b *testing.B) {
501525
router := loadRevelSingle("GET", twentyColon, "RevelController.Handle")
502526

@@ -653,6 +677,14 @@ func BenchmarkPat_ParamWrite(b *testing.B) {
653677
r, _ := http.NewRequest("GET", "/user/gordon", nil)
654678
benchRequest(b, router, r)
655679
}
680+
681+
func BenchmarkR2router_ParamWrite(b *testing.B) {
682+
router := loadR2routerSingle("GET", "/user/:name", r2routerHandleWrite)
683+
684+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
685+
benchRequest(b, router, r)
686+
}
687+
656688
func BenchmarkRevel_ParamWrite(b *testing.B) {
657689
router := loadRevelSingle("GET", "/user/:name", "RevelController.HandleWrite")
658690

github_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ var (
292292
githubMacaron http.Handler
293293
githubMartini http.Handler
294294
githubPat http.Handler
295+
githubR2router http.Handler
295296
githubRevel http.Handler
296297
githubRivet http.Handler
297298
githubTango http.Handler
@@ -358,6 +359,9 @@ func init() {
358359
calcMem("Pat", func() {
359360
githubPat = loadPat(githubAPI)
360361
})
362+
calcMem("R2router", func() {
363+
githubR2router = loadR2router(githubAPI)
364+
})
361365
calcMem("Revel", func() {
362366
githubRevel = loadRevel(githubAPI)
363367
})
@@ -456,6 +460,10 @@ func BenchmarkPat_GithubStatic(b *testing.B) {
456460
req, _ := http.NewRequest("GET", "/user/repos", nil)
457461
benchRequest(b, githubPat, req)
458462
}
463+
func BenchmarkR2router_GithubStatic(b *testing.B) {
464+
req, _ := http.NewRequest("GET", "/user/repos", nil)
465+
benchRequest(b, githubR2router, req)
466+
}
459467
func BenchmarkRevel_GithubStatic(b *testing.B) {
460468
req, _ := http.NewRequest("GET", "/user/repos", nil)
461469
benchRequest(b, githubRevel, req)
@@ -558,6 +566,10 @@ func BenchmarkPat_GithubParam(b *testing.B) {
558566
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
559567
benchRequest(b, githubPat, req)
560568
}
569+
func BenchmarkR2router_GithubParam(b *testing.B) {
570+
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
571+
benchRequest(b, githubR2router, req)
572+
}
561573
func BenchmarkRevel_GithubParam(b *testing.B) {
562574
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
563575
benchRequest(b, githubRevel, req)
@@ -642,6 +654,9 @@ func BenchmarkMartini_GithubAll(b *testing.B) {
642654
func BenchmarkPat_GithubAll(b *testing.B) {
643655
benchRoutes(b, githubPat, githubAPI)
644656
}
657+
func BenchmarkR2router_GithubAll(b *testing.B) {
658+
benchRoutes(b, githubR2router, githubAPI)
659+
}
645660
func BenchmarkRevel_GithubAll(b *testing.B) {
646661
benchRoutes(b, githubRevel, githubAPI)
647662
}

gplus_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454
gplusMacaron http.Handler
5555
gplusMartini http.Handler
5656
gplusPat http.Handler
57+
gplusR2router http.Handler
5758
gplusRevel http.Handler
5859
gplusRivet http.Handler
5960
gplusTango http.Handler
@@ -120,6 +121,9 @@ func init() {
120121
calcMem("Pat", func() {
121122
gplusPat = loadPat(gplusAPI)
122123
})
124+
calcMem("R2router", func() {
125+
gplusR2router = loadR2router(gplusAPI)
126+
})
123127
calcMem("Revel", func() {
124128
gplusRevel = loadRevel(gplusAPI)
125129
})
@@ -218,6 +222,10 @@ func BenchmarkPat_GPlusStatic(b *testing.B) {
218222
req, _ := http.NewRequest("GET", "/people", nil)
219223
benchRequest(b, gplusPat, req)
220224
}
225+
func BenchmarkR2router_GPlusStatic(b *testing.B) {
226+
req, _ := http.NewRequest("GET", "/people", nil)
227+
benchRequest(b, gplusR2router, req)
228+
}
221229
func BenchmarkRevel_GPlusStatic(b *testing.B) {
222230
req, _ := http.NewRequest("GET", "/people", nil)
223231
benchRequest(b, gplusRevel, req)
@@ -320,6 +328,10 @@ func BenchmarkPat_GPlusParam(b *testing.B) {
320328
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
321329
benchRequest(b, gplusPat, req)
322330
}
331+
func BenchmarkR2router_GPlusParam(b *testing.B) {
332+
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
333+
benchRequest(b, gplusR2router, req)
334+
}
323335
func BenchmarkRevel_GPlusParam(b *testing.B) {
324336
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
325337
benchRequest(b, gplusRevel, req)
@@ -422,6 +434,10 @@ func BenchmarkPat_GPlus2Params(b *testing.B) {
422434
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
423435
benchRequest(b, gplusPat, req)
424436
}
437+
func BenchmarkR2router_GPlus2Params(b *testing.B) {
438+
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
439+
benchRequest(b, gplusR2router, req)
440+
}
425441
func BenchmarkRevel_GPlus2Params(b *testing.B) {
426442
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
427443
benchRequest(b, gplusRevel, req)
@@ -506,6 +522,9 @@ func BenchmarkMartini_GPlusAll(b *testing.B) {
506522
func BenchmarkPat_GPlusAll(b *testing.B) {
507523
benchRoutes(b, gplusPat, gplusAPI)
508524
}
525+
func BenchmarkR2router_GPlusAll(b *testing.B) {
526+
benchRoutes(b, gplusR2router, gplusAPI)
527+
}
509528
func BenchmarkRevel_GPlusAll(b *testing.B) {
510529
benchRoutes(b, gplusRevel, gplusAPI)
511530
}

parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var (
7474
parseMacaron http.Handler
7575
parseMartini http.Handler
7676
parsePat http.Handler
77+
parseR2router http.Handler
7778
parseRevel http.Handler
7879
parseRivet http.Handler
7980
parseTango http.Handler
@@ -140,6 +141,9 @@ func init() {
140141
calcMem("Pat", func() {
141142
parsePat = loadPat(parseAPI)
142143
})
144+
calcMem("R2router", func() {
145+
parseR2router = loadR2router(parseAPI)
146+
})
143147
calcMem("Revel", func() {
144148
parseRevel = loadRevel(parseAPI)
145149
})
@@ -238,6 +242,10 @@ func BenchmarkPat_ParseStatic(b *testing.B) {
238242
req, _ := http.NewRequest("GET", "/1/users", nil)
239243
benchRequest(b, parsePat, req)
240244
}
245+
func BenchmarkR2router_ParseStatic(b *testing.B) {
246+
req, _ := http.NewRequest("GET", "/1/users", nil)
247+
benchRequest(b, parseR2router, req)
248+
}
241249
func BenchmarkRevel_ParseStatic(b *testing.B) {
242250
req, _ := http.NewRequest("GET", "/1/users", nil)
243251
benchRequest(b, parseRevel, req)
@@ -340,6 +348,10 @@ func BenchmarkPat_ParseParam(b *testing.B) {
340348
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
341349
benchRequest(b, parsePat, req)
342350
}
351+
func BenchmarkR2router_ParseParam(b *testing.B) {
352+
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
353+
benchRequest(b, parseR2router, req)
354+
}
343355
func BenchmarkRevel_ParseParam(b *testing.B) {
344356
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
345357
benchRequest(b, parseRevel, req)
@@ -442,6 +454,10 @@ func BenchmarkPat_Parse2Params(b *testing.B) {
442454
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
443455
benchRequest(b, parsePat, req)
444456
}
457+
func BenchmarkR2router_Parse2Params(b *testing.B) {
458+
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
459+
benchRequest(b, parseR2router, req)
460+
}
445461
func BenchmarkRevel_Parse2Params(b *testing.B) {
446462
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
447463
benchRequest(b, parseRevel, req)
@@ -526,6 +542,9 @@ func BenchmarkMartini_ParseAll(b *testing.B) {
526542
func BenchmarkPat_ParseAll(b *testing.B) {
527543
benchRoutes(b, parsePat, parseAPI)
528544
}
545+
func BenchmarkR2router_ParseAll(b *testing.B) {
546+
benchRoutes(b, parseR2router, parseAPI)
547+
}
529548
func BenchmarkRevel_ParseAll(b *testing.B) {
530549
benchRoutes(b, parseRevel, parseAPI)
531550
}

routers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/typepress/rivet"
4747
"github.com/ursiform/bear"
4848
goji "github.com/zenazn/goji/web"
49+
"github.com/vanng822/r2router"
4950
)
5051

5152
type route struct {
@@ -790,6 +791,26 @@ func loadPatSingle(method, path string, handler http.Handler) http.Handler {
790791
return m
791792
}
792793

794+
// R2router
795+
func r2routerHandler(w http.ResponseWriter, req *http.Request, _ r2router.Params) {}
796+
func r2routerHandleWrite(w http.ResponseWriter, req *http.Request, params r2router.Params) {
797+
io.WriteString(w, params.Get("name"))
798+
}
799+
800+
func loadR2router(routes []route) http.Handler {
801+
router := r2router.NewRouter()
802+
for _, r := range routes {
803+
router.AddHandler(r.method, r.path, r2routerHandler)
804+
}
805+
return router
806+
}
807+
808+
func loadR2routerSingle(method, path string, handler r2router.HandlerFunc) http.Handler {
809+
router := r2router.NewRouter()
810+
router.AddHandler(method, path, handler)
811+
return router
812+
}
813+
793814
// Revel (Router only)
794815
// In the following code some Revel internals are modelled.
795816
// The original revel code is copyrighted by Rob Figueiredo.

static_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var (
190190
staticMacaron http.Handler
191191
staticMartini http.Handler
192192
staticPat http.Handler
193+
staticR2router http.Handler
193194
staticRevel http.Handler
194195
staticRivet http.Handler
195196
staticTango http.Handler
@@ -264,6 +265,9 @@ func init() {
264265
calcMem("Pat", func() {
265266
staticPat = loadPat(staticRoutes)
266267
})
268+
calcMem("R2router", func() {
269+
staticR2router = loadR2router(staticRoutes)
270+
})
267271
calcMem("Revel", func() {
268272
staticRevel = loadRevel(staticRoutes)
269273
})
@@ -348,6 +352,9 @@ func BenchmarkMartini_StaticAll(b *testing.B) {
348352
func BenchmarkPat_StaticAll(b *testing.B) {
349353
benchRoutes(b, staticPat, staticRoutes)
350354
}
355+
func BenchmarkR2router_StaticAll(b *testing.B) {
356+
benchRoutes(b, staticR2router, staticRoutes)
357+
}
351358
func BenchmarkRevel_StaticAll(b *testing.B) {
352359
benchRoutes(b, staticRevel, staticRoutes)
353360
}

0 commit comments

Comments
 (0)