github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/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/testify/assert"
    12  	"github.com/instana/testify/require"
    13  	"github.com/mier85/go-sensor/autoprofile/internal"
    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  }