benchmark_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. package attribute_test
  15. import (
  16. "testing"
  17. "go.opentelemetry.io/otel/attribute"
  18. )
  19. // Store results in a file scope var to ensure compiler does not optimize the
  20. // test away.
  21. var (
  22. outV attribute.Value
  23. outKV attribute.KeyValue
  24. outBool bool
  25. outBoolSlice []bool
  26. outInt64 int64
  27. outInt64Slice []int64
  28. outFloat64 float64
  29. outFloat64Slice []float64
  30. outStr string
  31. outStrSlice []string
  32. )
  33. func benchmarkEmit(kv attribute.KeyValue) func(*testing.B) {
  34. return func(b *testing.B) {
  35. b.ReportAllocs()
  36. for i := 0; i < b.N; i++ {
  37. outStr = kv.Value.Emit()
  38. }
  39. }
  40. }
  41. func BenchmarkBool(b *testing.B) {
  42. k, v := "bool", true
  43. kv := attribute.Bool(k, v)
  44. b.Run("Value", func(b *testing.B) {
  45. b.ReportAllocs()
  46. for i := 0; i < b.N; i++ {
  47. outV = attribute.BoolValue(v)
  48. }
  49. })
  50. b.Run("KeyValue", func(b *testing.B) {
  51. b.ReportAllocs()
  52. for i := 0; i < b.N; i++ {
  53. outKV = attribute.Bool(k, v)
  54. }
  55. })
  56. b.Run("AsBool", func(b *testing.B) {
  57. b.ReportAllocs()
  58. for i := 0; i < b.N; i++ {
  59. outBool = kv.Value.AsBool()
  60. }
  61. })
  62. b.Run("Emit", benchmarkEmit(kv))
  63. }
  64. func BenchmarkBoolSlice(b *testing.B) {
  65. k, v := "bool slice", []bool{true, false, true}
  66. kv := attribute.BoolSlice(k, v)
  67. b.Run("Value", func(b *testing.B) {
  68. b.ReportAllocs()
  69. for i := 0; i < b.N; i++ {
  70. outV = attribute.BoolSliceValue(v)
  71. }
  72. })
  73. b.Run("KeyValue", func(b *testing.B) {
  74. b.ReportAllocs()
  75. for i := 0; i < b.N; i++ {
  76. outKV = attribute.BoolSlice(k, v)
  77. }
  78. })
  79. b.Run("AsBoolSlice", func(b *testing.B) {
  80. b.ReportAllocs()
  81. for i := 0; i < b.N; i++ {
  82. outBoolSlice = kv.Value.AsBoolSlice()
  83. }
  84. })
  85. b.Run("Emit", benchmarkEmit(kv))
  86. }
  87. func BenchmarkInt(b *testing.B) {
  88. k, v := "int", int(42)
  89. kv := attribute.Int(k, v)
  90. b.Run("Value", func(b *testing.B) {
  91. b.ReportAllocs()
  92. for i := 0; i < b.N; i++ {
  93. outV = attribute.IntValue(v)
  94. }
  95. })
  96. b.Run("KeyValue", func(b *testing.B) {
  97. b.ReportAllocs()
  98. for i := 0; i < b.N; i++ {
  99. outKV = attribute.Int(k, v)
  100. }
  101. })
  102. b.Run("Emit", benchmarkEmit(kv))
  103. }
  104. func BenchmarkIntSlice(b *testing.B) {
  105. k, v := "int slice", []int{42, -3, 12}
  106. kv := attribute.IntSlice(k, v)
  107. b.Run("Value", func(b *testing.B) {
  108. b.ReportAllocs()
  109. for i := 0; i < b.N; i++ {
  110. outV = attribute.IntSliceValue(v)
  111. }
  112. })
  113. b.Run("KeyValue", func(b *testing.B) {
  114. b.ReportAllocs()
  115. for i := 0; i < b.N; i++ {
  116. outKV = attribute.IntSlice(k, v)
  117. }
  118. })
  119. b.Run("Emit", benchmarkEmit(kv))
  120. }
  121. func BenchmarkInt64(b *testing.B) {
  122. k, v := "int64", int64(42)
  123. kv := attribute.Int64(k, v)
  124. b.Run("Value", func(b *testing.B) {
  125. b.ReportAllocs()
  126. for i := 0; i < b.N; i++ {
  127. outV = attribute.Int64Value(v)
  128. }
  129. })
  130. b.Run("KeyValue", func(b *testing.B) {
  131. b.ReportAllocs()
  132. for i := 0; i < b.N; i++ {
  133. outKV = attribute.Int64(k, v)
  134. }
  135. })
  136. b.Run("AsInt64", func(b *testing.B) {
  137. b.ReportAllocs()
  138. for i := 0; i < b.N; i++ {
  139. outInt64 = kv.Value.AsInt64()
  140. }
  141. })
  142. b.Run("Emit", benchmarkEmit(kv))
  143. }
  144. func BenchmarkInt64Slice(b *testing.B) {
  145. k, v := "int64 slice", []int64{42, -3, 12}
  146. kv := attribute.Int64Slice(k, v)
  147. b.Run("Value", func(b *testing.B) {
  148. b.ReportAllocs()
  149. for i := 0; i < b.N; i++ {
  150. outV = attribute.Int64SliceValue(v)
  151. }
  152. })
  153. b.Run("KeyValue", func(b *testing.B) {
  154. b.ReportAllocs()
  155. for i := 0; i < b.N; i++ {
  156. outKV = attribute.Int64Slice(k, v)
  157. }
  158. })
  159. b.Run("AsInt64Slice", func(b *testing.B) {
  160. b.ReportAllocs()
  161. for i := 0; i < b.N; i++ {
  162. outInt64Slice = kv.Value.AsInt64Slice()
  163. }
  164. })
  165. b.Run("Emit", benchmarkEmit(kv))
  166. }
  167. func BenchmarkFloat64(b *testing.B) {
  168. k, v := "float64", float64(42)
  169. kv := attribute.Float64(k, v)
  170. b.Run("Value", func(b *testing.B) {
  171. b.ReportAllocs()
  172. for i := 0; i < b.N; i++ {
  173. outV = attribute.Float64Value(v)
  174. }
  175. })
  176. b.Run("KeyValue", func(b *testing.B) {
  177. b.ReportAllocs()
  178. for i := 0; i < b.N; i++ {
  179. outKV = attribute.Float64(k, v)
  180. }
  181. })
  182. b.Run("AsFloat64", func(b *testing.B) {
  183. b.ReportAllocs()
  184. for i := 0; i < b.N; i++ {
  185. outFloat64 = kv.Value.AsFloat64()
  186. }
  187. })
  188. b.Run("Emit", benchmarkEmit(kv))
  189. }
  190. func BenchmarkFloat64Slice(b *testing.B) {
  191. k, v := "float64 slice", []float64{42, -3, 12}
  192. kv := attribute.Float64Slice(k, v)
  193. b.Run("Value", func(b *testing.B) {
  194. b.ReportAllocs()
  195. for i := 0; i < b.N; i++ {
  196. outV = attribute.Float64SliceValue(v)
  197. }
  198. })
  199. b.Run("KeyValue", func(b *testing.B) {
  200. b.ReportAllocs()
  201. for i := 0; i < b.N; i++ {
  202. outKV = attribute.Float64Slice(k, v)
  203. }
  204. })
  205. b.Run("AsFloat64Slice", func(b *testing.B) {
  206. b.ReportAllocs()
  207. for i := 0; i < b.N; i++ {
  208. outFloat64Slice = kv.Value.AsFloat64Slice()
  209. }
  210. })
  211. b.Run("Emit", benchmarkEmit(kv))
  212. }
  213. func BenchmarkString(b *testing.B) {
  214. k, v := "string", "42"
  215. kv := attribute.String(k, v)
  216. b.Run("Value", func(b *testing.B) {
  217. b.ReportAllocs()
  218. for i := 0; i < b.N; i++ {
  219. outV = attribute.StringValue(v)
  220. }
  221. })
  222. b.Run("KeyValue", func(b *testing.B) {
  223. b.ReportAllocs()
  224. for i := 0; i < b.N; i++ {
  225. outKV = attribute.String(k, v)
  226. }
  227. })
  228. b.Run("AsString", func(b *testing.B) {
  229. b.ReportAllocs()
  230. for i := 0; i < b.N; i++ {
  231. outStr = kv.Value.AsString()
  232. }
  233. })
  234. b.Run("Emit", benchmarkEmit(kv))
  235. }
  236. func BenchmarkStringSlice(b *testing.B) {
  237. k, v := "float64 slice", []string{"forty-two", "negative three", "twelve"}
  238. kv := attribute.StringSlice(k, v)
  239. b.Run("Value", func(b *testing.B) {
  240. b.ReportAllocs()
  241. for i := 0; i < b.N; i++ {
  242. outV = attribute.StringSliceValue(v)
  243. }
  244. })
  245. b.Run("KeyValue", func(b *testing.B) {
  246. b.ReportAllocs()
  247. for i := 0; i < b.N; i++ {
  248. outKV = attribute.StringSlice(k, v)
  249. }
  250. })
  251. b.Run("AsStringSlice", func(b *testing.B) {
  252. b.ReportAllocs()
  253. for i := 0; i < b.N; i++ {
  254. outStrSlice = kv.Value.AsStringSlice()
  255. }
  256. })
  257. b.Run("Emit", benchmarkEmit(kv))
  258. }