forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyvalue_bench_test.go
201 lines (175 loc) · 3.68 KB
/
keyvalue_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package log_test
import (
"testing"
"go.opentelemetry.io/otel/log"
)
// Store results in a file scope var to ensure compiler does not optimize the
// test away.
var (
outV log.Value
outKV log.KeyValue
outBool bool
outFloat64 float64
outInt64 int64
outMap []log.KeyValue
outSlice []log.Value
outStr string
)
func BenchmarkBool(b *testing.B) {
const k, v = "bool", true
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.BoolValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Bool(k, v)
}
})
kv := log.Bool(k, v)
b.Run("AsBool", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outBool = kv.Value.AsBool()
}
})
}
func BenchmarkFloat64(b *testing.B) {
const k, v = "float64", 3.0
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.Float64Value(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Float64(k, v)
}
})
kv := log.Float64(k, v)
b.Run("AsFloat64", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outFloat64 = kv.Value.AsFloat64()
}
})
}
func BenchmarkInt(b *testing.B) {
const k, v = "int", 32
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.IntValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Int(k, v)
}
})
kv := log.Int(k, v)
b.Run("AsInt64", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outInt64 = kv.Value.AsInt64()
}
})
}
func BenchmarkInt64(b *testing.B) {
const k, v = "int64", int64(32)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.Int64Value(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Int64(k, v)
}
})
kv := log.Int64(k, v)
b.Run("AsInt64", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outInt64 = kv.Value.AsInt64()
}
})
}
func BenchmarkMap(b *testing.B) {
const k = "map"
v := []log.KeyValue{log.Bool("b", true), log.Int("i", 1)}
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.MapValue(v...)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Map(k, v...)
}
})
kv := log.Map(k, v...)
b.Run("AsMap", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outMap = kv.Value.AsMap()
}
})
}
func BenchmarkSlice(b *testing.B) {
const k = "slice"
v := []log.Value{log.BoolValue(true), log.IntValue(1)}
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.SliceValue(v...)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.Slice(k, v...)
}
})
kv := log.Slice(k, v...)
b.Run("AsSlice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outSlice = kv.Value.AsSlice()
}
})
}
func BenchmarkString(b *testing.B) {
const k, v = "str", "value"
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = log.StringValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = log.String(k, v)
}
})
kv := log.String(k, v)
b.Run("AsString", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outStr = kv.Value.AsString()
}
})
}