github.com/netdata/go.d.plugin@v0.58.1/pkg/metrics/gauge_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package metrics
     4  
     5  import (
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestGauge_Set(t *testing.T) {
    13  	var g Gauge
    14  	assert.Equal(t, 0.0, g.Value())
    15  	g.Set(100)
    16  	assert.Equal(t, 100.0, g.Value())
    17  	g.Set(200)
    18  	assert.Equal(t, 200.0, g.Value())
    19  }
    20  
    21  func TestGauge_Add(t *testing.T) {
    22  	var g Gauge
    23  	assert.Equal(t, 0.0, g.Value())
    24  	g.Add(100)
    25  	assert.Equal(t, 100.0, g.Value())
    26  	g.Add(200)
    27  	assert.Equal(t, 300.0, g.Value())
    28  }
    29  func TestGauge_Sub(t *testing.T) {
    30  	var g Gauge
    31  	assert.Equal(t, 0.0, g.Value())
    32  	g.Sub(100)
    33  	assert.Equal(t, -100.0, g.Value())
    34  	g.Sub(200)
    35  	assert.Equal(t, -300.0, g.Value())
    36  }
    37  
    38  func TestGauge_Inc(t *testing.T) {
    39  	var g Gauge
    40  	assert.Equal(t, 0.0, g.Value())
    41  	g.Inc()
    42  	assert.Equal(t, 1.0, g.Value())
    43  }
    44  
    45  func TestGauge_Dec(t *testing.T) {
    46  	var g Gauge
    47  	assert.Equal(t, 0.0, g.Value())
    48  	g.Dec()
    49  	assert.Equal(t, -1.0, g.Value())
    50  }
    51  
    52  func TestGauge_SetToCurrentTime(t *testing.T) {
    53  	var g Gauge
    54  	g.SetToCurrentTime()
    55  	assert.InDelta(t, time.Now().Unix(), g.Value(), 1)
    56  }
    57  
    58  func TestGauge_WriteTo(t *testing.T) {
    59  	g := Gauge(3.14)
    60  	m := map[string]int64{}
    61  	g.WriteTo(m, "pi", 100, 1)
    62  	assert.Len(t, m, 1)
    63  	assert.EqualValues(t, 314, m["pi"])
    64  }
    65  
    66  func TestGaugeVec_WriteTo(t *testing.T) {
    67  	g := NewGaugeVec()
    68  	g.Get("foo").Inc()
    69  	g.Get("foo").Inc()
    70  	g.Get("bar").Inc()
    71  	g.Get("bar").Add(0.14)
    72  
    73  	m := map[string]int64{}
    74  	g.WriteTo(m, "pi", 100, 1)
    75  	assert.Len(t, m, 2)
    76  	assert.EqualValues(t, 200, m["pi_foo"])
    77  	assert.EqualValues(t, 114, m["pi_bar"])
    78  }
    79  
    80  func BenchmarkGauge_Add(b *testing.B) {
    81  	benchmarks := []struct {
    82  		name  string
    83  		value float64
    84  	}{
    85  		{"int", 1},
    86  		{"float", 3.14},
    87  	}
    88  	for _, bm := range benchmarks {
    89  		b.Run(bm.name, func(b *testing.B) {
    90  			var c Gauge
    91  			for i := 0; i < b.N; i++ {
    92  				c.Add(bm.value)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func BenchmarkGauge_Inc(b *testing.B) {
    99  	var c Gauge
   100  	for i := 0; i < b.N; i++ {
   101  		c.Inc()
   102  	}
   103  }
   104  
   105  func BenchmarkGauge_Set(b *testing.B) {
   106  	var c Gauge
   107  	for i := 0; i < b.N; i++ {
   108  		c.Set(3.14)
   109  	}
   110  }
   111  
   112  func BenchmarkGauge_Value(b *testing.B) {
   113  	var c Gauge
   114  	c.Inc()
   115  	c.Add(3.14)
   116  	for i := 0; i < b.N; i++ {
   117  		c.Value()
   118  	}
   119  }
   120  
   121  func BenchmarkGauge_WriteTo(b *testing.B) {
   122  	var c Gauge
   123  	c.Inc()
   124  	c.Add(3.14)
   125  	m := map[string]int64{}
   126  	for i := 0; i < b.N; i++ {
   127  		c.WriteTo(m, "pi", 100, 1)
   128  	}
   129  }