Debian Codegraph @

benchmark/benchmark_test.go

raw
1 /*
2 Copyright 2021 The logr Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package logr
18
19 import (
20 "fmt"
21 "testing"
22
23 "github.com/go-logr/logr"
24 "github.com/go-logr/logr/funcr"
25 )
26
27 //go:noinline
28 func doInfoOneArg(b *testing.B, log logr.Logger) {
29 for i := 0; i < b.N; i++ {
30 log.Info("this is", "a", "string")
31 }
32 }
33
34 //go:noinline
35 func doInfoSeveralArgs(b *testing.B, log logr.Logger) {
36 for i := 0; i < b.N; i++ {
37 log.Info("multi",
38 "bool", true, "string", "str", "int", 42,
39 "float", 3.14, "struct", struct{ X, Y int }{93, 76})
40 }
41 }
42
43 //go:noinline
44 func doInfoPointerArgs(b *testing.B, log logr.Logger) {
45 vBool := true
46 vString := "string"
47 vInt := 42
48 vFloat := 3.14
49 vStruct := struct {
50 X *int
51 Y *string
52 }{&vInt, &vString}
53 for i := 0; i < b.N; i++ {
54 log.Info("multi",
55 "bool", &vBool, "string", &vString, "int", &vInt,
56 "float", &vFloat, "struct", &vStruct)
57 }
58 }
59
60 //go:noinline
61 func doInfoWithValues(b *testing.B, log logr.Logger) {
62 log = log.WithValues("k1", "str", "k2", 222, "k3", true, "k4", 1.0)
63 for i := 0; i < b.N; i++ {
64 log.Info("multi",
65 "bool", true, "string", "str", "int", 42,
66 "float", 3.14, "struct", struct{ X, Y int }{93, 76})
67 }
68 }
69
70 //go:noinline
71 func doV0Info(b *testing.B, log logr.Logger) {
72 for i := 0; i < b.N; i++ {
73 log.V(0).Info("multi",
74 "bool", true, "string", "str", "int", 42,
75 "float", 3.14, "struct", struct{ X, Y int }{93, 76})
76 }
77 }
78
79 //go:noinline
80 func doV9Info(b *testing.B, log logr.Logger) {
81 for i := 0; i < b.N; i++ {
82 log.V(9).Info("multi",
83 "bool", true, "string", "str", "int", 42,
84 "float", 3.14, "struct", struct{ X, Y int }{93, 76})
85 }
86 }
87
88 //go:noinline
89 func doError(b *testing.B, log logr.Logger) {
90 err := fmt.Errorf("error message")
91 for i := 0; i < b.N; i++ {
92 log.Error(err, "multi",
93 "bool", true, "string", "str", "int", 42,
94 "float", 3.14, "struct", struct{ X, Y int }{93, 76})
95 }
96 }
97
98 //go:noinline
99 func doWithValues(b *testing.B, log logr.Logger) {
100 for i := 0; i < b.N; i++ {
101 l := log.WithValues("k1", "v1", "k2", "v2")
102 _ = l
103 }
104 }
105
106 //go:noinline
107 func doWithName(b *testing.B, log logr.Logger) {
108 for i := 0; i < b.N; i++ {
109 l := log.WithName("name")
110 _ = l
111 }
112 }
113
114 //go:noinline
115 func doWithCallDepth(b *testing.B, log logr.Logger) {
116 for i := 0; i < b.N; i++ {
117 l := log.WithCallDepth(1)
118 _ = l
119 }
120 }
121
122 type Tstringer struct{ s string }
123
124 func (t Tstringer) String() string {
125 return t.s
126 }
127
128 //go:noinline
129 func doStringerValue(b *testing.B, log logr.Logger) {
130 for i := 0; i < b.N; i++ {
131 log.Info("this is", "a", Tstringer{"stringer"})
132 }
133 }
134
135 type Terror struct{ s string }
136
137 func (t Terror) Error() string {
138 return t.s
139 }
140
141 //go:noinline
142 func doErrorValue(b *testing.B, log logr.Logger) {
143 for i := 0; i < b.N; i++ {
144 log.Info("this is", "an", Terror{"error"})
145 }
146 }
147
148 type Tmarshaler struct{ s string }
149
150 func (t Tmarshaler) MarshalLog() any {
151 return t.s
152 }
153
154 //go:noinline
155 func doMarshalerValue(b *testing.B, log logr.Logger) {
156 for i := 0; i < b.N; i++ {
157 log.Info("this is", "a", Tmarshaler{"marshaler"})
158 }
159 }
160
161 //
162 // discard
163 //
164
165 func BenchmarkDiscardLogInfoOneArg(b *testing.B) {
166 var log logr.Logger = logr.Discard() //nolint:staticcheck
167 doInfoOneArg(b, log)
168 }
169
170 func BenchmarkDiscardLogInfoSeveralArgs(b *testing.B) {
171 var log logr.Logger = logr.Discard() //nolint:staticcheck
172 doInfoSeveralArgs(b, log)
173 }
174
175 func BenchmarkDiscardLogInfoPointerArgs(b *testing.B) {
176 var log logr.Logger = logr.Discard() //nolint:staticcheck
177 doInfoPointerArgs(b, log)
178 }
179
180 func BenchmarkDiscardLogInfoWithValues(b *testing.B) {
181 var log logr.Logger = logr.Discard() //nolint:staticcheck
182 doInfoWithValues(b, log)
183 }
184
185 func BenchmarkDiscardLogV0Info(b *testing.B) {
186 var log logr.Logger = logr.Discard() //nolint:staticcheck
187 doV0Info(b, log)
188 }
189
190 func BenchmarkDiscardLogV9Info(b *testing.B) {
191 var log logr.Logger = logr.Discard() //nolint:staticcheck
192 doV9Info(b, log)
193 }
194
195 func BenchmarkDiscardLogError(b *testing.B) {
196 var log logr.Logger = logr.Discard() //nolint:staticcheck
197 doError(b, log)
198 }
199
200 func BenchmarkDiscardWithValues(b *testing.B) {
201 var log logr.Logger = logr.Discard() //nolint:staticcheck
202 doWithValues(b, log)
203 }
204
205 func BenchmarkDiscardWithName(b *testing.B) {
206 var log logr.Logger = logr.Discard() //nolint:staticcheck
207 doWithName(b, log)
208 }
209
210 //
211 // funcr
212 //
213
214 func noopKV(_, _ string) {}
215 func noopJSON(_ string) {}
216
217 func BenchmarkFuncrLogInfoOneArg(b *testing.B) {
218 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
219 doInfoOneArg(b, log)
220 }
221
222 func BenchmarkFuncrJSONLogInfoOneArg(b *testing.B) {
223 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
224 doInfoOneArg(b, log)
225 }
226
227 func BenchmarkFuncrLogInfoSeveralArgs(b *testing.B) {
228 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
229 doInfoSeveralArgs(b, log)
230 }
231
232 func BenchmarkFuncrLogInfoPointerArgs(b *testing.B) {
233 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
234 doInfoPointerArgs(b, log)
235 }
236
237 func BenchmarkFuncrJSONLogInfoSeveralArgs(b *testing.B) {
238 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
239 doInfoSeveralArgs(b, log)
240 }
241
242 func BenchmarkFuncrLogInfoWithValues(b *testing.B) {
243 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
244 doInfoWithValues(b, log)
245 }
246
247 func BenchmarkFuncrJSONLogInfoWithValues(b *testing.B) {
248 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
249 doInfoWithValues(b, log)
250 }
251
252 func BenchmarkFuncrLogV0Info(b *testing.B) {
253 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
254 doV0Info(b, log)
255 }
256
257 func BenchmarkFuncrJSONLogV0Info(b *testing.B) {
258 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
259 doV0Info(b, log)
260 }
261
262 func BenchmarkFuncrLogV9Info(b *testing.B) {
263 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
264 doV9Info(b, log)
265 }
266
267 func BenchmarkFuncrJSONLogV9Info(b *testing.B) {
268 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
269 doV9Info(b, log)
270 }
271
272 func BenchmarkFuncrLogError(b *testing.B) {
273 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
274 doError(b, log)
275 }
276
277 func BenchmarkFuncrJSONLogError(b *testing.B) {
278 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
279 doError(b, log)
280 }
281
282 func BenchmarkFuncrWithValues(b *testing.B) {
283 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
284 doWithValues(b, log)
285 }
286
287 func BenchmarkFuncrWithName(b *testing.B) {
288 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
289 doWithName(b, log)
290 }
291
292 func BenchmarkFuncrWithCallDepth(b *testing.B) {
293 var log logr.Logger = funcr.New(noopKV, funcr.Options{}) //nolint:staticcheck
294 doWithCallDepth(b, log)
295 }
296
297 func BenchmarkFuncrJSONLogInfoStringerValue(b *testing.B) {
298 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
299 doStringerValue(b, log)
300 }
301
302 func BenchmarkFuncrJSONLogInfoErrorValue(b *testing.B) {
303 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
304 doErrorValue(b, log)
305 }
306
307 func BenchmarkFuncrJSONLogInfoMarshalerValue(b *testing.B) {
308 var log logr.Logger = funcr.NewJSON(noopJSON, funcr.Options{}) //nolint:staticcheck
309 doMarshalerValue(b, log)
310 }