github.com/newrelic/go-agent@v3.26.0+incompatible/internal/sampler_test.go (about)

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