| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofile |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
gootlpprofiles "go.opentelemetry.io/proto/slim/otlp/profiles/v1development" |
| 12 |
goproto "google.golang.org/protobuf/proto" |
| 13 |
) |
| 14 |
|
| 15 |
func TestProfilesProtoWireCompatibility(t *testing.T) { |
| 16 |
// This test verifies that OTLP ProtoBufs generated using goproto lib in |
| 17 |
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in |
| 18 |
// this repository are wire compatible. |
| 19 |
|
| 20 |
// Generate Profiles as pdata struct. |
| 21 |
td := generateTestProfiles() |
| 22 |
|
| 23 |
// Marshal its underlying ProtoBuf to wire. |
| 24 |
marshaler := &ProtoMarshaler{} |
| 25 |
wire1, err := marshaler.MarshalProfiles(td) |
| 26 |
require.NoError(t, err) |
| 27 |
assert.NotNil(t, wire1) |
| 28 |
|
| 29 |
// Unmarshal from the wire to OTLP Protobuf in goproto's representation. |
| 30 |
var goprotoMessage gootlpprofiles.ProfilesData |
| 31 |
err = goproto.Unmarshal(wire1, &goprotoMessage) |
| 32 |
require.NoError(t, err) |
| 33 |
|
| 34 |
// Marshal to the wire again. |
| 35 |
wire2, err := goproto.Marshal(&goprotoMessage) |
| 36 |
require.NoError(t, err) |
| 37 |
assert.NotNil(t, wire2) |
| 38 |
|
| 39 |
// Unmarshal from the wire into gogoproto's representation. |
| 40 |
var td2 Profiles |
| 41 |
unmarshaler := &ProtoUnmarshaler{} |
| 42 |
td2, err = unmarshaler.UnmarshalProfiles(wire2) |
| 43 |
require.NoError(t, err) |
| 44 |
|
| 45 |
// Now compare that the original and final ProtoBuf messages are the same. |
| 46 |
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible. |
| 47 |
assert.Equal(t, td, td2) |
| 48 |
} |
| 49 |
|
| 50 |
func TestProtoProfilesUnmarshalerError(t *testing.T) { |
| 51 |
p := &ProtoUnmarshaler{} |
| 52 |
_, err := p.UnmarshalProfiles([]byte("+$%")) |
| 53 |
assert.Error(t, err) |
| 54 |
} |
| 55 |
|
| 56 |
func TestProtoSizer(t *testing.T) { |
| 57 |
marshaler := &ProtoMarshaler{} |
| 58 |
td := NewProfiles() |
| 59 |
td.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 60 |
td.Dictionary().StringTable().Append("foobar") |
| 61 |
|
| 62 |
size := marshaler.ProfilesSize(td) |
| 63 |
|
| 64 |
bytes, err := marshaler.MarshalProfiles(td) |
| 65 |
require.NoError(t, err) |
| 66 |
assert.Equal(t, len(bytes), size) |
| 67 |
} |
| 68 |
|
| 69 |
func TestProtoSizerEmptyProfiles(t *testing.T) { |
| 70 |
sizer := &ProtoMarshaler{} |
| 71 |
assert.Equal(t, 2, sizer.ProfilesSize(NewProfiles())) |
| 72 |
} |
| 73 |
|
| 74 |
func BenchmarkProfilesToProto(b *testing.B) { |
| 75 |
marshaler := &ProtoMarshaler{} |
| 76 |
profiles := generateBenchmarkProfiles(128) |
| 77 |
|
| 78 |
for b.Loop() { |
| 79 |
buf, err := marshaler.MarshalProfiles(profiles) |
| 80 |
require.NoError(b, err) |
| 81 |
assert.NotEmpty(b, buf) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func BenchmarkProfilesFromProto(b *testing.B) { |
| 86 |
marshaler := &ProtoMarshaler{} |
| 87 |
unmarshaler := &ProtoUnmarshaler{} |
| 88 |
baseProfiles := generateBenchmarkProfiles(128) |
| 89 |
buf, err := marshaler.MarshalProfiles(baseProfiles) |
| 90 |
require.NoError(b, err) |
| 91 |
assert.NotEmpty(b, buf) |
| 92 |
|
| 93 |
b.ReportAllocs() |
| 94 |
for b.Loop() { |
| 95 |
profiles, err := unmarshaler.UnmarshalProfiles(buf) |
| 96 |
require.NoError(b, err) |
| 97 |
assert.Equal(b, baseProfiles.ResourceProfiles().Len(), profiles.ResourceProfiles().Len()) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func generateBenchmarkProfiles(samplesCount int) Profiles { |
| 102 |
md := NewProfiles() |
| 103 |
ilm := md.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 104 |
ilm.Samples().EnsureCapacity(samplesCount) |
| 105 |
for range samplesCount { |
| 106 |
im := ilm.Samples().AppendEmpty() |
| 107 |
im.SetStackIndex(0) |
| 108 |
} |
| 109 |
return md |
| 110 |
} |