...

Source file src/go.opentelemetry.io/otel/attribute/benchmark_test.go

Documentation: go.opentelemetry.io/otel/attribute

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

View as plain text