github.com/theQRL/go-zond@v0.1.1/metrics/counter_float_64_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func BenchmarkCounterFloat64(b *testing.B) {
     9  	c := NewCounterFloat64()
    10  	b.ResetTimer()
    11  	for i := 0; i < b.N; i++ {
    12  		c.Inc(1.0)
    13  	}
    14  }
    15  
    16  func BenchmarkCounterFloat64Parallel(b *testing.B) {
    17  	c := NewCounterFloat64()
    18  	b.ResetTimer()
    19  	var wg sync.WaitGroup
    20  	for i := 0; i < 10; i++ {
    21  		wg.Add(1)
    22  		go func() {
    23  			for i := 0; i < b.N; i++ {
    24  				c.Inc(1.0)
    25  			}
    26  			wg.Done()
    27  		}()
    28  	}
    29  	wg.Wait()
    30  	if have, want := c.Snapshot().Count(), 10.0*float64(b.N); have != want {
    31  		b.Fatalf("have %f want %f", have, want)
    32  	}
    33  }
    34  
    35  func TestCounterFloat64Clear(t *testing.T) {
    36  	c := NewCounterFloat64()
    37  	c.Inc(1.0)
    38  	c.Clear()
    39  	if count := c.Snapshot().Count(); count != 0 {
    40  		t.Errorf("c.Count(): 0 != %v\n", count)
    41  	}
    42  }
    43  
    44  func TestCounterFloat64Dec1(t *testing.T) {
    45  	c := NewCounterFloat64()
    46  	c.Dec(1.0)
    47  	if count := c.Snapshot().Count(); count != -1.0 {
    48  		t.Errorf("c.Count(): -1.0 != %v\n", count)
    49  	}
    50  }
    51  
    52  func TestCounterFloat64Dec2(t *testing.T) {
    53  	c := NewCounterFloat64()
    54  	c.Dec(2.0)
    55  	if count := c.Snapshot().Count(); count != -2.0 {
    56  		t.Errorf("c.Count(): -2.0 != %v\n", count)
    57  	}
    58  }
    59  
    60  func TestCounterFloat64Inc1(t *testing.T) {
    61  	c := NewCounterFloat64()
    62  	c.Inc(1.0)
    63  	if count := c.Snapshot().Count(); count != 1.0 {
    64  		t.Errorf("c.Count(): 1.0 != %v\n", count)
    65  	}
    66  }
    67  
    68  func TestCounterFloat64Inc2(t *testing.T) {
    69  	c := NewCounterFloat64()
    70  	c.Inc(2.0)
    71  	if count := c.Snapshot().Count(); count != 2.0 {
    72  		t.Errorf("c.Count(): 2.0 != %v\n", count)
    73  	}
    74  }
    75  
    76  func TestCounterFloat64Snapshot(t *testing.T) {
    77  	c := NewCounterFloat64()
    78  	c.Inc(1.0)
    79  	snapshot := c.Snapshot()
    80  	c.Inc(1.0)
    81  	if count := snapshot.Count(); count != 1.0 {
    82  		t.Errorf("c.Count(): 1.0 != %v\n", count)
    83  	}
    84  }
    85  
    86  func TestCounterFloat64Zero(t *testing.T) {
    87  	c := NewCounterFloat64()
    88  	if count := c.Snapshot().Count(); count != 0 {
    89  		t.Errorf("c.Count(): 0 != %v\n", count)
    90  	}
    91  }
    92  
    93  func TestGetOrRegisterCounterFloat64(t *testing.T) {
    94  	r := NewRegistry()
    95  	NewRegisteredCounterFloat64("foo", r).Inc(47.0)
    96  	if c := GetOrRegisterCounterFloat64("foo", r).Snapshot(); c.Count() != 47.0 {
    97  		t.Fatal(c)
    98  	}
    99  }