Skip to content

Commit 6ea99af

Browse files
authored
log: Add benchmark tests (#4958)
1 parent 7b3382e commit 6ea99af

File tree

2 files changed

+375
-0
lines changed

2 files changed

+375
-0
lines changed

log/keyvalue_bench_test.go

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package log_test
16+
17+
import (
18+
"testing"
19+
20+
"go.opentelemetry.io/otel/log"
21+
)
22+
23+
// Store results in a file scope var to ensure compiler does not optimize the
24+
// test away.
25+
var (
26+
outV log.Value
27+
outKV log.KeyValue
28+
29+
outAny any
30+
outBool bool
31+
outFloat64 float64
32+
outInt64 int64
33+
outMap []log.KeyValue
34+
outSlice []log.Value
35+
outStr string
36+
)
37+
38+
func BenchmarkBool(b *testing.B) {
39+
const k, v = "bool", true
40+
41+
b.Run("Value", func(b *testing.B) {
42+
b.ReportAllocs()
43+
for i := 0; i < b.N; i++ {
44+
outV = log.BoolValue(v)
45+
}
46+
})
47+
b.Run("KeyValue", func(b *testing.B) {
48+
b.ReportAllocs()
49+
for i := 0; i < b.N; i++ {
50+
outKV = log.Bool(k, v)
51+
}
52+
})
53+
54+
kv := log.Bool(k, v)
55+
b.Run("AsBool", func(b *testing.B) {
56+
b.ReportAllocs()
57+
for i := 0; i < b.N; i++ {
58+
outBool = kv.Value.AsBool()
59+
}
60+
})
61+
b.Run("AsAny", func(b *testing.B) {
62+
b.ReportAllocs()
63+
for i := 0; i < b.N; i++ {
64+
outAny = kv.Value.AsAny()
65+
}
66+
})
67+
}
68+
69+
func BenchmarkFloat64(b *testing.B) {
70+
const k, v = "float64", 3.0
71+
72+
b.Run("Value", func(b *testing.B) {
73+
b.ReportAllocs()
74+
for i := 0; i < b.N; i++ {
75+
outV = log.Float64Value(v)
76+
}
77+
})
78+
b.Run("KeyValue", func(b *testing.B) {
79+
b.ReportAllocs()
80+
for i := 0; i < b.N; i++ {
81+
outKV = log.Float64(k, v)
82+
}
83+
})
84+
85+
kv := log.Float64(k, v)
86+
b.Run("AsFloat64", func(b *testing.B) {
87+
b.ReportAllocs()
88+
for i := 0; i < b.N; i++ {
89+
outFloat64 = kv.Value.AsFloat64()
90+
}
91+
})
92+
b.Run("AsAny", func(b *testing.B) {
93+
b.ReportAllocs()
94+
for i := 0; i < b.N; i++ {
95+
outAny = kv.Value.AsAny()
96+
}
97+
})
98+
}
99+
100+
func BenchmarkInt(b *testing.B) {
101+
const k, v = "int", 32
102+
103+
b.Run("Value", func(b *testing.B) {
104+
b.ReportAllocs()
105+
for i := 0; i < b.N; i++ {
106+
outV = log.IntValue(v)
107+
}
108+
})
109+
b.Run("KeyValue", func(b *testing.B) {
110+
b.ReportAllocs()
111+
for i := 0; i < b.N; i++ {
112+
outKV = log.Int(k, v)
113+
}
114+
})
115+
116+
kv := log.Int(k, v)
117+
b.Run("AsInt64", func(b *testing.B) {
118+
b.ReportAllocs()
119+
for i := 0; i < b.N; i++ {
120+
outInt64 = kv.Value.AsInt64()
121+
}
122+
})
123+
b.Run("AsAny", func(b *testing.B) {
124+
b.ReportAllocs()
125+
for i := 0; i < b.N; i++ {
126+
outAny = kv.Value.AsAny()
127+
}
128+
})
129+
}
130+
131+
func BenchmarkInt64(b *testing.B) {
132+
const k, v = "int64", int64(32)
133+
134+
b.Run("Value", func(b *testing.B) {
135+
b.ReportAllocs()
136+
for i := 0; i < b.N; i++ {
137+
outV = log.Int64Value(v)
138+
}
139+
})
140+
b.Run("KeyValue", func(b *testing.B) {
141+
b.ReportAllocs()
142+
for i := 0; i < b.N; i++ {
143+
outKV = log.Int64(k, v)
144+
}
145+
})
146+
147+
kv := log.Int64(k, v)
148+
b.Run("AsInt64", func(b *testing.B) {
149+
b.ReportAllocs()
150+
for i := 0; i < b.N; i++ {
151+
outInt64 = kv.Value.AsInt64()
152+
}
153+
})
154+
b.Run("AsAny", func(b *testing.B) {
155+
b.ReportAllocs()
156+
for i := 0; i < b.N; i++ {
157+
outAny = kv.Value.AsAny()
158+
}
159+
})
160+
}
161+
162+
func BenchmarkMap(b *testing.B) {
163+
const k = "map"
164+
v := []log.KeyValue{log.Bool("b", true), log.Int("i", 1)}
165+
166+
b.Run("Value", func(b *testing.B) {
167+
b.ReportAllocs()
168+
for i := 0; i < b.N; i++ {
169+
outV = log.MapValue(v...)
170+
}
171+
})
172+
b.Run("KeyValue", func(b *testing.B) {
173+
b.ReportAllocs()
174+
for i := 0; i < b.N; i++ {
175+
outKV = log.Map(k, v...)
176+
}
177+
})
178+
179+
kv := log.Map(k, v...)
180+
b.Run("AsMap", func(b *testing.B) {
181+
b.ReportAllocs()
182+
for i := 0; i < b.N; i++ {
183+
outMap = kv.Value.AsMap()
184+
}
185+
})
186+
b.Run("AsAny", func(b *testing.B) {
187+
b.ReportAllocs()
188+
for i := 0; i < b.N; i++ {
189+
outAny = kv.Value.AsAny()
190+
}
191+
})
192+
}
193+
194+
func BenchmarkSlice(b *testing.B) {
195+
const k = "slice"
196+
v := []log.Value{log.BoolValue(true), log.IntValue(1)}
197+
198+
b.Run("Value", func(b *testing.B) {
199+
b.ReportAllocs()
200+
for i := 0; i < b.N; i++ {
201+
outV = log.SliceValue(v...)
202+
}
203+
})
204+
b.Run("KeyValue", func(b *testing.B) {
205+
b.ReportAllocs()
206+
for i := 0; i < b.N; i++ {
207+
outKV = log.Slice(k, v...)
208+
}
209+
})
210+
211+
kv := log.Slice(k, v...)
212+
b.Run("AsSlice", func(b *testing.B) {
213+
b.ReportAllocs()
214+
for i := 0; i < b.N; i++ {
215+
outSlice = kv.Value.AsSlice()
216+
}
217+
})
218+
b.Run("AsAny", func(b *testing.B) {
219+
b.ReportAllocs()
220+
for i := 0; i < b.N; i++ {
221+
outAny = kv.Value.AsAny()
222+
}
223+
})
224+
}
225+
226+
func BenchmarkString(b *testing.B) {
227+
const k, v = "str", "value"
228+
229+
b.Run("Value", func(b *testing.B) {
230+
b.ReportAllocs()
231+
for i := 0; i < b.N; i++ {
232+
outV = log.StringValue(v)
233+
}
234+
})
235+
b.Run("KeyValue", func(b *testing.B) {
236+
b.ReportAllocs()
237+
for i := 0; i < b.N; i++ {
238+
outKV = log.String(k, v)
239+
}
240+
})
241+
242+
kv := log.String(k, v)
243+
b.Run("AsString", func(b *testing.B) {
244+
b.ReportAllocs()
245+
for i := 0; i < b.N; i++ {
246+
outStr = kv.Value.AsString()
247+
}
248+
})
249+
b.Run("AsAny", func(b *testing.B) {
250+
b.ReportAllocs()
251+
for i := 0; i < b.N; i++ {
252+
outAny = kv.Value.AsAny()
253+
}
254+
})
255+
}

log/record_bench_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package log_test
16+
17+
import (
18+
"testing"
19+
"time"
20+
21+
"go.opentelemetry.io/otel/log"
22+
)
23+
24+
func BenchmarkRecord(b *testing.B) {
25+
var (
26+
tStamp time.Time
27+
sev log.Severity
28+
text string
29+
body log.Value
30+
attr log.KeyValue
31+
n int
32+
)
33+
34+
b.Run("Timestamp", func(b *testing.B) {
35+
b.ReportAllocs()
36+
for n := 0; n < b.N; n++ {
37+
var r log.Record
38+
r.SetTimestamp(y2k)
39+
tStamp = r.Timestamp()
40+
}
41+
})
42+
43+
b.Run("ObservedTimestamp", func(b *testing.B) {
44+
b.ReportAllocs()
45+
for n := 0; n < b.N; n++ {
46+
var r log.Record
47+
r.SetObservedTimestamp(y2k)
48+
tStamp = r.ObservedTimestamp()
49+
}
50+
})
51+
52+
b.Run("Severity", func(b *testing.B) {
53+
b.ReportAllocs()
54+
for n := 0; n < b.N; n++ {
55+
var r log.Record
56+
r.SetSeverity(log.SeverityDebug)
57+
sev = r.Severity()
58+
}
59+
})
60+
61+
b.Run("SeverityText", func(b *testing.B) {
62+
b.ReportAllocs()
63+
for n := 0; n < b.N; n++ {
64+
var r log.Record
65+
r.SetSeverityText("text")
66+
text = r.SeverityText()
67+
}
68+
})
69+
70+
bodyVal := log.BoolValue(true)
71+
b.Run("Body", func(b *testing.B) {
72+
b.ReportAllocs()
73+
for n := 0; n < b.N; n++ {
74+
var r log.Record
75+
r.SetBody(bodyVal)
76+
body = r.Body()
77+
}
78+
})
79+
80+
attrs10 := []log.KeyValue{
81+
log.Bool("b1", true),
82+
log.Int("i1", 324),
83+
log.Float64("f1", -230.213),
84+
log.String("s1", "value1"),
85+
log.Map("m1", log.Slice("slice1", log.BoolValue(true))),
86+
log.Bool("b2", false),
87+
log.Int("i2", 39847),
88+
log.Float64("f2", 0.382964329),
89+
log.String("s2", "value2"),
90+
log.Map("m2", log.Slice("slice2", log.BoolValue(false))),
91+
}
92+
attrs5 := attrs10[:5]
93+
walk := func(kv log.KeyValue) bool {
94+
attr = kv
95+
return true
96+
}
97+
b.Run("Attributes", func(b *testing.B) {
98+
b.Run("5", func(b *testing.B) {
99+
b.ReportAllocs()
100+
for i := 0; i < b.N; i++ {
101+
var r log.Record
102+
r.AddAttributes(attrs5...)
103+
n = r.AttributesLen()
104+
r.WalkAttributes(walk)
105+
}
106+
})
107+
b.Run("10", func(b *testing.B) {
108+
b.ReportAllocs()
109+
for i := 0; i < b.N; i++ {
110+
var r log.Record
111+
r.AddAttributes(attrs10...)
112+
n = r.AttributesLen()
113+
r.WalkAttributes(walk)
114+
}
115+
})
116+
})
117+
118+
// Convince the linter these values are used.
119+
_, _, _, _, _, _ = tStamp, sev, text, body, attr, n
120+
}

0 commit comments

Comments
 (0)