forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathbench_test.go
More file actions
203 lines (185 loc) · 4.22 KB
/
bench_test.go
File metadata and controls
203 lines (185 loc) · 4.22 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package errors
import (
stderrors "errors"
"fmt"
"strings"
"testing"
)
func noErrors(at, depth int) error {
if at >= depth {
return stderrors.New("no error")
}
return noErrors(at+1, depth)
}
func yesErrors(at, depth int) error {
if at >= depth {
return New("ye error")
}
return yesErrors(at+1, depth)
}
// GlobalE is an exported global to store the result of benchmark results,
// preventing the compiler from optimising the benchmark functions away.
var GlobalE interface{}
func BenchmarkErrors(b *testing.B) {
type run struct {
stack int
std bool
}
runs := []run{
{10, false},
{10, true},
{100, false},
{100, true},
{1000, false},
{1000, true},
}
for _, r := range runs {
part := "pkg/errors"
if r.std {
part = "errors"
}
name := fmt.Sprintf("%s-stack-%d", part, r.stack)
b.Run(name, func(b *testing.B) {
var err error
f := yesErrors
if r.std {
f = noErrors
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err = f(0, r.stack)
}
b.StopTimer()
GlobalE = err
})
}
}
func BenchmarkStackFormatting(b *testing.B) {
type run struct {
stack int
format string
}
runs := []run{
{10, "%s"},
{10, "%v"},
{10, "%+v"},
{30, "%s"},
{30, "%v"},
{30, "%+v"},
{60, "%s"},
{60, "%v"},
{60, "%+v"},
}
var stackStr string
for _, r := range runs {
name := fmt.Sprintf("%s-stack-%d", r.format, r.stack)
b.Run(name, func(b *testing.B) {
err := yesErrors(0, r.stack)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stackStr = fmt.Sprintf(r.format, err)
}
b.StopTimer()
})
}
for _, r := range runs {
name := fmt.Sprintf("%s-stacktrace-%d", r.format, r.stack)
b.Run(name, func(b *testing.B) {
err := yesErrors(0, r.stack)
st := err.(*fundamental).stack.StackTrace()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stackStr = fmt.Sprintf(r.format, st)
}
b.StopTimer()
})
}
GlobalE = stackStr
}
type argsProfile struct {
name string
containsString bool
build func(count, stringLen int) []interface{}
}
type benchmarkHackedStr string
func (s benchmarkHackedStr) FreezeStr() string {
return string(append([]byte(nil), s...))
}
func buildHackedStringArgs(count, stringLen int) []interface{} {
arg := benchmarkHackedStr(strings.Repeat("x", stringLen))
args := make([]interface{}, count)
for i := range args {
args[i] = arg
}
return args
}
func buildPlainStringArgs(count, stringLen int) []interface{} {
arg := strings.Repeat("x", stringLen)
args := make([]interface{}, count)
for i := range args {
args[i] = arg
}
return args
}
func buildIntArgs(count, _ int) []interface{} {
args := make([]interface{}, count)
for i := range args {
args[i] = i
}
return args
}
func BenchmarkByArgsHackedStrFreeze(b *testing.B) {
errPrototype := Normalize("bench", RFCCodeText("Internal:Bench"))
apiCases := []struct {
name string
call func(errPrototype *Error, args []interface{}) error
}{
{
name: "FastGenByArgs",
call: func(errPrototype *Error, args []interface{}) error {
return errPrototype.FastGenByArgs(args...)
},
},
}
profiles := []argsProfile{
{name: "plain", containsString: true, build: buildPlainStringArgs},
{name: "hacked", containsString: true, build: buildHackedStringArgs},
}
argCounts := []int{1, 4, 8}
stringLens := []int{16, 1024}
for _, apiCase := range apiCases {
apiCase := apiCase
b.Run(apiCase.name, func(b *testing.B) {
for _, profile := range profiles {
profile := profile
lens := []int{0}
if profile.containsString {
lens = stringLens
}
for _, argCount := range argCounts {
argCount := argCount
for _, strLen := range lens {
strLen := strLen
templateArgs := profile.build(argCount, strLen)
caseName := fmt.Sprintf("type-%s/count-%d", profile.name, argCount)
if profile.containsString {
caseName = fmt.Sprintf("%s/strlen-%d", caseName, strLen)
}
b.Run(caseName, func(b *testing.B) {
var err error
args := make([]interface{}, len(templateArgs))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
copy(args, templateArgs)
err = apiCase.call(errPrototype, args)
}
GlobalE = err
})
}
}
}
})
}
}