github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/profiler/profiler_internal_test.go (about) 1 package profiler 2 3 import ( 4 "bytes" 5 "runtime" 6 "testing" 7 "time" 8 9 "github.com/google/pprof/profile" 10 "github.com/rs/zerolog" 11 "github.com/stretchr/testify/require" 12 13 "github.com/onflow/flow-go/utils/unittest" 14 ) 15 16 func TestGoHeapProfile(t *testing.T) { 17 t.Parallel() 18 t.Run("goHeapProfile", func(t *testing.T) { 19 unittest.RunWithTempDir(t, func(tempDir string) { 20 p, err := New( 21 zerolog.Nop(), 22 &NoopUploader{}, 23 ProfilerConfig{ 24 Enabled: false, 25 Dir: tempDir, 26 Interval: 100 * time.Millisecond, 27 Duration: 100 * time.Millisecond, 28 }) 29 require.NoError(t, err) 30 unittest.AssertClosesBefore(t, p.Ready(), 5*time.Second) 31 t.Logf("profiler ready %s", tempDir) 32 33 prof, err := p.goHeapProfile("inuse_objects", "alloc_space") 34 require.NoError(t, err) 35 require.NotEmpty(t, prof) 36 37 require.Equal(t, "inuse_objects", prof.DefaultSampleType) 38 require.Equal(t, 2, len(prof.SampleType)) 39 require.Equal(t, "inuse_objects", prof.SampleType[0].Type) 40 require.Equal(t, "alloc_space", prof.SampleType[1].Type) 41 require.NotZero(t, len(prof.Sample)) 42 require.Equal(t, 2, len(prof.Sample[0].Value)) 43 require.NotZero(t, prof.Sample[0].Value[0]+prof.Sample[0].Value[1]) 44 45 unittest.AssertClosesBefore(t, p.Done(), 5*time.Second) 46 }) 47 }) 48 } 49 50 func TestGoAllocsProfile(t *testing.T) { 51 t.Parallel() 52 t.Run("pprofAllocs", func(t *testing.T) { 53 unittest.RunWithTempDir(t, func(tempDir string) { 54 p, err := New( 55 zerolog.Nop(), 56 &NoopUploader{}, 57 ProfilerConfig{ 58 Enabled: false, 59 Dir: tempDir, 60 Interval: time.Hour, 61 Duration: time.Second, 62 }) 63 require.NoError(t, err) 64 unittest.AssertClosesBefore(t, p.Ready(), 5*time.Second) 65 t.Logf("profiler ready %s", tempDir) 66 67 ticker := time.NewTicker(time.Millisecond * 10) 68 defer ticker.Stop() 69 70 // do some allocations in the background 71 go func() { 72 for range ticker.C { 73 var m runtime.MemStats 74 runtime.ReadMemStats(&m) 75 } 76 }() 77 78 buf := &bytes.Buffer{} 79 err = p.pprofAllocs(buf, time.Second*1) 80 require.NoError(t, err) 81 82 prof, err := profile.Parse(buf) 83 require.NoError(t, err) 84 85 require.Equal(t, "alloc_objects", prof.DefaultSampleType) 86 require.Equal(t, 2, len(prof.SampleType)) 87 require.Equal(t, "alloc_objects", prof.SampleType[0].Type) 88 require.Equal(t, "alloc_space", prof.SampleType[1].Type) 89 require.NotZero(t, len(prof.Sample)) 90 require.Equal(t, 2, len(prof.Sample[0].Value)) 91 require.NotZero(t, prof.Sample[0].Value[0]+prof.Sample[0].Value[1]) 92 93 unittest.AssertClosesBefore(t, p.Done(), 5*time.Second) 94 }) 95 }) 96 }