-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjantar_test.go
185 lines (147 loc) · 4.03 KB
/
jantar_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package jantar
import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
// modified version of https://github.com/cypriss/golang-mux-benchmark
// Returns a routeset with N *resources per namespace*. so N=1 gives about 15 routes
func resourceSetup(N int) (namespaces []string, resources []string, requests []*http.Request) {
namespaces = []string{"admin", "api", "site"}
resources = []string{}
uniqueID := make([]byte, 32)
rand.Read(uniqueID)
cookie := &http.Cookie{Name: "JANTAR_ID", Value: hex.EncodeToString(uniqueID)}
for i := 0; i < N; i++ {
sha1 := sha1.New()
io.WriteString(sha1, fmt.Sprintf("%d", i))
strResource := fmt.Sprintf("%x", sha1.Sum(nil))
resources = append(resources, strResource)
}
for _, ns := range namespaces {
for _, res := range resources {
req, _ := http.NewRequest("GET", "/"+ns+"/"+res, nil)
req.AddCookie(cookie)
requests = append(requests, req)
req, _ = http.NewRequest("POST", "/"+ns+"/"+res, nil)
req.AddCookie(cookie)
requests = append(requests, req)
req, _ = http.NewRequest("GET", "/"+ns+"/"+res+"/3937", nil)
req.AddCookie(cookie)
requests = append(requests, req)
req, _ = http.NewRequest("PUT", "/"+ns+"/"+res+"/3937", nil)
req.AddCookie(cookie)
requests = append(requests, req)
req, _ = http.NewRequest("DELETE", "/"+ns+"/"+res+"/3937", nil)
req.AddCookie(cookie)
requests = append(requests, req)
}
}
return
}
func routeFor(namespaces []string, resources []string, csrf bool) http.Handler {
j := setupServer(csrf)
for _, ns := range namespaces {
for _, res := range resources {
j.AddRoute("GET", "/"+ns+"/"+res, helloHandler)
j.AddRoute("POST", "/"+ns+"/"+res, helloHandler)
j.AddRoute("GET", "/"+ns+"/"+res+"/:id", helloHandler)
j.AddRoute("PUT", "/"+ns+"/"+res+"/:id", helloHandler)
j.AddRoute("DELETE", "/"+ns+"/"+res+"/:id", helloHandler)
}
}
return j
}
func benchmarkSimple(b *testing.B, csrf bool) {
j := setupServer(csrf)
j.AddRoute("GET", "/action", helloHandler)
rw, req := testRequest("GET", "/action")
uniqueID := make([]byte, 32)
rand.Read(uniqueID)
req.AddCookie(&http.Cookie{Name: "JANTAR_ID", Value: hex.EncodeToString(uniqueID)})
b.ResetTimer()
for i := 0; i < b.N; i++ {
j.ServeHTTP(rw, req)
}
}
func BenchmarkSimple(b *testing.B) {
benchmarkSimple(b, false)
}
func BenchmarkRoute15(b *testing.B) {
benchmarkRoutes(b, 1, false)
}
func BenchmarkRoute75(b *testing.B) {
benchmarkRoutes(b, 5, false)
}
func BenchmarkRoute150(b *testing.B) {
benchmarkRoutes(b, 10, false)
}
func BenchmarkRoute300(b *testing.B) {
benchmarkRoutes(b, 20, false)
}
func BenchmarkRoute3000(b *testing.B) {
benchmarkRoutes(b, 200, false)
}
func BenchmarkCsrfSimple(b *testing.B) {
benchmarkSimple(b, true)
}
func BenchmarkCsrfRoute15(b *testing.B) {
benchmarkRoutes(b, 1, true)
}
func BenchmarkCsrfRoute75(b *testing.B) {
benchmarkRoutes(b, 5, true)
}
func BenchmarkCsrfRoute150(b *testing.B) {
benchmarkRoutes(b, 10, true)
}
func BenchmarkCsrfRoute300(b *testing.B) {
benchmarkRoutes(b, 20, true)
}
func BenchmarkCsrfRoute3000(b *testing.B) {
benchmarkRoutes(b, 200, true)
}
//
// Helpers:
//
func helloHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "hello")
}
func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
request, _ := http.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
return recorder, request
}
func setupServer(csrf bool) *Jantar {
j := New(&Config{
Hostname: "localhost",
Port: 3000,
})
if !csrf {
j.middleware = nil
}
Log.SetMinLevel(LogLevelPanic)
return j
}
func benchmarkRoutes(b *testing.B, n int, csrf bool) {
namespaces, resources, requests := resourceSetup(n)
handler := routeFor(namespaces, resources, csrf)
recorder := httptest.NewRecorder()
reqID := 0
b.ResetTimer()
for i := 0; i < b.N*10; i++ {
if reqID >= len(requests) {
reqID = 0
}
req := requests[reqID]
handler.ServeHTTP(recorder, req)
if recorder.Code != 200 {
panic("wat")
}
reqID++
}
}