Files
telemetry/pdata/pprofile/resourceprofiles_test.go
Damien MathieuandGitHub 0217e5d61c chore: add switchDictionary benchmarks (#14201)
This adds benchmarks for the `switchDictionary` method within pprofile.
None of them should emit allocs ever.

```
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/collector/pdata/pprofile
cpu: Apple M1 Max
BenchmarkFunctionSwitchDictionary-10                    87434488                13.44 ns/op            0 B/op          0 allocs/op
BenchmarkKeyValueAndUnitSwitchDictionary-10             584721352                2.047 ns/op           0 B/op          0 allocs/op
BenchmarkLineSwitchDictionary-10                        577840999                2.073 ns/op           0 B/op          0 allocs/op
BenchmarkLocationSwitchDictionary-10                    55512399                21.56 ns/op            0 B/op          0 allocs/op
BenchmarkMappingSwitchDictionary-10                     26952688                44.67 ns/op            0 B/op          0 allocs/op
BenchmarkProfileSwitchDictionary-10                     24649387                48.70 ns/op            0 B/op          0 allocs/op
BenchmarkProfilesSwitchDictionary-10                    58513749                20.36 ns/op            0 B/op          0 allocs/op
BenchmarkResourceProfilesSwitchDictionary-10            67610554                16.99 ns/op            0 B/op          0 allocs/op
BenchmarkSampleSwitchDictionary-10                      468034540                2.573 ns/op           0 B/op          0 allocs/op
BenchmarkScopeProfilesSwitchDictionary-10               90358615                13.13 ns/op            0 B/op          0 allocs/op
BenchmarkStackSwitchDictionary-10                       35702508                33.42 ns/op            0 B/op          0 allocs/op
BenchmarkValueTypeSwitchDictionary-10                   91959018                12.88 ns/op            0 B/op          0 allocs/op
```
2025-11-26 11:23:26 +00:00

115 lines
2.9 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pprofile
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/internal/testutil"
"go.opentelemetry.io/collector/pdata/pcommon"
)
func TestResourceProfilesSwitchDictionary(t *testing.T) {
for _, tt := range []struct {
name string
resourceProfiles ResourceProfiles
src ProfilesDictionary
dst ProfilesDictionary
wantResourceProfiles ResourceProfiles
wantDictionary ProfilesDictionary
wantErr error
}{
{
name: "with an empty resource profile",
resourceProfiles: NewResourceProfiles(),
src: NewProfilesDictionary(),
dst: NewProfilesDictionary(),
wantResourceProfiles: NewResourceProfiles(),
wantDictionary: NewProfilesDictionary(),
},
{
name: "with a resource profiles that has a profile",
resourceProfiles: func() ResourceProfiles {
r := NewResourceProfiles()
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
profile.Samples().AppendEmpty().SetLinkIndex(1)
return r
}(),
src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.LinkTable().AppendEmpty()
l := d.LinkTable().AppendEmpty()
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
return d
}(),
dst: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.LinkTable().AppendEmpty()
d.LinkTable().AppendEmpty()
return d
}(),
wantResourceProfiles: func() ResourceProfiles {
r := NewResourceProfiles()
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
profile.Samples().AppendEmpty().SetLinkIndex(2)
return r
}(),
wantDictionary: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.LinkTable().AppendEmpty()
d.LinkTable().AppendEmpty()
l := d.LinkTable().AppendEmpty()
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
return d
}(),
},
} {
t.Run(tt.name, func(t *testing.T) {
rp := tt.resourceProfiles
dst := tt.dst
err := rp.switchDictionary(tt.src, dst)
if tt.wantErr == nil {
require.NoError(t, err)
} else {
require.Equal(t, tt.wantErr, err)
}
assert.Equal(t, tt.wantResourceProfiles, rp)
assert.Equal(t, tt.wantDictionary, dst)
})
}
}
func BenchmarkResourceProfilesSwitchDictionary(b *testing.B) {
testutil.SkipMemoryBench(b)
r := NewResourceProfiles()
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
profile.Samples().AppendEmpty().SetLinkIndex(1)
src := NewProfilesDictionary()
src.LinkTable().AppendEmpty()
src.LinkTable().AppendEmpty().SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
b.ReportAllocs()
for b.Loop() {
b.StopTimer()
dst := NewProfilesDictionary()
b.StartTimer()
_ = r.switchDictionary(src, dst)
}
}