forked from dbubel/intake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
53 lines (46 loc) · 1.35 KB
/
Copy pathbenchmark_test.go
File metadata and controls
53 lines (46 loc) · 1.35 KB
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
package intake
import (
"net/http"
"net/http/httptest"
"testing"
)
func BenchmarkServeHTTP(b *testing.B) {
app := New()
app.AddGlobalMiddleware(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
next(w, r)
}
})
app.AddEndpoint(http.MethodGet, "/bench", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/bench", nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
rr := httptest.NewRecorder()
app.Mux.ServeHTTP(rr, req)
}
}
func BenchmarkCORSPreflight(b *testing.B) {
app := New()
app.AddGlobalMiddleware(CORS(CORSConfig{
AllowedOrigins: []string{"https://example.com"},
AllowedMethods: []string{http.MethodGet, http.MethodOptions},
AllowedHeaders: []string{"X-Token", "Content-Type"},
MaxAge: 600,
}))
app.AddEndpoint(http.MethodGet, "/bench", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest(http.MethodOptions, "/bench", nil)
req.Header.Set("Origin", "https://example.com")
req.Header.Set("Access-Control-Request-Method", http.MethodGet)
req.Header.Set("Access-Control-Request-Headers", "X-Token")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
rr := httptest.NewRecorder()
app.Mux.ServeHTTP(rr, req)
}
}