gauge_float64_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package metrics
  2. import "testing"
  3. func BenchmarkGuageFloat64(b *testing.B) {
  4. g := NewGaugeFloat64()
  5. b.ResetTimer()
  6. for i := 0; i < b.N; i++ {
  7. g.Update(float64(i))
  8. }
  9. }
  10. func BenchmarkGuageFloat64Parallel(b *testing.B) {
  11. g := NewGaugeFloat64()
  12. b.ResetTimer()
  13. b.RunParallel(func(pb *testing.PB) {
  14. for pb.Next() {
  15. g.Update(float64(1))
  16. }
  17. })
  18. }
  19. func TestGaugeFloat64(t *testing.T) {
  20. g := NewGaugeFloat64()
  21. g.Update(float64(47.0))
  22. if v := g.Value(); float64(47.0) != v {
  23. t.Errorf("g.Value(): 47.0 != %v\n", v)
  24. }
  25. }
  26. func TestGaugeFloat64Snapshot(t *testing.T) {
  27. g := NewGaugeFloat64()
  28. g.Update(float64(47.0))
  29. snapshot := g.Snapshot()
  30. g.Update(float64(0))
  31. if v := snapshot.Value(); float64(47.0) != v {
  32. t.Errorf("g.Value(): 47.0 != %v\n", v)
  33. }
  34. }
  35. func TestGetOrRegisterGaugeFloat64(t *testing.T) {
  36. r := NewRegistry()
  37. NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0))
  38. t.Logf("registry: %v", r)
  39. if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() {
  40. t.Fatal(g)
  41. }
  42. }
  43. func TestFunctionalGaugeFloat64(t *testing.T) {
  44. var counter float64
  45. fg := NewFunctionalGaugeFloat64(func() float64 {
  46. counter++
  47. return counter
  48. })
  49. fg.Value()
  50. fg.Value()
  51. if counter != 2 {
  52. t.Error("counter != 2")
  53. }
  54. }
  55. func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
  56. r := NewRegistry()
  57. NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
  58. if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
  59. t.Fatal(g)
  60. }
  61. }