Debian Codegraph @

log/keyvalue_bench_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package log_test
5
6 import (
7 "testing"
8
9 "go.opentelemetry.io/otel/attribute"
10 "go.opentelemetry.io/otel/log"
11 )
12
13 // Store results in a file scope var to ensure compiler does not optimize the
14 // test away.
15 var (
16 outV log.Value
17 outKV log.KeyValue
18
19 outBool bool
20 outFloat64 float64
21 outInt64 int64
22 outMap []log.KeyValue
23 outSlice []log.Value
24 outStr string
25 )
26
27 func BenchmarkBool(b *testing.B) {
28 const k, v = "bool", true
29
30 b.Run("Value", func(b *testing.B) {
31 b.ReportAllocs()
32 for i := 0; i < b.N; i++ {
33 outV = log.BoolValue(v)
34 }
35 })
36 b.Run("KeyValue", func(b *testing.B) {
37 b.ReportAllocs()
38 for i := 0; i < b.N; i++ {
39 outKV = log.Bool(k, v)
40 }
41 })
42
43 kv := log.Bool(k, v)
44 b.Run("AsBool", func(b *testing.B) {
45 b.ReportAllocs()
46 for i := 0; i < b.N; i++ {
47 outBool = kv.Value.AsBool()
48 }
49 })
50 }
51
52 func BenchmarkFloat64(b *testing.B) {
53 const k, v = "float64", 3.0
54
55 b.Run("Value", func(b *testing.B) {
56 b.ReportAllocs()
57 for i := 0; i < b.N; i++ {
58 outV = log.Float64Value(v)
59 }
60 })
61 b.Run("KeyValue", func(b *testing.B) {
62 b.ReportAllocs()
63 for i := 0; i < b.N; i++ {
64 outKV = log.Float64(k, v)
65 }
66 })
67
68 kv := log.Float64(k, v)
69 b.Run("AsFloat64", func(b *testing.B) {
70 b.ReportAllocs()
71 for i := 0; i < b.N; i++ {
72 outFloat64 = kv.Value.AsFloat64()
73 }
74 })
75 }
76
77 func BenchmarkInt(b *testing.B) {
78 const k, v = "int", 32
79
80 b.Run("Value", func(b *testing.B) {
81 b.ReportAllocs()
82 for i := 0; i < b.N; i++ {
83 outV = log.IntValue(v)
84 }
85 })
86 b.Run("KeyValue", func(b *testing.B) {
87 b.ReportAllocs()
88 for i := 0; i < b.N; i++ {
89 outKV = log.Int(k, v)
90 }
91 })
92
93 kv := log.Int(k, v)
94 b.Run("AsInt64", func(b *testing.B) {
95 b.ReportAllocs()
96 for i := 0; i < b.N; i++ {
97 outInt64 = kv.Value.AsInt64()
98 }
99 })
100 }
101
102 func BenchmarkInt64(b *testing.B) {
103 const k, v = "int64", int64(32)
104
105 b.Run("Value", func(b *testing.B) {
106 b.ReportAllocs()
107 for i := 0; i < b.N; i++ {
108 outV = log.Int64Value(v)
109 }
110 })
111 b.Run("KeyValue", func(b *testing.B) {
112 b.ReportAllocs()
113 for i := 0; i < b.N; i++ {
114 outKV = log.Int64(k, v)
115 }
116 })
117
118 kv := log.Int64(k, v)
119 b.Run("AsInt64", func(b *testing.B) {
120 b.ReportAllocs()
121 for i := 0; i < b.N; i++ {
122 outInt64 = kv.Value.AsInt64()
123 }
124 })
125 }
126
127 func BenchmarkMap(b *testing.B) {
128 const k = "map"
129 v := []log.KeyValue{log.Bool("b", true), log.Int("i", 1)}
130
131 b.Run("Value", func(b *testing.B) {
132 b.ReportAllocs()
133 for i := 0; i < b.N; i++ {
134 outV = log.MapValue(v...)
135 }
136 })
137 b.Run("KeyValue", func(b *testing.B) {
138 b.ReportAllocs()
139 for i := 0; i < b.N; i++ {
140 outKV = log.Map(k, v...)
141 }
142 })
143
144 kv := log.Map(k, v...)
145 b.Run("AsMap", func(b *testing.B) {
146 b.ReportAllocs()
147 for i := 0; i < b.N; i++ {
148 outMap = kv.Value.AsMap()
149 }
150 })
151 }
152
153 func BenchmarkSlice(b *testing.B) {
154 const k = "slice"
155 v := []log.Value{log.BoolValue(true), log.IntValue(1)}
156
157 b.Run("Value", func(b *testing.B) {
158 b.ReportAllocs()
159 for i := 0; i < b.N; i++ {
160 outV = log.SliceValue(v...)
161 }
162 })
163 b.Run("KeyValue", func(b *testing.B) {
164 b.ReportAllocs()
165 for i := 0; i < b.N; i++ {
166 outKV = log.Slice(k, v...)
167 }
168 })
169
170 kv := log.Slice(k, v...)
171 b.Run("AsSlice", func(b *testing.B) {
172 b.ReportAllocs()
173 for i := 0; i < b.N; i++ {
174 outSlice = kv.Value.AsSlice()
175 }
176 })
177 }
178
179 func BenchmarkString(b *testing.B) {
180 const k, v = "str", "value"
181
182 b.Run("Value", func(b *testing.B) {
183 b.ReportAllocs()
184 for i := 0; i < b.N; i++ {
185 outV = log.StringValue(v)
186 }
187 })
188 b.Run("KeyValue", func(b *testing.B) {
189 b.ReportAllocs()
190 for i := 0; i < b.N; i++ {
191 outKV = log.String(k, v)
192 }
193 })
194
195 kv := log.String(k, v)
196 b.Run("AsString", func(b *testing.B) {
197 b.ReportAllocs()
198 for i := 0; i < b.N; i++ {
199 outStr = kv.Value.AsString()
200 }
201 })
202 }
203
204 func BenchmarkValueEqual(b *testing.B) {
205 vals := []log.Value{
206 {},
207 log.Int64Value(1),
208 log.Int64Value(2),
209 log.Float64Value(3.5),
210 log.Float64Value(3.7),
211 log.BoolValue(true),
212 log.BoolValue(false),
213 log.StringValue("hi"),
214 log.StringValue("bye"),
215 log.BytesValue([]byte{1, 3, 5}),
216 log.SliceValue(log.StringValue("foo")),
217 log.SliceValue(log.IntValue(3), log.StringValue("foo")),
218 log.MapValue(log.Bool("b", true), log.Int("i", 3)),
219 log.MapValue(
220 log.Slice("l", log.IntValue(3), log.StringValue("foo")),
221 log.Bytes("b", []byte{3, 5, 7}),
222 log.Empty("e"),
223 ),
224 }
225 for _, v1 := range vals {
226 for _, v2 := range vals {
227 b.Run(v1.String()+" with "+v2.String(), func(b *testing.B) {
228 b.ReportAllocs()
229 for i := 0; i < b.N; i++ {
230 _ = v1.Equal(v2)
231 }
232 })
233 }
234 }
235 }
236
237 func BenchmarkKeyValueFromAttribute(b *testing.B) {
238 testCases := []struct {
239 desc string
240 kv attribute.KeyValue
241 }{
242 {
243 desc: "Empty",
244 kv: attribute.KeyValue{},
245 },
246 {
247 desc: "Bool",
248 kv: attribute.Bool("k", true),
249 },
250 {
251 desc: "BoolSlice",
252 kv: attribute.BoolSlice("k", []bool{true, false}),
253 },
254 {
255 desc: "Int64",
256 kv: attribute.Int64("k", 13),
257 },
258 {
259 desc: "Int64Slice",
260 kv: attribute.Int64Slice("k", []int64{12, 34}),
261 },
262 {
263 desc: "Float64",
264 kv: attribute.Float64("k", 3.14),
265 },
266 {
267 desc: "Float64Slice",
268 kv: attribute.Float64Slice("k", []float64{3.14, 2.72}),
269 },
270 {
271 desc: "String",
272 kv: attribute.String("k", "foo"),
273 },
274 {
275 desc: "StringSlice",
276 kv: attribute.StringSlice("k", []string{"foo", "bar"}),
277 },
278 }
279 for _, tc := range testCases {
280 b.Run(tc.desc, func(b *testing.B) {
281 b.ReportAllocs()
282 for range b.N {
283 outKV = log.KeyValueFromAttribute(tc.kv)
284 }
285 })
286 }
287 }