github.com/codingfuture/orig-energi3@v0.8.4/metrics/gauge_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  func BenchmarkGuage(b *testing.B) {
    26  	g := NewGauge()
    27  	b.ResetTimer()
    28  	for i := 0; i < b.N; i++ {
    29  		g.Update(int64(i))
    30  	}
    31  }
    32  
    33  func TestGauge(t *testing.T) {
    34  	g := NewGauge()
    35  	g.Update(int64(47))
    36  	if v := g.Value(); 47 != v {
    37  		t.Errorf("g.Value(): 47 != %v\n", v)
    38  	}
    39  }
    40  
    41  func TestGaugeSnapshot(t *testing.T) {
    42  	g := NewGauge()
    43  	g.Update(int64(47))
    44  	snapshot := g.Snapshot()
    45  	g.Update(int64(0))
    46  	if v := snapshot.Value(); 47 != v {
    47  		t.Errorf("g.Value(): 47 != %v\n", v)
    48  	}
    49  }
    50  
    51  func TestGetOrRegisterGauge(t *testing.T) {
    52  	r := NewRegistry()
    53  	NewRegisteredGauge("foo", r).Update(47)
    54  	if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
    55  		t.Fatal(g)
    56  	}
    57  }
    58  
    59  func TestFunctionalGauge(t *testing.T) {
    60  	var counter int64
    61  	fg := NewFunctionalGauge(func() int64 {
    62  		counter++
    63  		return counter
    64  	})
    65  	fg.Value()
    66  	fg.Value()
    67  	if counter != 2 {
    68  		t.Error("counter != 2")
    69  	}
    70  }
    71  
    72  func TestGetOrRegisterFunctionalGauge(t *testing.T) {
    73  	r := NewRegistry()
    74  	NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
    75  	if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
    76  		t.Fatal(g)
    77  	}
    78  }
    79  
    80  func ExampleGetOrRegisterGauge() {
    81  	m := "server.bytes_sent"
    82  	g := GetOrRegisterGauge(m, nil)
    83  	g.Update(47)
    84  	fmt.Println(g.Value()) // Output: 47
    85  }