Skip to content

Commit 2b13695

Browse files
committed
Merge pull request #54 from epipho/gojiv2
Add goji v2 benchmark
2 parents 83d33f4 + 2f79a15 commit 2b13695

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

@@ -331,6 +337,12 @@ func BenchmarkGoji_Param5(b *testing.B) {
331337
r, _ := http.NewRequest("GET", fiveRoute, nil)
332338
benchRequest(b, router, r)
333339
}
340+
func BenchmarkGojiv2_Param5(b *testing.B) {
341+
router := loadGojiv2Single("GET", fiveColon, gojiv2Handler)
342+
343+
r, _ := http.NewRequest("GET", fiveRoute, nil)
344+
benchRequest(b, router, r)
345+
}
334346
func BenchmarkGoJsonRest_Param5(b *testing.B) {
335347
handler := loadGoJsonRestSingle("GET", fiveColon, goJsonRestHandler)
336348

@@ -511,6 +523,12 @@ func BenchmarkGoji_Param20(b *testing.B) {
511523
r, _ := http.NewRequest("GET", twentyRoute, nil)
512524
benchRequest(b, router, r)
513525
}
526+
func BenchmarkGojiv2_Param20(b *testing.B) {
527+
router := loadGojiv2Single("GET", twentyColon, gojiv2Handler)
528+
529+
r, _ := http.NewRequest("GET", twentyRoute, nil)
530+
benchRequest(b, router, r)
531+
}
514532
func BenchmarkGoJsonRest_Param20(b *testing.B) {
515533
handler := loadGoJsonRestSingle("GET", twentyColon, goJsonRestHandler)
516534

@@ -687,6 +705,12 @@ func BenchmarkGoji_ParamWrite(b *testing.B) {
687705
r, _ := http.NewRequest("GET", "/user/gordon", nil)
688706
benchRequest(b, router, r)
689707
}
708+
func BenchmarkGojiv2_ParamWrite(b *testing.B) {
709+
router := loadGojiv2Single("GET", "/user/:name", gojiv2HandlerWrite)
710+
711+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
712+
benchRequest(b, router, r)
713+
}
690714
func BenchmarkGoJsonRest_ParamWrite(b *testing.B) {
691715
handler := loadGoJsonRestSingle("GET", "/user/:name", goJsonRestHandlerWrite)
692716

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
@@ -334,6 +335,9 @@ func init() {
334335
calcMem("Goji", func() {
335336
githubGoji = loadGoji(githubAPI)
336337
})
338+
calcMem("Gojiv2", func() {
339+
githubGojiv2 = loadGojiv2(githubAPI)
340+
})
337341
calcMem("GoJsonRest", func() {
338342
githubGoJsonRest = loadGoJsonRest(githubAPI)
339343
})
@@ -432,6 +436,10 @@ func BenchmarkGoji_GithubStatic(b *testing.B) {
432436
req, _ := http.NewRequest("GET", "/user/repos", nil)
433437
benchRequest(b, githubGoji, req)
434438
}
439+
func BenchmarkGojiv2_GithubStatic(b *testing.B) {
440+
req, _ := http.NewRequest("GET", "/user/repos", nil)
441+
benchRequest(b, githubGojiv2, req)
442+
}
435443
func BenchmarkGoRestful_GithubStatic(b *testing.B) {
436444
req, _ := http.NewRequest("GET", "/user/repos", nil)
437445
benchRequest(b, githubGoRestful, req)
@@ -547,6 +555,10 @@ func BenchmarkGoji_GithubParam(b *testing.B) {
547555
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
548556
benchRequest(b, githubGoji, req)
549557
}
558+
func BenchmarkGojiv2_GithubParam(b *testing.B) {
559+
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
560+
benchRequest(b, githubGojiv2, req)
561+
}
550562
func BenchmarkGoJsonRest_GithubParam(b *testing.B) {
551563
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
552564
benchRequest(b, githubGoJsonRest, req)
@@ -653,6 +665,9 @@ func BenchmarkGocraftWeb_GithubAll(b *testing.B) {
653665
func BenchmarkGoji_GithubAll(b *testing.B) {
654666
benchRoutes(b, githubGoji, githubAPI)
655667
}
668+
func BenchmarkGojiv2_GithubAll(b *testing.B) {
669+
benchRoutes(b, githubGojiv2, githubAPI)
670+
}
656671
func BenchmarkGoJsonRest_GithubAll(b *testing.B) {
657672
benchRoutes(b, githubGoJsonRest, githubAPI)
658673
}

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
@@ -96,6 +97,9 @@ func init() {
9697
calcMem("Goji", func() {
9798
gplusGoji = loadGoji(gplusAPI)
9899
})
100+
calcMem("Gojiv2", func() {
101+
gplusGojiv2 = loadGojiv2(gplusAPI)
102+
})
99103
calcMem("GoJsonRest", func() {
100104
gplusGoJsonRest = loadGoJsonRest(gplusAPI)
101105
})
@@ -194,6 +198,10 @@ func BenchmarkGoji_GPlusStatic(b *testing.B) {
194198
req, _ := http.NewRequest("GET", "/people", nil)
195199
benchRequest(b, gplusGoji, req)
196200
}
201+
func BenchmarkGojiv2_GPlusStatic(b *testing.B) {
202+
req, _ := http.NewRequest("GET", "/people", nil)
203+
benchRequest(b, gplusGojiv2, req)
204+
}
197205
func BenchmarkGoJsonRest_GPlusStatic(b *testing.B) {
198206
req, _ := http.NewRequest("GET", "/people", nil)
199207
benchRequest(b, gplusGoJsonRest, req)
@@ -309,6 +317,10 @@ func BenchmarkGoji_GPlusParam(b *testing.B) {
309317
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
310318
benchRequest(b, gplusGoji, req)
311319
}
320+
func BenchmarkGojiv2_GPlusParam(b *testing.B) {
321+
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
322+
benchRequest(b, gplusGojiv2, req)
323+
}
312324
func BenchmarkGoJsonRest_GPlusParam(b *testing.B) {
313325
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
314326
benchRequest(b, gplusGoJsonRest, req)
@@ -424,6 +436,10 @@ func BenchmarkGoji_GPlus2Params(b *testing.B) {
424436
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
425437
benchRequest(b, gplusGoji, req)
426438
}
439+
func BenchmarkGojiv2_GPlus2Params(b *testing.B) {
440+
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
441+
benchRequest(b, gplusGojiv2, req)
442+
}
427443
func BenchmarkGoJsonRest_GPlus2Params(b *testing.B) {
428444
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
429445
benchRequest(b, gplusGoJsonRest, req)
@@ -530,6 +546,9 @@ func BenchmarkGocraftWeb_GPlusAll(b *testing.B) {
530546
func BenchmarkGoji_GPlusAll(b *testing.B) {
531547
benchRoutes(b, gplusGoji, gplusAPI)
532548
}
549+
func BenchmarkGojiv2_GPlusAll(b *testing.B) {
550+
benchRoutes(b, gplusGojiv2, gplusAPI)
551+
}
533552
func BenchmarkGoJsonRest_GPlusAll(b *testing.B) {
534553
benchRoutes(b, gplusGoJsonRest, gplusAPI)
535554
}

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
@@ -116,6 +117,9 @@ func init() {
116117
calcMem("Goji", func() {
117118
parseGoji = loadGoji(parseAPI)
118119
})
120+
calcMem("Gojiv2", func() {
121+
parseGojiv2 = loadGojiv2(parseAPI)
122+
})
119123
calcMem("GoJsonRest", func() {
120124
parseGoJsonRest = loadGoJsonRest(parseAPI)
121125
})
@@ -214,6 +218,10 @@ func BenchmarkGoji_ParseStatic(b *testing.B) {
214218
req, _ := http.NewRequest("GET", "/1/users", nil)
215219
benchRequest(b, parseGoji, req)
216220
}
221+
func BenchmarkGojiv2_ParseStatic(b *testing.B) {
222+
req, _ := http.NewRequest("GET", "/1/users", nil)
223+
benchRequest(b, parseGojiv2, req)
224+
}
217225
func BenchmarkGoJsonRest_ParseStatic(b *testing.B) {
218226
req, _ := http.NewRequest("GET", "/1/users", nil)
219227
benchRequest(b, parseGoJsonRest, req)
@@ -329,6 +337,10 @@ func BenchmarkGoji_ParseParam(b *testing.B) {
329337
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
330338
benchRequest(b, parseGoji, req)
331339
}
340+
func BenchmarkGojiv2_ParseParam(b *testing.B) {
341+
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
342+
benchRequest(b, parseGojiv2, req)
343+
}
332344
func BenchmarkGoJsonRest_ParseParam(b *testing.B) {
333345
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
334346
benchRequest(b, parseGoJsonRest, req)
@@ -444,6 +456,10 @@ func BenchmarkGoji_Parse2Params(b *testing.B) {
444456
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
445457
benchRequest(b, parseGoji, req)
446458
}
459+
func BenchmarkGojiv2_Parse2Params(b *testing.B) {
460+
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
461+
benchRequest(b, parseGojiv2, req)
462+
}
447463
func BenchmarkGoJsonRest_Parse2Params(b *testing.B) {
448464
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
449465
benchRequest(b, parseGoJsonRest, req)
@@ -550,6 +566,9 @@ func BenchmarkGocraftWeb_ParseAll(b *testing.B) {
550566
func BenchmarkGoji_ParseAll(b *testing.B) {
551567
benchRoutes(b, parseGoji, parseAPI)
552568
}
569+
func BenchmarkGojiv2_ParseAll(b *testing.B) {
570+
benchRoutes(b, parseGojiv2, parseAPI)
571+
}
553572
func BenchmarkGoJsonRest_ParseAll(b *testing.B) {
554573
benchRoutes(b, parseGoJsonRest, parseAPI)
555574
}

routers.go

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

5659
type route struct {
@@ -528,6 +531,62 @@ func loadGojiSingle(method, path string, handler interface{}) http.Handler {
528531
return mux
529532
}
530533

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

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
@@ -240,6 +241,9 @@ func init() {
240241
calcMem("Goji", func() {
241242
staticGoji = loadGoji(staticRoutes)
242243
})
244+
calcMem("Gojiv2", func() {
245+
staticGojiv2 = loadGojiv2(staticRoutes)
246+
})
243247
calcMem("GoJsonRest", func() {
244248
staticGoJsonRest = loadGoJsonRest(staticRoutes)
245249
})
@@ -333,6 +337,9 @@ func BenchmarkGocraftWeb_StaticAll(b *testing.B) {
333337
func BenchmarkGoji_StaticAll(b *testing.B) {
334338
benchRoutes(b, staticGoji, staticRoutes)
335339
}
340+
func BenchmarkGojiv2_StaticAll(b *testing.B) {
341+
benchRoutes(b, staticGojiv2, staticRoutes)
342+
}
336343
func BenchmarkGoJsonRest_StaticAll(b *testing.B) {
337344
benchRoutes(b, staticGoJsonRest, staticRoutes)
338345
}

0 commit comments

Comments
 (0)