-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
81 lines (68 loc) · 1.69 KB
/
benchmarks_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
package rum
import (
"net/http"
"testing"
)
func BenchmarkOneRoute(B *testing.B) {
router := New(":9678")
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark5Params(B *testing.B) {
router := New(":9678")
router.Use(func(c *Context) {})
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
}
func BenchmarkOneRouteSet(B *testing.B) {
router := New(":9678")
router.GET("/ping", func(c *Context) {
c.Set("key", "value")
})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkOneRouteString(B *testing.B) {
router := New(":9678")
router.GET("/text", func(c *Context) {
c.String(http.StatusOK, "this is a plain text")
})
runRequest(B, router, "GET", "/text")
}
func BenchmarkManyHandlers(B *testing.B) {
router := New(":9678")
router.Use(func(c *Context) {})
router.Use(func(c *Context) {})
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
type mockWriter struct {
headers http.Header
}
func newMockWriter() *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 *Engine, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
r.ServeHTTP(w, req)
}
}