-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_pathparams_test.go
68 lines (52 loc) · 1.44 KB
/
bench_pathparams_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
package router
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func BenchmarkWithPathParamOneRequest(b *testing.B) {
rtr := New()
rtr.Add("/repos/:owner/:repo/pulls/:number/merge", http.MethodGet, func(w http.ResponseWriter, r *http.Request, params PathParams) {
w.WriteHeader(http.StatusOK)
})
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/repos/shyamz-22/oidc/pulls/44/merge", nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
rtr.ServeHTTP(w, r)
}
}
func BenchmarkWithPathParamTenRequests(b *testing.B) {
rtr := New()
for j := 0; j < 9; j++ {
path := fmt.Sprintf("/ping/:id/%d/pongs", j)
rtr.Add(path, http.MethodGet, func(w http.ResponseWriter, r *http.Request, params PathParams) {
w.WriteHeader(http.StatusOK)
})
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/ping/ball/1/pongs", nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rtr.ServeHTTP(w, r)
}
}
func BenchmarkWithPathParamTwentyRequests(b *testing.B) {
rtr := New()
for j := 0; j < 9; j++ {
path := fmt.Sprintf("/ping/:id/%d/pongs", j)
rtr.Add(path, http.MethodGet, func(w http.ResponseWriter, r *http.Request, params PathParams) {
w.WriteHeader(http.StatusOK)
})
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/ping/ball/8/pongs", nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rtr.ServeHTTP(w, r)
}
}