-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench.go
134 lines (105 loc) · 2.84 KB
/
bench.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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package routertest
import (
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"runtime/metrics"
"testing"
"github.com/issue9/mux/v8"
)
// Bench 执行所有的性能测试
//
// h 表示路由的处理函数,只要向终端输出 URL.Path 值即可,
// 以 T 的类型为 http.HandlerFunc 为例:
//
// func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte(r.URL.Path))
// }
func (t *Tester[T]) Bench(b *testing.B, h T) {
allocs := t.calcMemStats(h)
fmt.Printf("\n加载 %d 条路由总共占用 %d KB\n", len(apis), allocs/1024)
b.Run("URL", func(b *testing.B) {
t.benchURL(b, h)
})
b.Run("Add", func(b *testing.B) {
t.benchAddAndServeHTTP(b, h)
})
b.Run("Serve", func(b *testing.B) {
t.benchServeHTTP(b, h)
})
}
func (t *Tester[T]) benchURL(b *testing.B, h T) {
const domain = "https://github.com"
router := mux.NewRouterOf("test", t.c, t.notFound, t.m, t.o, mux.Lock(true), mux.URLDomain(domain))
for _, api := range apis {
router.Handle(api.pattern, h, api.method)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
api := apis[i%len(apis)]
url, err := router.URL(true, api.pattern, api.ps)
if err != nil {
b.Error(err)
}
if url != domain+api.test {
b.Errorf("URL 出错,位于 %s", api.pattern)
}
}
}
func (t *Tester[T]) benchAddAndServeHTTP(b *testing.B, h T) {
router := mux.NewRouterOf("test", t.c, t.notFound, t.m, t.o, mux.Lock(true))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
api := apis[i%len(apis)]
w := httptest.NewRecorder()
r, _ := http.NewRequest(api.method, api.test, nil)
router.Handle(api.pattern, h, api.method)
router.ServeHTTP(w, r)
router.Remove(api.pattern, api.method)
if w.Body.String() != r.URL.Path {
b.Errorf("%s:%s", w.Body.String(), r.URL.Path)
}
}
}
func (t *Tester[T]) benchServeHTTP(b *testing.B, h T) {
router := mux.NewRouterOf("test", t.c, t.notFound, t.m, t.o)
for _, api := range apis {
router.Handle(api.pattern, h, api.method)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
api := apis[i%len(apis)]
w := httptest.NewRecorder()
r, _ := http.NewRequest(api.method, api.test, nil)
router.ServeHTTP(w, r)
if w.Body.String() != r.URL.Path {
b.Errorf("%s:%s", w.Body.String(), r.URL.Path)
}
}
}
func (t *Tester[T]) calcMemStats(h T) uint64 {
return calcMemStats(func() {
r := mux.NewRouterOf("test", t.c, t.notFound, t.m, t.o, mux.Lock(true))
for _, api := range apis {
r.Handle(api.pattern, h, api.method)
}
})
}
func calcMemStats(load func()) uint64 {
sample := make([]metrics.Sample, 1)
sample[0].Name = "/gc/heap/allocs:bytes"
runtime.GC()
metrics.Read(sample)
before := sample[0].Value.Uint64()
load()
metrics.Read(sample)
after := sample[0].Value.Uint64()
return after - before
}