forked from uptrace/bunrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
96 lines (67 loc) · 1.92 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
package bunrouter
import (
"net/http"
"testing"
)
func BenchmarkRouterSimple(b *testing.B) {
router := New()
router.GET("/", simpleHandler)
router.GET("/user/dimfeld", simpleHandler)
req, _ := http.NewRequest("GET", "/user/dimfeld", nil)
benchRequest(b, router, req)
}
func BenchmarkRouterRoot(b *testing.B) {
router := New()
router.GET("/", simpleHandler)
router.GET("/user/dimfeld", simpleHandler)
req, _ := http.NewRequest("GET", "/", nil)
benchRequest(b, router, req)
}
func BenchmarkRouterParam(b *testing.B) {
router := New()
router.GET("/", simpleHandler)
router.GET("/user/:name", simpleHandler)
req, _ := http.NewRequest("GET", "/user/dimfeld", nil)
benchRequest(b, router, req)
}
func BenchmarkRouterLongParams(b *testing.B) {
router := New()
router.GET("/", simpleHandler)
router.GET("/user/:name/:resource", simpleHandler)
req, _ := http.NewRequest("GET", "/user/aaaabbbbccccddddeeeeffff/asdfghjkl", nil)
benchRequest(b, router, req)
}
func BenchmarkRouterFiveColon(b *testing.B) {
router := New()
router.GET("/", simpleHandler)
router.GET("/:a/:b/:c/:d/:e", simpleHandler)
req, _ := http.NewRequest("GET", "/test/test/test/test/test", nil)
benchRequest(b, router, req)
}
// This type and the benchRequest function are modified from go-http-routing-benchmark.
type mockResponseWriter struct {
code int
calledWrite bool
}
func (m *mockResponseWriter) Header() (h http.Header) {
return http.Header{}
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
m.calledWrite = true
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
m.calledWrite = true
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(code int) {
m.code = code
}
func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
w := new(mockResponseWriter)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, r)
}
}