forked from yarf-framework/benchmarks
/
param_test.go
151 lines (118 loc) · 3.14 KB
/
param_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package benchmarks
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/bmizerany/pat"
"github.com/gin-gonic/gin"
"github.com/go-martini/martini"
"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
"github.com/yarf-framework/yarf"
"github.com/zenazn/goji/web"
)
// Yarf
type YarfParam struct {
yarf.Resource
}
func (y *YarfParam) Get(c *yarf.Context) error {
c.Render("Hello, " + c.Param("name"))
return nil
}
// HttpRouter
func HttpRouterParam(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Write([]byte("Hello, " + ps.ByName("name")))
}
// Goji
func GojiParam(c web.C, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, " + c.URLParams["name"]))
}
// Gorilla
func GorillaParam(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, " + mux.Vars(r)["name"]))
}
// Martini
func MartiniParam(params martini.Params) string {
return "Hello, " + params["name"]
}
// Gin
func GinParam(c *gin.Context) {
c.String(200, "Hello, "+c.Param("name"))
}
// Pat
func PatParam(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, " + req.URL.Query().Get(":name")))
}
// Benchmarks
func BenchmarkParamYarf(b *testing.B) {
y := yarf.New()
y.Add("/hello/:name", new(YarfParam))
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
y.ServeHTTP(res, req)
}
}
func BenchmarkParamHttpRouter(b *testing.B) {
router := httprouter.New()
router.GET("/hello/:name", HttpRouterParam)
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
router.ServeHTTP(res, req)
}
}
func BenchmarkParamGoji(b *testing.B) {
g := web.New()
g.Get("/hello/:name", GojiParam)
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
g.ServeHTTP(res, req)
}
}
func BenchmarkParamGorilla(b *testing.B) {
m := mux.NewRouter()
m.HandleFunc("/hello/{name}", GorillaParam)
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
m.ServeHTTP(res, req)
}
}
func BenchmarkParamMartini(b *testing.B) {
r := martini.NewRouter()
m := martini.New()
r.Get("/hello/:name", MartiniParam)
m.Action(r.Handle)
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
m.ServeHTTP(res, req)
}
}
func BenchmarkParamGin(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.GET("/hello/:name", GinParam)
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
r.ServeHTTP(res, req)
}
}
func BenchmarkParamPat(b *testing.B) {
m := pat.New()
m.Get("/", http.HandlerFunc(PatParam))
req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
res := httptest.NewRecorder()
// Run benchmark
for i := 0; i < b.N; i++ {
m.ServeHTTP(res, req)
}
}