Skip to content

Commit

Permalink
Fix skewed middleware benchmarks (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Feb 9, 2024
1 parent 9297f15 commit eacc8e8
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,31 @@ const (
)

func BenchmarkWithout(b *testing.B) {
res := FakeResponse{http.Header{}}
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(res.header)
testHandler.ServeHTTP(res, req)
testHandler.ServeHTTP(resps[i], req)
}
}

func BenchmarkDefault(b *testing.B) {
res := FakeResponse{http.Header{}}
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
handler := Default().Handler(testHandler)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(res.header)
handler.ServeHTTP(res, req)
handler.ServeHTTP(resps[i], req)
}
}

func BenchmarkAllowedOrigin(b *testing.B) {
res := FakeResponse{http.Header{}}
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
c := New(Options{
Expand All @@ -66,13 +64,12 @@ func BenchmarkAllowedOrigin(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(res.header)
handler.ServeHTTP(res, req)
handler.ServeHTTP(resps[i], req)
}
}

func BenchmarkPreflight(b *testing.B) {
res := FakeResponse{http.Header{}}
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
Expand All @@ -81,13 +78,12 @@ func BenchmarkPreflight(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(res.header)
handler.ServeHTTP(res, req)
handler.ServeHTTP(resps[i], req)
}
}

func BenchmarkPreflightHeader(b *testing.B) {
res := FakeResponse{http.Header{}}
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
Expand All @@ -97,13 +93,14 @@ func BenchmarkPreflightHeader(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
clear(res.header)
handler.ServeHTTP(res, req)
handler.ServeHTTP(resps[i], req)
}
}

func clear(h http.Header) {
for k := range h {
delete(h, k)
func makeFakeResponses(n int) []*FakeResponse {
resps := make([]*FakeResponse, n)
for i := 0; i < n; i++ {
resps[i] = &FakeResponse{http.Header{}}
}
return resps
}

0 comments on commit eacc8e8

Please sign in to comment.