mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
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 ```
113 lines
2.7 KiB
Go
113 lines
2.7 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 TestScopeProfilesSwitchDictionary(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
scopeProfiles ScopeProfiles
|
|
|
|
src ProfilesDictionary
|
|
dst ProfilesDictionary
|
|
|
|
wantScopeProfiles ScopeProfiles
|
|
wantDictionary ProfilesDictionary
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "with an empty scope profile",
|
|
scopeProfiles: NewScopeProfiles(),
|
|
|
|
src: NewProfilesDictionary(),
|
|
dst: NewProfilesDictionary(),
|
|
|
|
wantScopeProfiles: NewScopeProfiles(),
|
|
wantDictionary: NewProfilesDictionary(),
|
|
},
|
|
{
|
|
name: "with a scope profiles that has a profile",
|
|
scopeProfiles: func() ScopeProfiles {
|
|
s := NewScopeProfiles()
|
|
profile := s.Profiles().AppendEmpty()
|
|
profile.Samples().AppendEmpty().SetLinkIndex(1)
|
|
return s
|
|
}(),
|
|
|
|
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
|
|
}(),
|
|
|
|
wantScopeProfiles: func() ScopeProfiles {
|
|
s := NewScopeProfiles()
|
|
profile := s.Profiles().AppendEmpty()
|
|
profile.Samples().AppendEmpty().SetLinkIndex(2)
|
|
return s
|
|
}(),
|
|
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) {
|
|
sp := tt.scopeProfiles
|
|
dst := tt.dst
|
|
err := sp.switchDictionary(tt.src, dst)
|
|
|
|
if tt.wantErr == nil {
|
|
require.NoError(t, err)
|
|
} else {
|
|
require.Equal(t, tt.wantErr, err)
|
|
}
|
|
|
|
assert.Equal(t, tt.wantScopeProfiles, sp)
|
|
assert.Equal(t, tt.wantDictionary, dst)
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkScopeProfilesSwitchDictionary(b *testing.B) {
|
|
testutil.SkipMemoryBench(b)
|
|
|
|
s := NewScopeProfiles()
|
|
profile := s.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}))
|
|
|
|
dst := NewProfilesDictionary()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for b.Loop() {
|
|
_ = s.switchDictionary(src, dst)
|
|
}
|
|
}
|