Debian Codegraph @

pdata/pprofile/functions_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 TestSetFunction(t *testing.T) {
16 table := NewFunctionSlice()
17 f := NewFunction()
18 f.SetNameStrindex(1)
19 f2 := NewFunction()
20 f2.SetNameStrindex(2)
21
22 // Put a first function
23 idx, err := SetFunction(table, f)
24 require.NoError(t, err)
25 assert.Equal(t, 1, table.Len())
26 assert.Equal(t, int32(0), idx)
27
28 // Put the same function
29 // This should be a no-op.
30 idx, err = SetFunction(table, f)
31 require.NoError(t, err)
32 assert.Equal(t, 1, table.Len())
33 assert.Equal(t, int32(0), idx)
34
35 // Set a new function
36 // This sets the index and adds to the table.
37 idx, err = SetFunction(table, f2)
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 function
43 idx, err = SetFunction(table, f)
44 require.NoError(t, err)
45 assert.Equal(t, 2, table.Len())
46 assert.Equal(t, int32(0), idx)
47 // Set another existing function
48 idx, err = SetFunction(table, f2)
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 BenchmarkSetFunction(b *testing.B) {
55 testutil.SkipMemoryBench(b)
56 for _, bb := range []struct {
57 name string
58 fn Function
59
60 runBefore func(*testing.B, FunctionSlice)
61 }{
62 {
63 name: "with a new function",
64 fn: NewFunction(),
65 },
66 {
67 name: "with an existing function",
68 fn: func() Function {
69 f := NewFunction()
70 f.SetNameStrindex(1)
71 return f
72 }(),
73
74 runBefore: func(_ *testing.B, table FunctionSlice) {
75 f := table.AppendEmpty()
76 f.SetNameStrindex(1)
77 },
78 },
79 {
80 name: "with a duplicate function",
81 fn: NewFunction(),
82
83 runBefore: func(b *testing.B, table FunctionSlice) {
84 _, err := SetFunction(table, NewFunction())
85 require.NoError(b, err)
86 },
87 },
88 {
89 name: "with a hundred functions to loop through",
90 fn: func() Function {
91 f := NewFunction()
92 f.SetNameStrindex(1)
93 return f
94 }(),
95
96 runBefore: func(_ *testing.B, table FunctionSlice) {
97 for i := range 100 {
98 f := table.AppendEmpty()
99 f.SetNameStrindex(int32(i))
100 }
101 },
102 },
103 } {
104 b.Run(bb.name, func(b *testing.B) {
105 table := NewFunctionSlice()
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 _, _ = SetFunction(table, bb.fn)
116 }
117 })
118 }
119 }