Skip to content

Commit ee25ee0

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add LARS to benchmarks
1 parent bcf0ab1 commit ee25ee0

File tree

7 files changed

+152
-3
lines changed

7 files changed

+152
-3
lines changed

bench_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ func BenchmarkKocha_Param(b *testing.B) {
190190
r, _ := http.NewRequest("GET", "/user/gordon", nil)
191191
benchRequest(b, router, r)
192192
}
193+
func BenchmarkLARS_Param(b *testing.B) {
194+
router := loadLARSSingle("GET", "/user/:name", larsHandler)
195+
196+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
197+
benchRequest(b, router, r)
198+
}
193199
func BenchmarkMacaron_Param(b *testing.B) {
194200
router := loadMacaronSingle("GET", "/user/:name", macaronHandler)
195201

@@ -365,6 +371,12 @@ func BenchmarkKocha_Param5(b *testing.B) {
365371
r, _ := http.NewRequest("GET", fiveRoute, nil)
366372
benchRequest(b, router, r)
367373
}
374+
func BenchmarkLARS_Param5(b *testing.B) {
375+
router := loadLARSSingle("GET", fiveColon, larsHandler)
376+
377+
r, _ := http.NewRequest("GET", fiveRoute, nil)
378+
benchRequest(b, router, r)
379+
}
368380
func BenchmarkMacaron_Param5(b *testing.B) {
369381
router := loadMacaronSingle("GET", fiveColon, macaronHandler)
370382

@@ -539,6 +551,12 @@ func BenchmarkKocha_Param20(b *testing.B) {
539551
r, _ := http.NewRequest("GET", twentyRoute, nil)
540552
benchRequest(b, router, r)
541553
}
554+
func BenchmarkLARS_Param20(b *testing.B) {
555+
router := loadLARSSingle("GET", twentyColon, larsHandler)
556+
557+
r, _ := http.NewRequest("GET", twentyRoute, nil)
558+
benchRequest(b, router, r)
559+
}
542560
func BenchmarkMacaron_Param20(b *testing.B) {
543561
router := loadMacaronSingle("GET", twentyColon, macaronHandler)
544562

@@ -709,6 +727,12 @@ func BenchmarkKocha_ParamWrite(b *testing.B) {
709727
r, _ := http.NewRequest("GET", "/user/gordon", nil)
710728
benchRequest(b, router, r)
711729
}
730+
func BenchmarkLARS_ParamWrite(b *testing.B) {
731+
router := loadLARSSingle("GET", "/user/:name", larsHandlerWrite)
732+
733+
r, _ := http.NewRequest("GET", "/user/gordon", nil)
734+
benchRequest(b, router, r)
735+
}
712736
func BenchmarkMacaron_ParamWrite(b *testing.B) {
713737
router := loadMacaronSingle("GET", "/user/:name", macaronHandlerWrite)
714738

github_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ var (
289289
githubHttpRouter http.Handler
290290
githubHttpTreeMux http.Handler
291291
githubKocha http.Handler
292+
githubLARS http.Handler
292293
githubMacaron http.Handler
293294
githubMartini http.Handler
294295
githubPat http.Handler
@@ -351,6 +352,9 @@ func init() {
351352
calcMem("Kocha", func() {
352353
githubKocha = loadKocha(githubAPI)
353354
})
355+
calcMem("LARS", func() {
356+
githubLARS = loadLARS(githubAPI)
357+
})
354358
calcMem("Macaron", func() {
355359
githubMacaron = loadMacaron(githubAPI)
356360
})
@@ -452,6 +456,10 @@ func BenchmarkKocha_GithubStatic(b *testing.B) {
452456
req, _ := http.NewRequest("GET", "/user/repos", nil)
453457
benchRequest(b, githubKocha, req)
454458
}
459+
func BenchmarkLARS_GithubStatic(b *testing.B) {
460+
req, _ := http.NewRequest("GET", "/user/repos", nil)
461+
benchRequest(b, githubLARS, req)
462+
}
455463
func BenchmarkMacaron_GithubStatic(b *testing.B) {
456464
req, _ := http.NewRequest("GET", "/user/repos", nil)
457465
benchRequest(b, githubMacaron, req)
@@ -563,6 +571,10 @@ func BenchmarkKocha_GithubParam(b *testing.B) {
563571
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
564572
benchRequest(b, githubKocha, req)
565573
}
574+
func BenchmarkLARS_GithubParam(b *testing.B) {
575+
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
576+
benchRequest(b, githubLARS, req)
577+
}
566578
func BenchmarkMacaron_GithubParam(b *testing.B) {
567579
req, _ := http.NewRequest("GET", "/repos/julienschmidt/httprouter/stargazers", nil)
568580
benchRequest(b, githubMacaron, req)
@@ -659,6 +671,9 @@ func BenchmarkHttpTreeMux_GithubAll(b *testing.B) {
659671
func BenchmarkKocha_GithubAll(b *testing.B) {
660672
benchRoutes(b, githubKocha, githubAPI)
661673
}
674+
func BenchmarkLARS_GithubAll(b *testing.B) {
675+
benchRoutes(b, githubLARS, githubAPI)
676+
}
662677
func BenchmarkMacaron_GithubAll(b *testing.B) {
663678
benchRoutes(b, githubMacaron, githubAPI)
664679
}

gplus_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var (
5151
gplusHttpRouter http.Handler
5252
gplusHttpTreeMux http.Handler
5353
gplusKocha http.Handler
54+
gplusLARS http.Handler
5455
gplusMacaron http.Handler
5556
gplusMartini http.Handler
5657
gplusPat http.Handler
@@ -113,6 +114,9 @@ func init() {
113114
calcMem("Kocha", func() {
114115
gplusKocha = loadKocha(gplusAPI)
115116
})
117+
calcMem("LARS", func() {
118+
gplusLARS = loadLARS(gplusAPI)
119+
})
116120
calcMem("Macaron", func() {
117121
gplusMacaron = loadMacaron(gplusAPI)
118122
})
@@ -214,6 +218,10 @@ func BenchmarkKocha_GPlusStatic(b *testing.B) {
214218
req, _ := http.NewRequest("GET", "/people", nil)
215219
benchRequest(b, gplusKocha, req)
216220
}
221+
func BenchmarkLARS_GPlusStatic(b *testing.B) {
222+
req, _ := http.NewRequest("GET", "/people", nil)
223+
benchRequest(b, gplusLARS, req)
224+
}
217225
func BenchmarkMacaron_GPlusStatic(b *testing.B) {
218226
req, _ := http.NewRequest("GET", "/people", nil)
219227
benchRequest(b, gplusMacaron, req)
@@ -325,6 +333,10 @@ func BenchmarkKocha_GPlusParam(b *testing.B) {
325333
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
326334
benchRequest(b, gplusKocha, req)
327335
}
336+
func BenchmarkLARS_GPlusParam(b *testing.B) {
337+
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
338+
benchRequest(b, gplusLARS, req)
339+
}
328340
func BenchmarkMacaron_GPlusParam(b *testing.B) {
329341
req, _ := http.NewRequest("GET", "/people/118051310819094153327", nil)
330342
benchRequest(b, gplusMacaron, req)
@@ -436,6 +448,10 @@ func BenchmarkKocha_GPlus2Params(b *testing.B) {
436448
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
437449
benchRequest(b, gplusKocha, req)
438450
}
451+
func BenchmarkLARS_GPlus2Params(b *testing.B) {
452+
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
453+
benchRequest(b, gplusLARS, req)
454+
}
439455
func BenchmarkMacaron_GPlus2Params(b *testing.B) {
440456
req, _ := http.NewRequest("GET", "/people/118051310819094153327/activities/123456789", nil)
441457
benchRequest(b, gplusMacaron, req)
@@ -532,6 +548,9 @@ func BenchmarkHttpTreeMux_GPlusAll(b *testing.B) {
532548
func BenchmarkKocha_GPlusAll(b *testing.B) {
533549
benchRoutes(b, gplusKocha, gplusAPI)
534550
}
551+
func BenchmarkLARS_GPlusAll(b *testing.B) {
552+
benchRoutes(b, gplusLARS, gplusAPI)
553+
}
535554
func BenchmarkMacaron_GPlusAll(b *testing.B) {
536555
benchRoutes(b, gplusMacaron, gplusAPI)
537556
}

parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var (
7171
parseHttpRouter http.Handler
7272
parseHttpTreeMux http.Handler
7373
parseKocha http.Handler
74+
parseLARS http.Handler
7475
parseMacaron http.Handler
7576
parseMartini http.Handler
7677
parsePat http.Handler
@@ -133,6 +134,9 @@ func init() {
133134
calcMem("Kocha", func() {
134135
parseKocha = loadKocha(parseAPI)
135136
})
137+
calcMem("LARS", func() {
138+
parseLARS = loadLARS(parseAPI)
139+
})
136140
calcMem("Macaron", func() {
137141
parseMacaron = loadMacaron(parseAPI)
138142
})
@@ -234,6 +238,10 @@ func BenchmarkKocha_ParseStatic(b *testing.B) {
234238
req, _ := http.NewRequest("GET", "/1/users", nil)
235239
benchRequest(b, parseKocha, req)
236240
}
241+
func BenchmarkLARS_ParseStatic(b *testing.B) {
242+
req, _ := http.NewRequest("GET", "/1/users", nil)
243+
benchRequest(b, parseLARS, req)
244+
}
237245
func BenchmarkMacaron_ParseStatic(b *testing.B) {
238246
req, _ := http.NewRequest("GET", "/1/users", nil)
239247
benchRequest(b, parseMacaron, req)
@@ -345,6 +353,10 @@ func BenchmarkKocha_ParseParam(b *testing.B) {
345353
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
346354
benchRequest(b, parseKocha, req)
347355
}
356+
func BenchmarkLARS_ParseParam(b *testing.B) {
357+
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
358+
benchRequest(b, parseLARS, req)
359+
}
348360
func BenchmarkMacaron_ParseParam(b *testing.B) {
349361
req, _ := http.NewRequest("GET", "/1/classes/go", nil)
350362
benchRequest(b, parseMacaron, req)
@@ -456,6 +468,10 @@ func BenchmarkKocha_Parse2Params(b *testing.B) {
456468
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
457469
benchRequest(b, parseKocha, req)
458470
}
471+
func BenchmarkLARS_Parse2Params(b *testing.B) {
472+
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
473+
benchRequest(b, parseLARS, req)
474+
}
459475
func BenchmarkMacaron_Parse2Params(b *testing.B) {
460476
req, _ := http.NewRequest("GET", "/1/classes/go/123456789", nil)
461477
benchRequest(b, parseMacaron, req)
@@ -552,6 +568,9 @@ func BenchmarkHttpTreeMux_ParseAll(b *testing.B) {
552568
func BenchmarkKocha_ParseAll(b *testing.B) {
553569
benchRoutes(b, parseKocha, parseAPI)
554570
}
571+
func BenchmarkLARS_ParseAll(b *testing.B) {
572+
benchRoutes(b, parseLARS, parseAPI)
573+
}
555574
func BenchmarkMacaron_ParseAll(b *testing.B) {
556575
benchRoutes(b, parseMacaron, parseAPI)
557576
}

routers.go

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/astaxie/beego"
2323
"github.com/astaxie/beego/context"
2424
"github.com/bmizerany/pat"
25+
"github.com/go-playground/lars"
2526
// "github.com/daryl/zeus"
2627
"github.com/dimfeld/httptreemux"
2728
"github.com/emicklei/go-restful"
@@ -807,6 +808,69 @@ func loadKochaSingle(method, path string, handler *kochaHandler, hfunc http.Hand
807808
return handler
808809
}
809810

811+
// LARS
812+
func larsHandler(c lars.Context) {
813+
}
814+
815+
func larsHandlerWrite(c lars.Context) {
816+
io.WriteString(c.Response(), c.Param("name"))
817+
}
818+
819+
func larsHandlerTest(c lars.Context) {
820+
io.WriteString(c.Response(), c.Request().RequestURI)
821+
}
822+
823+
func larsNativeHandlerTest(w http.ResponseWriter, r *http.Request) {
824+
io.WriteString(w, r.RequestURI)
825+
}
826+
827+
func loadLARS(routes []route) http.Handler {
828+
var h interface{} = echoHandler
829+
if loadTestHandler {
830+
h = larsHandlerTest
831+
}
832+
833+
l := lars.New()
834+
835+
for _, r := range routes {
836+
switch r.method {
837+
case "GET":
838+
l.Get(r.path, h)
839+
case "POST":
840+
l.Post(r.path, h)
841+
case "PUT":
842+
l.Put(r.path, h)
843+
case "PATCH":
844+
l.Patch(r.path, h)
845+
case "DELETE":
846+
l.Delete(r.path, h)
847+
default:
848+
panic("Unknow HTTP method: " + r.method)
849+
}
850+
}
851+
return l
852+
}
853+
854+
func loadLARSSingle(method, path string, h interface{}) http.Handler {
855+
l := lars.New()
856+
857+
switch method {
858+
case "GET":
859+
l.Get(path, h)
860+
case "POST":
861+
l.Post(path, h)
862+
case "PUT":
863+
l.Put(path, h)
864+
case "PATCH":
865+
l.Patch(path, h)
866+
case "DELETE":
867+
l.Delete(path, h)
868+
default:
869+
panic("Unknow HTTP method: " + method)
870+
}
871+
return l
872+
}
873+
810874
// Macaron
811875
func macaronHandler() {}
812876

@@ -1070,13 +1134,13 @@ func initRevel() {
10701134

10711135
revel.RegisterController((*RevelController)(nil),
10721136
[]*revel.MethodType{
1073-
&revel.MethodType{
1137+
{
10741138
Name: "Handle",
10751139
},
1076-
&revel.MethodType{
1140+
{
10771141
Name: "HandleWrite",
10781142
},
1079-
&revel.MethodType{
1143+
{
10801144
Name: "HandleTest",
10811145
},
10821146
})

routers_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
{"HttpRouter", loadHttpRouter},
2828
{"HttpTreeMux", loadHttpTreeMux},
2929
//{"Kocha", loadKocha},
30+
{"LARS", loadLARS},
3031
{"Macaron", loadMacaron},
3132
{"Martini", loadMartini},
3233
{"Pat", loadPat},

static_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ var (
187187
staticHttpRouter http.Handler
188188
staticHttpTreeMux http.Handler
189189
staticKocha http.Handler
190+
staticLARS http.Handler
190191
staticMacaron http.Handler
191192
staticMartini http.Handler
192193
staticPat http.Handler
@@ -257,6 +258,9 @@ func init() {
257258
calcMem("Kocha", func() {
258259
staticKocha = loadKocha(staticRoutes)
259260
})
261+
calcMem("LARS", func() {
262+
staticLARS = loadLARS(staticRoutes)
263+
})
260264
calcMem("Macaron", func() {
261265
staticMacaron = loadMacaron(staticRoutes)
262266
})
@@ -347,6 +351,9 @@ func BenchmarkHttpTreeMux_StaticAll(b *testing.B) {
347351
func BenchmarkKocha_StaticAll(b *testing.B) {
348352
benchRoutes(b, staticKocha, staticRoutes)
349353
}
354+
func BenchmarkLARS_StaticAll(b *testing.B) {
355+
benchRoutes(b, staticLARS, staticRoutes)
356+
}
350357
func BenchmarkMacaron_StaticAll(b *testing.B) {
351358
benchRoutes(b, staticMacaron, staticRoutes)
352359
}

0 commit comments

Comments
 (0)