Debian Codegraph @

pdata/pprofile/mappings_test.go

raw
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
12 "go.opentelemetry.io/collector/internal/testutil"
13 )
14
15 func TestSetMapping(t *testing.T) {
16 table := NewMappingSlice()
17 m := NewMapping()
18 m.SetMemoryLimit(1)
19 m2 := NewMapping()
20 m2.SetMemoryLimit(2)
21
22 // Put a first mapping
23 idx, err := SetMapping(table, m)
24 require.NoError(t, err)
25 assert.Equal(t, 1, table.Len())
26 assert.Equal(t, int32(0), idx)
27
28 // Put the same mapping
29 // This should be a no-op.
30 idx, err = SetMapping(table, m)
31 require.NoError(t, err)
32 assert.Equal(t, 1, table.Len())
33 assert.Equal(t, int32(0), idx)
34
35 // Set a new mapping
36 // This sets the index and adds to the table.
37 idx, err = SetMapping(table, m2)
38 require.NoError(t, err)
39 assert.Equal(t, 2, table.Len())
40 assert.Equal(t, int32(table.Len()-1), idx)
41
42 // Set an existing mapping
43 idx, err = SetMapping(table, m)
44 require.NoError(t, err)
45 assert.Equal(t, 2, table.Len())
46 assert.Equal(t, int32(0), idx)
47 // Set another existing mapping
48 idx, err = SetMapping(table, m2)
49 require.NoError(t, err)
50 assert.Equal(t, 2, table.Len())
51 assert.Equal(t, int32(table.Len()-1), idx)
52 }
53
54 func BenchmarkSetMapping(b *testing.B) {
55 testutil.SkipMemoryBench(b)
56 for _, bb := range []struct {
57 name string
58 mapping Mapping
59
60 runBefore func(*testing.B, MappingSlice)
61 }{
62 {
63 name: "with a new mapping",
64 mapping: NewMapping(),
65 },
66 {
67 name: "with an existing mapping",
68 mapping: func() Mapping {
69 m := NewMapping()
70 m.SetMemoryLimit(1)
71 return m
72 }(),
73
74 runBefore: func(_ *testing.B, table MappingSlice) {
75 m := table.AppendEmpty()
76 m.SetMemoryLimit(1)
77 },
78 },
79 {
80 name: "with a duplicate mapping",
81 mapping: NewMapping(),
82
83 runBefore: func(_ *testing.B, table MappingSlice) {
84 _, err := SetMapping(table, NewMapping())
85 require.NoError(b, err)
86 },
87 },
88 {
89 name: "with a hundred mappings to loop through",
90 mapping: func() Mapping {
91 m := NewMapping()
92 m.SetMemoryLimit(1)
93 return m
94 }(),
95
96 runBefore: func(_ *testing.B, table MappingSlice) {
97 for i := range 100 {
98 m := table.AppendEmpty()
99 m.SetMemoryLimit(uint64(i))
100 }
101 },
102 },
103 } {
104 b.Run(bb.name, func(b *testing.B) {
105 table := NewMappingSlice()
106
107 if bb.runBefore != nil {
108 bb.runBefore(b, table)
109 }
110
111 b.ResetTimer()
112 b.ReportAllocs()
113
114 for b.Loop() {
115 _, _ = SetMapping(table, bb.mapping)
116 }
117 })
118 }
119 }