github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/autoprofile/internal/cpu_sampler_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package internal_test 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 11 "github.com/instana/go-sensor/autoprofile/internal" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestCreateCPUProfile(t *testing.T) { 17 cpuSampler := internal.NewCPUSampler() 18 internal.IncludeProfilerFrames = true 19 20 cpuSampler.Reset() 21 cpuSampler.Start() 22 23 simulateCPULoad(1 * time.Second) 24 25 cpuSampler.Stop() 26 27 profile, err := cpuSampler.Profile(500*1e6, 120) 28 require.NoError(t, err) 29 30 assert.Contains(t, fmt.Sprintf("%v", internal.NewAgentProfile(profile)), "simulateCPULoad") 31 } 32 33 func simulateCPULoad(d time.Duration) { 34 done := time.After(d) 35 36 for { 37 select { 38 case <-done: 39 return 40 default: //nolint:staticcheck 41 } 42 } 43 }