forked from guregu/kami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
107 lines (97 loc) · 2.63 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
package kami_test
import (
"net/http"
"net/http/httptest"
"testing"
"golang.org/x/net/context"
"github.com/guregu/kami"
)
func BenchmarkStaticRoute(b *testing.B) {
kami.Reset()
kami.Get("/hello", noop)
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hello", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
panic(resp.Code)
}
}
}
// Param benchmarks test accessing URL params
func BenchmarkParameter(b *testing.B) {
kami.Reset()
kami.Get("/hello/:name", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
kami.Param(ctx, "name")
})
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hello/bob", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
panic(resp.Code)
}
}
}
func BenchmarkParameter5(b *testing.B) {
kami.Reset()
kami.Get("/:a/:b/:c/:d/:e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, v := range []string{"a", "b", "c", "d", "e"} {
kami.Param(ctx, v)
}
})
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/a/b/c/d/e", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
panic(resp.Code)
}
}
}
// Middleware tests setting and using values with middleware
func BenchmarkMiddleware(b *testing.B) {
kami.Reset()
kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test", "ok")
})
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if ctx.Value("test") != "ok" {
w.WriteHeader(http.StatusServiceUnavailable)
}
})
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/test", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
panic(resp.Code)
}
}
}
func BenchmarkMiddleware5(b *testing.B) {
kami.Reset()
numbers := []int{1, 2, 3, 4, 5}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, n := range numbers {
if ctx.Value(n) != n {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
})
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/test", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
panic(resp.Code)
}
}
}