github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sampler_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/lulzWill/go-agent/internal/logger"
     8  )
     9  
    10  func TestGetSample(t *testing.T) {
    11  	now := time.Now()
    12  	sample := GetSample(now, logger.ShimLogger{})
    13  	if nil == sample {
    14  		t.Fatal(sample)
    15  	}
    16  	if now != sample.when {
    17  		t.Error(now, sample.when)
    18  	}
    19  	if sample.numGoroutine <= 0 {
    20  		t.Error(sample.numGoroutine)
    21  	}
    22  	if sample.numCPU <= 0 {
    23  		t.Error(sample.numCPU)
    24  	}
    25  	if sample.memStats.HeapObjects == 0 {
    26  		t.Error(sample.memStats.HeapObjects)
    27  	}
    28  }
    29  
    30  func TestMetricsCreated(t *testing.T) {
    31  	now := time.Now()
    32  	h := NewHarvest(now)
    33  
    34  	stats := Stats{
    35  		heapObjects:  5 * 1000,
    36  		numGoroutine: 23,
    37  		allocBytes:   37 * 1024 * 1024,
    38  		user: cpuStats{
    39  			used:     20 * time.Millisecond,
    40  			fraction: 0.01,
    41  		},
    42  		system: cpuStats{
    43  			used:     40 * time.Millisecond,
    44  			fraction: 0.02,
    45  		},
    46  		gcPauseFraction: 3e-05,
    47  		deltaNumGC:      2,
    48  		deltaPauseTotal: 500 * time.Microsecond,
    49  		minPause:        100 * time.Microsecond,
    50  		maxPause:        400 * time.Microsecond,
    51  	}
    52  
    53  	stats.MergeIntoHarvest(h)
    54  
    55  	ExpectMetrics(t, h.Metrics, []WantMetric{
    56  		{"Memory/Heap/AllocatedObjects", "", true, []float64{1, 5000, 5000, 5000, 5000, 25000000}},
    57  		{"Memory/Physical", "", true, []float64{1, 37, 0, 37, 37, 1369}},
    58  		{"CPU/User Time", "", true, []float64{1, 0.02, 0.02, 0.02, 0.02, 0.0004}},
    59  		{"CPU/System Time", "", true, []float64{1, 0.04, 0.04, 0.04, 0.04, 0.0016}},
    60  		{"CPU/User/Utilization", "", true, []float64{1, 0.01, 0, 0.01, 0.01, 0.0001}},
    61  		{"CPU/System/Utilization", "", true, []float64{1, 0.02, 0, 0.02, 0.02, 0.0004}},
    62  		{"Go/Runtime/Goroutines", "", true, []float64{1, 23, 23, 23, 23, 529}},
    63  		{"GC/System/Pause Fraction", "", true, []float64{1, 3e-05, 0, 3e-05, 3e-05, 9e-10}},
    64  		{"GC/System/Pauses", "", true, []float64{2, 0.0005, 0, 0.0001, 0.0004, 2.5e-7}},
    65  	})
    66  }
    67  
    68  func TestMetricsCreatedEmpty(t *testing.T) {
    69  	now := time.Now()
    70  	h := NewHarvest(now)
    71  	stats := Stats{}
    72  
    73  	stats.MergeIntoHarvest(h)
    74  
    75  	ExpectMetrics(t, h.Metrics, []WantMetric{
    76  		{"Memory/Heap/AllocatedObjects", "", true, []float64{1, 0, 0, 0, 0, 0}},
    77  		{"Memory/Physical", "", true, []float64{1, 0, 0, 0, 0, 0}},
    78  		{"CPU/User Time", "", true, []float64{1, 0, 0, 0, 0, 0}},
    79  		{"CPU/System Time", "", true, []float64{1, 0, 0, 0, 0, 0}},
    80  		{"CPU/User/Utilization", "", true, []float64{1, 0, 0, 0, 0, 0}},
    81  		{"CPU/System/Utilization", "", true, []float64{1, 0, 0, 0, 0, 0}},
    82  		{"Go/Runtime/Goroutines", "", true, []float64{1, 0, 0, 0, 0, 0}},
    83  		{"GC/System/Pause Fraction", "", true, []float64{1, 0, 0, 0, 0, 0}},
    84  	})
    85  }