|
| 1 | +//go:build (linux || darwin) && (amd64 || arm64) |
| 2 | + |
| 3 | +package java |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/go-kit/log" |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | + "github.com/prometheus/prometheus/model/labels" |
| 16 | + "github.com/stretchr/testify/mock" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + |
| 19 | + "github.com/grafana/alloy/internal/component/discovery" |
| 20 | + "github.com/grafana/alloy/internal/component/pyroscope" |
| 21 | + "github.com/grafana/alloy/internal/component/pyroscope/java/asprof" |
| 22 | +) |
| 23 | + |
| 24 | +type mockProfiler struct { |
| 25 | + mock.Mock |
| 26 | + dist *asprof.Distribution |
| 27 | +} |
| 28 | + |
| 29 | +func (m *mockProfiler) CopyLib(dist *asprof.Distribution, pid int) error { |
| 30 | + args := m.Called(dist, pid) |
| 31 | + return args.Error(0) |
| 32 | +} |
| 33 | + |
| 34 | +func (m *mockProfiler) Execute(dist *asprof.Distribution, argv []string) (string, string, error) { |
| 35 | + args := m.Called(dist, argv) |
| 36 | + return args.String(0), args.String(1), args.Error(2) |
| 37 | +} |
| 38 | + |
| 39 | +func (m *mockProfiler) Distribution() *asprof.Distribution { |
| 40 | + return m.dist |
| 41 | +} |
| 42 | + |
| 43 | +type mockAppendable struct { |
| 44 | + mock.Mock |
| 45 | +} |
| 46 | + |
| 47 | +func (m *mockAppendable) Appender() pyroscope.Appender { |
| 48 | + return m |
| 49 | +} |
| 50 | + |
| 51 | +func (m *mockAppendable) Append(ctx context.Context, labels labels.Labels, samples []*pyroscope.RawSample) error { |
| 52 | + args := m.Called(ctx, labels, samples) |
| 53 | + return args.Error(0) |
| 54 | +} |
| 55 | + |
| 56 | +func (m *mockAppendable) AppendIngest(ctx context.Context, profile *pyroscope.IncomingProfile) error { |
| 57 | + args := m.Called(ctx, profile) |
| 58 | + return args.Error(0) |
| 59 | +} |
| 60 | + |
| 61 | +func newTestProfilingLoop(_ *testing.T, profiler *mockProfiler, appendable pyroscope.Appendable) *profilingLoop { |
| 62 | + reg := prometheus.NewRegistry() |
| 63 | + output := pyroscope.NewFanout([]pyroscope.Appendable{appendable}, "test-appendable", reg) |
| 64 | + logger := log.NewNopLogger() |
| 65 | + cfg := ProfilingConfig{ |
| 66 | + Interval: 10 * time.Millisecond, |
| 67 | + SampleRate: 1000, |
| 68 | + CPU: true, |
| 69 | + Event: "cpu", |
| 70 | + } |
| 71 | + target := discovery.NewTargetFromMap(map[string]string{"foo": "bar"}) |
| 72 | + return newProfilingLoop(os.Getpid(), target, logger, profiler, output, cfg) |
| 73 | +} |
| 74 | + |
| 75 | +func TestProfilingLoop_StartStop(t *testing.T) { |
| 76 | + profiler := &mockProfiler{dist: &asprof.Distribution{}} |
| 77 | + appendable := &mockAppendable{} |
| 78 | + pid := os.Getpid() |
| 79 | + jfrPath := fmt.Sprintf("/tmp/asprof-%d-%d.jfr", pid, pid) |
| 80 | + |
| 81 | + pCh := make(chan *profilingLoop) |
| 82 | + |
| 83 | + profiler.On("CopyLib", profiler.dist, pid).Return(nil).Once() |
| 84 | + |
| 85 | + // expect the profiler to be executed with the correct arguments to start it |
| 86 | + profiler.On("Execute", profiler.dist, []string{ |
| 87 | + "-f", |
| 88 | + jfrPath, |
| 89 | + "-o", "jfr", |
| 90 | + "-e", "cpu", |
| 91 | + "-i", "1000000", |
| 92 | + "start", |
| 93 | + "--timeout", "0", |
| 94 | + strconv.Itoa(pid), |
| 95 | + }).Run(func(args mock.Arguments) { |
| 96 | + // wait for the profiling loop to be created |
| 97 | + p := <-pCh |
| 98 | + |
| 99 | + // create the jfr file |
| 100 | + f, err := os.Create(p.jfrFile) |
| 101 | + require.NoError(t, err) |
| 102 | + defer f.Close() |
| 103 | + }).Return("", "", nil).Once() |
| 104 | + |
| 105 | + // expect the profiler to be executed with the correct arguments to stop it |
| 106 | + profiler.On("Execute", profiler.dist, []string{ |
| 107 | + "stop", |
| 108 | + "-o", "jfr", |
| 109 | + strconv.Itoa(pid), |
| 110 | + }).Return("", "", nil).Once() |
| 111 | + |
| 112 | + p := newTestProfilingLoop(t, profiler, appendable) |
| 113 | + pCh <- p |
| 114 | + |
| 115 | + // wait for the profiling loop to finish |
| 116 | + require.NoError(t, p.Close()) |
| 117 | + |
| 118 | + // expect the profiler to clean up the jfr file |
| 119 | + _, err := os.Stat(p.jfrFile) |
| 120 | + require.True(t, os.IsNotExist(err)) |
| 121 | + |
| 122 | + profiler.AssertExpectations(t) |
| 123 | + appendable.AssertExpectations(t) |
| 124 | +} |
0 commit comments