mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-02-09 13:37:12 +02:00
Remove one allocation in all SliceValue function (going from 3 to 2). Here is benchstat results ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/internal/attribute cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ BoolSliceValue-8 128.4n ± 22% 103.8n ± 25% -19.12% (p=0.007 n=10) Int64SliceValue-8 167.9n ± 7% 130.8n ± 5% -22.13% (p=0.000 n=10) Float64SliceValue-8 133.8n ± 14% 122.6n ± 4% -8.33% (p=0.000 n=10) StringSliceValue-8 166.4n ± 9% 158.5n ± 10% -4.75% (p=0.037 n=10) geomean 148.0n 127.5n -13.88% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ BoolSliceValue-8 32.000 ± 0% 8.000 ± 0% -75.00% (p=0.000 n=10) Int64SliceValue-8 88.00 ± 0% 64.00 ± 0% -27.27% (p=0.000 n=10) Float64SliceValue-8 88.00 ± 0% 64.00 ± 0% -27.27% (p=0.000 n=10) StringSliceValue-8 152.0 ± 0% 128.0 ± 0% -15.79% (p=0.000 n=10) geomean 78.34 45.25 -42.23% │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ BoolSliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) Int64SliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) Float64SliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) StringSliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) geomean 3.000 2.000 -33.33% ```
136 lines
3.2 KiB
Go
136 lines
3.2 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package attribute
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
var wrapFloat64SliceValue = func(v interface{}) interface{} {
|
|
if vi, ok := v.([]float64); ok {
|
|
return Float64SliceValue(vi)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var wrapInt64SliceValue = func(v interface{}) interface{} {
|
|
if vi, ok := v.([]int64); ok {
|
|
return Int64SliceValue(vi)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var wrapBoolSliceValue = func(v interface{}) interface{} {
|
|
if vi, ok := v.([]bool); ok {
|
|
return BoolSliceValue(vi)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var wrapStringSliceValue = func(v interface{}) interface{} {
|
|
if vi, ok := v.([]string); ok {
|
|
return StringSliceValue(vi)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
wrapAsBoolSlice = func(v interface{}) interface{} { return AsBoolSlice(v) }
|
|
wrapAsInt64Slice = func(v interface{}) interface{} { return AsInt64Slice(v) }
|
|
wrapAsFloat64Slice = func(v interface{}) interface{} { return AsFloat64Slice(v) }
|
|
wrapAsStringSlice = func(v interface{}) interface{} { return AsStringSlice(v) }
|
|
)
|
|
|
|
func TestSliceValue(t *testing.T) {
|
|
type args struct {
|
|
v interface{}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want interface{}
|
|
fn func(interface{}) interface{}
|
|
}{
|
|
{
|
|
name: "Float64SliceValue() two items",
|
|
args: args{v: []float64{1, 2.3}}, want: [2]float64{1, 2.3}, fn: wrapFloat64SliceValue,
|
|
},
|
|
{
|
|
name: "Int64SliceValue() two items",
|
|
args: args{[]int64{1, 2}}, want: [2]int64{1, 2}, fn: wrapInt64SliceValue,
|
|
},
|
|
{
|
|
name: "BoolSliceValue() two items",
|
|
args: args{v: []bool{true, false}}, want: [2]bool{true, false}, fn: wrapBoolSliceValue,
|
|
},
|
|
{
|
|
name: "StringSliceValue() two items",
|
|
args: args{[]string{"123", "2"}}, want: [2]string{"123", "2"}, fn: wrapStringSliceValue,
|
|
},
|
|
{
|
|
name: "AsBoolSlice() two items",
|
|
args: args{[2]bool{true, false}}, want: []bool{true, false}, fn: wrapAsBoolSlice,
|
|
},
|
|
{
|
|
name: "AsInt64Slice() two items",
|
|
args: args{[2]int64{1, 3}}, want: []int64{1, 3}, fn: wrapAsInt64Slice,
|
|
},
|
|
{
|
|
name: "AsFloat64Slice() two items",
|
|
args: args{[2]float64{1.2, 3.1}}, want: []float64{1.2, 3.1}, fn: wrapAsFloat64Slice,
|
|
},
|
|
{
|
|
name: "AsStringSlice() two items",
|
|
args: args{[2]string{"1234", "12"}}, want: []string{"1234", "12"}, fn: wrapAsStringSlice,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := tt.fn(tt.args.v); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("got %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// sync is a global used to ensure the benchmark are not optimized away.
|
|
var sync any
|
|
|
|
func BenchmarkBoolSliceValue(b *testing.B) {
|
|
b.ReportAllocs()
|
|
s := []bool{true, false, true, false}
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
sync = BoolSliceValue(s)
|
|
}
|
|
}
|
|
|
|
func BenchmarkInt64SliceValue(b *testing.B) {
|
|
b.ReportAllocs()
|
|
s := []int64{1, 2, 3, 4}
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
sync = Int64SliceValue(s)
|
|
}
|
|
}
|
|
|
|
func BenchmarkFloat64SliceValue(b *testing.B) {
|
|
b.ReportAllocs()
|
|
s := []float64{1.2, 3.4, 5.6, 7.8}
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
sync = Float64SliceValue(s)
|
|
}
|
|
}
|
|
|
|
func BenchmarkStringSliceValue(b *testing.B) {
|
|
b.ReportAllocs()
|
|
s := []string{"a", "b", "c", "d"}
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
sync = StringSliceValue(s)
|
|
}
|
|
}
|