-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter_test.go
46 lines (35 loc) · 999 Bytes
/
router_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
package ninjarouter
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func Benchmark_GET_Simple(b *testing.B) {
router := New()
router.GET("/action", helloHandler)
rw, req := testRequest("GET", "/action")
for i := 0; i < b.N; i++ {
router.ServeHTTP(rw, req)
}
}
func Benchmark_GET_Extreme(b *testing.B) {
router := New()
router.GET("/action/:lots/:of/:vars", helloVarsHandler)
rw, req := testRequest("GET", "/action/:lots/:of/:vars")
for i := 0; i < b.N; i++ {
router.ServeHTTP(rw, req)
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello")
}
func helloVarsHandler(w http.ResponseWriter, r *http.Request) {
params := Vars(r)
fmt.Fprintf(w, "hello %s %s %s %s", params["action"], params["lots"], params["of"], params["vars"])
}
func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
request, _ := http.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
return recorder, request
}