Skip to content

Commit 2f79a15

Browse files
committed
Add goji v2 benchmark
1 parent bcf0ab1 commit 2f79a15

File tree

7 files changed

+144
-0
lines changed

7 files changed

+144
-0
lines changed

bench_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ func BenchmarkGoji_Param(b *testing.B) {
150150
r, _ := http.NewRequest("GET", "/user/gordon", nil)
151151
benchRequest(b, router, r)
152152
}
153+
func BenchmarkGojiv2_Param(b *testing.B) {
154+
router := loadGojiv2Single("GET", "/user/:name", gojiv2Handler)
155+
156+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
157+
benchRequest(b, router, r)
158+
}
153159
func BenchmarkGoJsonRest_Param(b *testing.B) {
154160
router := loadGoJsonRestSingle("GET", "/user/:name", goJsonRestHandler)
155161

@@ -325,6 +331,12 @@ func BenchmarkGoji_Param5(b *testing.B) {
325331
r, _ := http.NewRequest("GET", fiveRoute, nil)
326332
benchRequest(b, router, r)
327333
}
334+
func BenchmarkGojiv2_Param5(b *testing.B) {
335+
router := loadGojiv2Single("GET", fiveColon, gojiv2Handler)
336+
337+
r, _ := http.NewRequest("GET", fiveRoute, nil)
338+
benchRequest(b, router, r)
339+
}
328340
func BenchmarkGoJsonRest_Param5(b *testing.B) {
329341
handler := loadGoJsonRestSingle("GET", fiveColon, goJsonRestHandler)
330342

@@ -499,6 +511,12 @@ func BenchmarkGoji_Param20(b *testing.B) {
499511
r, _ := http.NewRequest("GET", twentyRoute, nil)
500512
benchRequest(b, router, r)
501513
}
514+
func BenchmarkGojiv2_Param20(b *testing.B) {
515+
router := loadGojiv2Single("GET", twentyColon, gojiv2Handler)
516+
517+
r, _ := http.NewRequest("GET", twentyRoute, nil)
518+
benchRequest(b, router, r)
519+
}
502520
func BenchmarkGoJsonRest_Param20(b *testing.B) {
503521
handler := loadGoJsonRestSingle("GET", twentyColon, goJsonRestHandler)
504522

@@ -669,6 +687,12 @@ func BenchmarkGoji_ParamWrite(b *testing.B) {
669687
r, _ := http.NewRequest("GET", "/user/gordon", nil)
670688
benchRequest(b, router, r)
671689
}
690+
func BenchmarkGojiv2_ParamWrite(b *testing.B) {
691+
router := loadGojiv2Single("GET", "/user/:name", gojiv2HandlerWrite)
692+
693+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
694+
benchRequest(b, router, r)
695+
}
672696
func BenchmarkGoJsonRest_ParamWrite(b *testing.B) {
673697
handler := loadGoJsonRestSingle("GET", "/user/:name", goJsonRestHandlerWrite)
674698

github_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ var (
283283
githubGin http.Handler
284284
githubGocraftWeb http.Handler
285285
githubGoji http.Handler
286+
githubGojiv2 http.Handler
286287
githubGoJsonRest http.Handler
287288
githubGoRestful http.Handler
288289
githubGorillaMux http.Handler
@@ -333,6 +334,9 @@ func init() {
333334
calcMem("Goji", func() {
334335
githubGoji = loadGoji(githubAPI)
335336
})
337+
calcMem("Gojiv2", func() {
338+
githubGojiv2 = loadGojiv2(githubAPI)
339+
})
336340
calcMem("GoJsonRest", func() {
337341
githubGoJsonRest = loadGoJsonRest(githubAPI)
338342
})
@@ -428,6 +432,10 @@ func BenchmarkGoji_GithubStatic(b *testing.B) {
428432
req, _ := http.NewRequest("GET", "/user/repos", nil)
429433
benchRequest(b, githubGoji, req)
430434
}
435+
func BenchmarkGojiv2_GithubStatic(b *testing.B) {
436+
req, _ := http.NewRequest("GET", "/user/repos", nil)
437+
benchRequest(b, githubGojiv2, req)
438+
}
431439
func BenchmarkGoRestful_GithubStatic(b *testing.B) {
432440
req, _ := http.NewRequest("GET", "/user/repos", nil)
433441
benchRequest(b, githubGoRestful, req)
@@ -539,6 +547,10 @@ func BenchmarkGoji_GithubParam(b *testing.B) {
539547
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
540548
benchRequest(b, githubGoji, req)
541549
}
550+
func BenchmarkGojiv2_GithubParam(b *testing.B) {
551+
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
552+
benchRequest(b, githubGojiv2, req)
553+
}
542554
func BenchmarkGoJsonRest_GithubParam(b *testing.B) {
543555
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
544556
benchRequest(b, githubGoJsonRest, req)
@@ -641,6 +653,9 @@ func BenchmarkGocraftWeb_GithubAll(b *testing.B) {
641653
func BenchmarkGoji_GithubAll(b *testing.B) {
642654
benchRoutes(b, githubGoji, githubAPI)
643655
}
656+
func BenchmarkGojiv2_GithubAll(b *testing.B) {
657+
benchRoutes(b, githubGojiv2, githubAPI)
658+
}
644659
func BenchmarkGoJsonRest_GithubAll(b *testing.B) {
645660
benchRoutes(b, githubGoJsonRest, githubAPI)
646661
}

gplus_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
gplusGin http.Handler
4646
gplusGocraftWeb http.Handler
4747
gplusGoji http.Handler
48+
gplusGojiv2 http.Handler
4849
gplusGoJsonRest http.Handler
4950
gplusGoRestful http.Handler
5051
gplusGorillaMux http.Handler
@@ -95,6 +96,9 @@ func init() {
9596
calcMem("Goji", func() {
9697
gplusGoji = loadGoji(gplusAPI)
9798
})
99+
calcMem("Gojiv2", func() {
100+
gplusGojiv2 = loadGojiv2(gplusAPI)
101+
})
98102
calcMem("GoJsonRest", func() {
99103
gplusGoJsonRest = loadGoJsonRest(gplusAPI)
100104
})
@@ -190,6 +194,10 @@ func BenchmarkGoji_GPlusStatic(b *testing.B) {
190194
req, _ := http.NewRequest("GET", "/people", nil)
191195
benchRequest(b, gplusGoji, req)
192196
}
197+
func BenchmarkGojiv2_GPlusStatic(b *testing.B) {
198+
req, _ := http.NewRequest("GET", "/people", nil)
199+
benchRequest(b, gplusGojiv2, req)
200+
}
193201
func BenchmarkGoJsonRest_GPlusStatic(b *testing.B) {
194202
req, _ := http.NewRequest("GET", "/people", nil)
195203
benchRequest(b, gplusGoJsonRest, req)
@@ -301,6 +309,10 @@ func BenchmarkGoji_GPlusParam(b *testing.B) {
301309
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
302310
benchRequest(b, gplusGoji, req)
303311
}
312+
func BenchmarkGojiv2_GPlusParam(b *testing.B) {
313+
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
314+
benchRequest(b, gplusGojiv2, req)
315+
}
304316
func BenchmarkGoJsonRest_GPlusParam(b *testing.B) {
305317
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
306318
benchRequest(b, gplusGoJsonRest, req)
@@ -412,6 +424,10 @@ func BenchmarkGoji_GPlus2Params(b *testing.B) {
412424
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
413425
benchRequest(b, gplusGoji, req)
414426
}
427+
func BenchmarkGojiv2_GPlus2Params(b *testing.B) {
428+
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
429+
benchRequest(b, gplusGojiv2, req)
430+
}
415431
func BenchmarkGoJsonRest_GPlus2Params(b *testing.B) {
416432
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
417433
benchRequest(b, gplusGoJsonRest, req)
@@ -514,6 +530,9 @@ func BenchmarkGocraftWeb_GPlusAll(b *testing.B) {
514530
func BenchmarkGoji_GPlusAll(b *testing.B) {
515531
benchRoutes(b, gplusGoji, gplusAPI)
516532
}
533+
func BenchmarkGojiv2_GPlusAll(b *testing.B) {
534+
benchRoutes(b, gplusGojiv2, gplusAPI)
535+
}
517536
func BenchmarkGoJsonRest_GPlusAll(b *testing.B) {
518537
benchRoutes(b, gplusGoJsonRest, gplusAPI)
519538
}

parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
parseGin http.Handler
6666
parseGocraftWeb http.Handler
6767
parseGoji http.Handler
68+
parseGojiv2 http.Handler
6869
parseGoJsonRest http.Handler
6970
parseGoRestful http.Handler
7071
parseGorillaMux http.Handler
@@ -115,6 +116,9 @@ func init() {
115116
calcMem("Goji", func() {
116117
parseGoji = loadGoji(parseAPI)
117118
})
119+
calcMem("Gojiv2", func() {
120+
parseGojiv2 = loadGojiv2(parseAPI)
121+
})
118122
calcMem("GoJsonRest", func() {
119123
parseGoJsonRest = loadGoJsonRest(parseAPI)
120124
})
@@ -210,6 +214,10 @@ func BenchmarkGoji_ParseStatic(b *testing.B) {
210214
req, _ := http.NewRequest("GET", "/1/users", nil)
211215
benchRequest(b, parseGoji, req)
212216
}
217+
func BenchmarkGojiv2_ParseStatic(b *testing.B) {
218+
req, _ := http.NewRequest("GET", "/1/users", nil)
219+
benchRequest(b, parseGojiv2, req)
220+
}
213221
func BenchmarkGoJsonRest_ParseStatic(b *testing.B) {
214222
req, _ := http.NewRequest("GET", "/1/users", nil)
215223
benchRequest(b, parseGoJsonRest, req)
@@ -321,6 +329,10 @@ func BenchmarkGoji_ParseParam(b *testing.B) {
321329
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
322330
benchRequest(b, parseGoji, req)
323331
}
332+
func BenchmarkGojiv2_ParseParam(b *testing.B) {
333+
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
334+
benchRequest(b, parseGojiv2, req)
335+
}
324336
func BenchmarkGoJsonRest_ParseParam(b *testing.B) {
325337
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
326338
benchRequest(b, parseGoJsonRest, req)
@@ -432,6 +444,10 @@ func BenchmarkGoji_Parse2Params(b *testing.B) {
432444
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
433445
benchRequest(b, parseGoji, req)
434446
}
447+
func BenchmarkGojiv2_Parse2Params(b *testing.B) {
448+
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
449+
benchRequest(b, parseGojiv2, req)
450+
}
435451
func BenchmarkGoJsonRest_Parse2Params(b *testing.B) {
436452
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
437453
benchRequest(b, parseGoJsonRest, req)
@@ -534,6 +550,9 @@ func BenchmarkGocraftWeb_ParseAll(b *testing.B) {
534550
func BenchmarkGoji_ParseAll(b *testing.B) {
535551
benchRoutes(b, parseGoji, parseAPI)
536552
}
553+
func BenchmarkGojiv2_ParseAll(b *testing.B) {
554+
benchRoutes(b, parseGojiv2, parseAPI)
555+
}
537556
func BenchmarkGoJsonRest_ParseAll(b *testing.B) {
538557
benchRoutes(b, parseGoJsonRest, parseAPI)
539558
}

routers.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ import (
5050
"github.com/ursiform/bear"
5151
"github.com/vanng822/r2router"
5252
goji "github.com/zenazn/goji/web"
53+
gojiv2 "goji.io"
54+
gojiv2pat "goji.io/pat"
55+
gcontext "golang.org/x/net/context"
5356
)
5457

5558
type route struct {
@@ -527,6 +530,62 @@ func loadGojiSingle(method, path string, handler interface{}) http.Handler {
527530
return mux
528531
}
529532

533+
// goji v2 (github.com/goji/goji)
534+
func gojiv2Handler(ctx gcontext.Context, w http.ResponseWriter, r *http.Request) {}
535+
536+
func gojiv2HandlerWrite(ctx gcontext.Context, w http.ResponseWriter, r *http.Request) {
537+
io.WriteString(w, gojiv2pat.Param(ctx, "name"))
538+
}
539+
540+
func gojiv2HandlerTest(ctx gcontext.Context, w http.ResponseWriter, r *http.Request) {
541+
io.WriteString(w, r.RequestURI)
542+
}
543+
544+
func loadGojiv2(routes []route) http.Handler {
545+
h := gojiv2Handler
546+
if loadTestHandler {
547+
h = gojiv2HandlerTest
548+
}
549+
550+
mux := gojiv2.NewMux()
551+
for _, route := range routes {
552+
switch route.method {
553+
case "GET":
554+
mux.HandleFuncC(gojiv2pat.Get(route.path), h)
555+
case "POST":
556+
mux.HandleFuncC(gojiv2pat.Post(route.path), h)
557+
case "PUT":
558+
mux.HandleFuncC(gojiv2pat.Put(route.path), h)
559+
case "PATCH":
560+
mux.HandleFuncC(gojiv2pat.Patch(route.path), h)
561+
case "DELETE":
562+
mux.HandleFuncC(gojiv2pat.Delete(route.path), h)
563+
default:
564+
panic("Unknown HTTP method: " + route.method)
565+
}
566+
}
567+
return mux
568+
}
569+
570+
func loadGojiv2Single(method, path string, handler gojiv2.HandlerFunc) http.Handler {
571+
mux := gojiv2.NewMux()
572+
switch method {
573+
case "GET":
574+
mux.HandleFuncC(gojiv2pat.Get(path), handler)
575+
case "POST":
576+
mux.HandleFuncC(gojiv2pat.Post(path), handler)
577+
case "PUT":
578+
mux.HandleFuncC(gojiv2pat.Put(path), handler)
579+
case "PATCH":
580+
mux.HandleFuncC(gojiv2pat.Patch(path), handler)
581+
case "DELETE":
582+
mux.HandleFuncC(gojiv2pat.Delete(path), handler)
583+
default:
584+
panic("Unknow HTTP method: " + method)
585+
}
586+
return mux
587+
}
588+
530589
// go-json-rest/rest
531590
func goJsonRestHandler(w rest.ResponseWriter, req *rest.Request) {}
532591

routers_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
{"Gin", loadGin},
2222
{"GocraftWeb", loadGocraftWeb},
2323
{"Goji", loadGoji},
24+
{"Gojiv2", loadGojiv2},
2425
{"GoJsonRest", loadGoJsonRest},
2526
{"GoRestful", loadGoRestful},
2627
{"GorillaMux", loadGorillaMux},

static_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ var (
181181
staticGin http.Handler
182182
staticGocraftWeb http.Handler
183183
staticGoji http.Handler
184+
staticGojiv2 http.Handler
184185
staticGoJsonRest http.Handler
185186
staticGoRestful http.Handler
186187
staticGorillaMux http.Handler
@@ -239,6 +240,9 @@ func init() {
239240
calcMem("Goji", func() {
240241
staticGoji = loadGoji(staticRoutes)
241242
})
243+
calcMem("Gojiv2", func() {
244+
staticGojiv2 = loadGojiv2(staticRoutes)
245+
})
242246
calcMem("GoJsonRest", func() {
243247
staticGoJsonRest = loadGoJsonRest(staticRoutes)
244248
})
@@ -329,6 +333,9 @@ func BenchmarkGocraftWeb_StaticAll(b *testing.B) {
329333
func BenchmarkGoji_StaticAll(b *testing.B) {
330334
benchRoutes(b, staticGoji, staticRoutes)
331335
}
336+
func BenchmarkGojiv2_StaticAll(b *testing.B) {
337+
benchRoutes(b, staticGojiv2, staticRoutes)
338+
}
332339
func BenchmarkGoJsonRest_StaticAll(b *testing.B) {
333340
benchRoutes(b, staticGoJsonRest, staticRoutes)
334341
}

0 commit comments

Comments
 (0)