-
Notifications
You must be signed in to change notification settings - Fork 24
/
router_bench_test.go
178 lines (150 loc) · 4.44 KB
/
router_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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package denco_test
import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
"testing"
"github.com/naoina/denco"
)
func BenchmarkRouterLookupStatic100(b *testing.B) {
benchmarkRouterLookupStatic(b, 100)
}
func BenchmarkRouterLookupStatic300(b *testing.B) {
benchmarkRouterLookupStatic(b, 300)
}
func BenchmarkRouterLookupStatic700(b *testing.B) {
benchmarkRouterLookupStatic(b, 700)
}
func BenchmarkRouterLookupSingleParam100(b *testing.B) {
records := makeTestSingleParamRecords(100)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterLookupSingleParam300(b *testing.B) {
records := makeTestSingleParamRecords(300)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterLookupSingleParam700(b *testing.B) {
records := makeTestSingleParamRecords(700)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterLookupSingle2Param100(b *testing.B) {
records := makeTestSingle2ParamRecords(100)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterLookupSingle2Param300(b *testing.B) {
records := makeTestSingle2ParamRecords(300)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterLookupSingle2Param700(b *testing.B) {
records := makeTestSingle2ParamRecords(700)
benchmarkRouterLookupSingleParam(b, records)
}
func BenchmarkRouterBuildStatic100(b *testing.B) {
records := makeTestStaticRecords(100)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildStatic300(b *testing.B) {
records := makeTestStaticRecords(300)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildStatic700(b *testing.B) {
records := makeTestStaticRecords(700)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingleParam100(b *testing.B) {
records := makeTestSingleParamRecords(100)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingleParam300(b *testing.B) {
records := makeTestSingleParamRecords(300)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingleParam700(b *testing.B) {
records := makeTestSingleParamRecords(700)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingle2Param100(b *testing.B) {
records := makeTestSingle2ParamRecords(100)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingle2Param300(b *testing.B) {
records := makeTestSingle2ParamRecords(300)
benchmarkRouterBuild(b, records)
}
func BenchmarkRouterBuildSingle2Param700(b *testing.B) {
records := makeTestSingle2ParamRecords(700)
benchmarkRouterBuild(b, records)
}
func benchmarkRouterLookupStatic(b *testing.B, n int) {
b.StopTimer()
router := denco.New()
records := makeTestStaticRecords(n)
if err := router.Build(records); err != nil {
b.Fatal(err)
}
record := pickTestRecord(records)
b.StartTimer()
for i := 0; i < b.N; i++ {
if r, _, _ := router.Lookup(record.Key); r != record.Value {
b.Fail()
}
}
}
func benchmarkRouterLookupSingleParam(b *testing.B, records []denco.Record) {
router := denco.New()
if err := router.Build(records); err != nil {
b.Fatal(err)
}
record := pickTestRecord(records)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, _, found := router.Lookup(record.Key); !found {
b.Fail()
}
}
}
func benchmarkRouterBuild(b *testing.B, records []denco.Record) {
for i := 0; i < b.N; i++ {
router := denco.New()
if err := router.Build(records); err != nil {
b.Fatal(err)
}
}
}
func makeTestStaticRecords(n int) []denco.Record {
records := make([]denco.Record, n)
for i := 0; i < n; i++ {
records[i] = denco.NewRecord("/"+randomString(50), fmt.Sprintf("testroute%d", i))
}
return records
}
func makeTestSingleParamRecords(n int) []denco.Record {
records := make([]denco.Record, n)
for i := 0; i < len(records); i++ {
records[i] = denco.NewRecord(fmt.Sprintf("/user%d/:name", i), fmt.Sprintf("testroute%d", i))
}
return records
}
func makeTestSingle2ParamRecords(n int) []denco.Record {
records := make([]denco.Record, n)
for i := 0; i < len(records); i++ {
records[i] = denco.NewRecord(fmt.Sprintf("/user%d/:name/comment/:id", i), fmt.Sprintf("testroute%d", i))
}
return records
}
func pickTestRecord(records []denco.Record) denco.Record {
return records[len(records)/2]
}
func randomString(n int) string {
const srcStrings = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/"
var buf bytes.Buffer
for i := 0; i < n; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(srcStrings)-1)))
if err != nil {
panic(err)
}
buf.WriteByte(srcStrings[num.Int64()])
}
return buf.String()
}