-
Notifications
You must be signed in to change notification settings - Fork 219
/
bench_test.go
88 lines (73 loc) · 1.96 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
package cors
import (
"net/http"
"testing"
)
type FakeResponse struct {
header http.Header
}
func (r FakeResponse) Header() http.Header {
return r.header
}
func (r FakeResponse) WriteHeader(n int) {
}
func (r FakeResponse) Write(b []byte) (n int, err error) {
return len(b), nil
}
func BenchmarkWithout(b *testing.B) {
res := FakeResponse{http.Header{}}
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testHandler.ServeHTTP(res, req)
}
}
func BenchmarkDefault(b *testing.B) {
res := FakeResponse{http.Header{}}
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
req.Header.Add("Origin", "somedomain.com")
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(res, req)
}
}
func BenchmarkAllowedOrigin(b *testing.B) {
res := FakeResponse{http.Header{}}
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
req.Header.Add("Origin", "somedomain.com")
c := New(Options{
AllowedOrigins: []string{"somedomain.com"},
})
handler := c.Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(res, req)
}
}
func BenchmarkPreflight(b *testing.B) {
res := FakeResponse{http.Header{}}
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
req.Header.Add("Access-Control-Request-Method", "GET")
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(res, req)
}
}
func BenchmarkPreflightHeader(b *testing.B) {
res := FakeResponse{http.Header{}}
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
req.Header.Add("Access-Control-Request-Method", "GET")
req.Header.Add("Access-Control-Request-Headers", "Accept")
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(res, req)
}
}