github.com/jfrog/jfrog-cli-core/v2@v2.52.0/utils/coreutils/profiler_test.go (about)

     1  package coreutils
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestThreadDump(t *testing.T) {
    12  	// Create default profiler
    13  	profiler := NewProfiler()
    14  
    15  	// Start a thread that sleeps
    16  	go func() {
    17  		dummyZzzz()
    18  	}()
    19  
    20  	// Run thread dump
    21  	output, err := profiler.ThreadDump()
    22  	assert.NoError(t, err)
    23  
    24  	// Check results
    25  	assert.Contains(t, output, "Thread dump #0")
    26  	assert.Contains(t, output, "Thread dump #1")
    27  	assert.Contains(t, output, "Thread dump #2")
    28  	assert.Contains(t, output, "dummyZzzz")
    29  }
    30  
    31  func TestThreadInterval(t *testing.T) {
    32  	// Create profiler with 10 repetitions and 10ms intervals
    33  	var expectedRepetitions uint = 10
    34  	var expectedInterval = 10 * time.Millisecond
    35  	profiler := NewProfiler(WithInterval(expectedInterval), WithRepetitions(expectedRepetitions))
    36  
    37  	// Check that the required values are set
    38  	assert.Equal(t, profiler.interval, expectedInterval)
    39  	assert.Equal(t, profiler.repetitions, expectedRepetitions)
    40  
    41  	// start measure the time
    42  	start := time.Now()
    43  
    44  	// Run thread dump
    45  	output, err := profiler.ThreadDump()
    46  	assert.NoError(t, err)
    47  
    48  	// Ensure duration less than 1 second
    49  	assert.WithinDuration(t, start, time.Now(), time.Second)
    50  
    51  	// Ensure 10 repetitions
    52  	assert.Contains(t, output, "Thread dump #"+strconv.FormatUint(uint64(expectedRepetitions)-1, 10))
    53  }
    54  
    55  // In the thread dump test, we look for this function name in the output to ensure that functions executed in goroutines are recorded.
    56  func dummyZzzz() {
    57  	time.Sleep(2 * time.Second)
    58  }