-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbench_test.go
172 lines (148 loc) · 4.65 KB
/
bench_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package xmux
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/hyahm/xmux/helper"
)
func enter(w http.ResponseWriter, r *http.Request) bool {
fmt.Println(GetConnents())
return false
}
func BenchmarkOneRoute(B *testing.B) {
router := NewRouter()
router.Enter = enter
router.Exit = nil
router.Get("/ping", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkRecoveryMiddleware(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Get("/", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/")
}
func BenchmarkLoggerMiddleware(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Get("/", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/")
}
func BenchmarkManyHandlers(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Get("/ping", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark5Params(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Get("/param/{param1}/{params2}/{param3}/{param4}/{param5}", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
}
func BenchmarkOneRouteJSON(B *testing.B) {
router := NewRouter()
router.Exit = nil
data := struct {
Status string `json:"status"`
}{"ok"}
router.Get("/json", func(w http.ResponseWriter, r *http.Request) {
send, _ := json.Marshal(data)
// w.Write([]byte(`{"status": "ok}`))
w.Write(send)
})
runRequest(B, router, "GET", "/json")
}
// func BenchmarkOneRouteHTML(B *testing.B) {
// router := NewRouter()
// t := template.Must(template.NewRouter("index").Parse(`
// <html><body><h1>{{.}}</h1></body></html>`))
// router.SetHTMLTemplate(t)
// router.Get("/html", func(w http.ResponseWriter, r *http.Request) {
// c.HTML(http.StatusOK, "index", "hola")
// })
// runRequest(B, router, "GET", "/html")
// }
// func BenchmarkOneRouteSet(B *testing.B) {
// router := NewRouter()
// router.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
// c.Set("key", "value")
// })
// runRequest(B, router, "GET", "/ping")
// }
func BenchmarkOneRouteString(B *testing.B) {
router := NewRouter()
router.Get("/text", func(w http.ResponseWriter, r *http.Request) {
w.Write(helper.StringToBytes("this is a plain text"))
})
router.Exit = nil
runRequest(B, router, "GET", "/text")
}
func BenchmarkManyRoutesFist(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Any("/ping", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkManyRoutesLast(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Any("/ping", func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "OPTIONS", "/ping")
}
func Benchmark404(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Any("/something", func(w http.ResponseWriter, r *http.Request) {})
router.HandleNotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
// router.NoRoute(func(w http.ResponseWriter, r *http.Request) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark404Many(B *testing.B) {
router := NewRouter()
router.Exit = nil
router.Get("/", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/path/to/something", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/post/:id", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/view/:id", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/delete/:id", func(w http.ResponseWriter, r *http.Request) {})
router.Get("/user/:id/:mode", func(w http.ResponseWriter, r *http.Request) {})
router.HandleNotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
runRequest(B, router, "GET", "/viewfake")
}
type mockWriter struct {
headers http.Header
}
func NewRouterMockWriter() *mockWriter {
return &mockWriter{
http.Header{},
}
}
func (m *mockWriter) Header() (h http.Header) {
return m.headers
}
func (m *mockWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockWriter) WriteHeader(int) {}
func runRequest(B *testing.B, r *Router, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := NewRouterMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
r.ServeHTTP(w, req)
}
}