-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
bench_test.go
45 lines (38 loc) · 1.12 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
// go test -run=^$ -bench=BenchmarkRouterStatic
package violetear
import (
"net/http"
"net/http/httptest"
"testing"
)
func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
w := httptest.NewRecorder()
u := r.URL
rq := u.RawQuery
r.RequestURI = u.RequestURI()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.RawQuery = rq
router.ServeHTTP(w, r)
}
}
func BenchmarkRouterStatic(b *testing.B) {
router := New()
router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {}, "GET,HEAD")
r, _ := http.NewRequest("GET", "/hello", nil)
benchRequest(b, router, r)
}
func BenchmarkRouterDynamic(b *testing.B) {
router := New()
router.AddRegex(":word", `^\w+$`)
router.HandleFunc("/test/:word", func(w http.ResponseWriter, r *http.Request) {}, "GET,HEAD")
r, _ := http.NewRequest("GET", "/test/foo", nil)
benchRequest(b, router, r)
}
func BenchmarkRouterStaticWithName(b *testing.B) {
router := New()
router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {}, "GET,HEAD").Name("hello")
r, _ := http.NewRequest("GET", "/hello", nil)
benchRequest(b, router, r)
}