collection of high-performance prometheus counters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.9 KiB

  1. package prom
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/prometheus/client_golang/prometheus"
  7. dto "github.com/prometheus/client_model/go"
  8. )
  9. func TestNoPrecisionGauge(t *testing.T) {
  10. c := NewFixedPrecisionGauge(prometheus.GaugeOpts{
  11. Name: "test",
  12. Help: "test help",
  13. }, 0)
  14. c.Inc()
  15. var want float64 = 1
  16. if expected, got := want, c.Value(); expected != got {
  17. t.Errorf("Expected %v, got %v.", expected, got)
  18. }
  19. c.Add(0.9999999999999999999)
  20. want = 2
  21. if expected, got := want, c.Value(); expected != got {
  22. t.Errorf("Expected %v, got %v.", expected, got)
  23. }
  24. c.Sub(0.9999999999999999999 * 3)
  25. want = -1
  26. if expected, got := want, c.Value(); expected != got {
  27. t.Errorf("Expected %v, got %v.", expected, got)
  28. }
  29. m := &dto.Metric{}
  30. c.Write(m)
  31. if expected, got := `counter:<value:-1 > `, m.String(); expected != got {
  32. t.Errorf("expected %q, got %q", expected, got)
  33. }
  34. }
  35. func TestFixedPrecisionAdd(t *testing.T) {
  36. c := NewFixedPrecisionGauge(prometheus.GaugeOpts{
  37. Name: "test",
  38. Help: "test help",
  39. }, 3)
  40. c.Inc()
  41. want := 1.0
  42. if expected, got := want, c.Value(); expected != got {
  43. t.Errorf("Expected %f, got %f.", expected, got)
  44. }
  45. c.Add(42.3)
  46. want = 43.3
  47. if expected, got := want, c.Value(); expected != got {
  48. t.Errorf("Expected %f, got %f.", expected, got)
  49. }
  50. m := &dto.Metric{}
  51. c.Write(m)
  52. if expected, got := `counter:<value:43.3 > `, m.String(); expected != got {
  53. t.Errorf("expected %q, got %q", expected, got)
  54. }
  55. }
  56. func TestFixedPrecisionSub(t *testing.T) {
  57. c := NewFixedPrecisionGauge(prometheus.GaugeOpts{
  58. Name: "test",
  59. Help: "test help",
  60. }, 3)
  61. c.Dec()
  62. var want float64 = -1
  63. if expected, got := want, c.Value(); expected != got {
  64. t.Errorf("Expected %f, got %f.", expected, got)
  65. }
  66. c.Sub(42.3)
  67. want = -43.3
  68. if expected, got := want, c.Value(); expected != got {
  69. t.Errorf("Expected %f, got %f.", expected, got)
  70. }
  71. m := &dto.Metric{}
  72. c.Write(m)
  73. if expected, got := `counter:<value:-43.3 > `, m.String(); expected != got {
  74. t.Errorf("expected %q, got %q", expected, got)
  75. }
  76. }
  77. func TestSetToCurrentTime(t *testing.T) {
  78. precs := []uint{}
  79. var i uint
  80. for i = 0; i < 10; i++ {
  81. precs = append(precs, i)
  82. }
  83. for _, prec := range precs {
  84. c := NewFixedPrecisionGauge(prometheus.GaugeOpts{
  85. Name: "test",
  86. Help: "test help",
  87. }, prec)
  88. c.SetToCurrentTime()
  89. n := time.Now()
  90. delta := math.Abs(c.Value() - float64(n.Unix()))
  91. if !(delta <= 1) {
  92. t.Fatalf("SetToCurrentTime at %v precision was off from time.Now(): got: %v, want: <= 1", prec, delta)
  93. }
  94. }
  95. }
  96. func TestCounterDirection(t *testing.T) {
  97. defer func() {
  98. if e := recover(); e == nil {
  99. t.Fatalf("did not panic and shold have")
  100. }
  101. }()
  102. c := NewFixedPrecisionCounter(prometheus.CounterOpts{
  103. Name: "test",
  104. Help: "test help",
  105. }, 3)
  106. c.Add(1)
  107. var want float64 = 1
  108. if expected, got := want, c.Value(); expected != got {
  109. t.Errorf("Expected %f, got %f.", expected, got)
  110. }
  111. c.Add(-1)
  112. }