buckets_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package metrics
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestNewDurationBuckets(t *testing.T) {
  8. buckets := []time.Duration{
  9. 1 * time.Second,
  10. 3 * time.Second,
  11. 5 * time.Second,
  12. }
  13. bk := NewDurationBuckets(buckets...)
  14. expect := durationBuckets{
  15. buckets: []time.Duration{
  16. 1 * time.Second,
  17. 3 * time.Second,
  18. 5 * time.Second,
  19. },
  20. }
  21. assert.Equal(t, expect, bk)
  22. }
  23. func Test_durationBuckets_MapDuration(t *testing.T) {
  24. bk := NewDurationBuckets([]time.Duration{
  25. 1 * time.Second,
  26. 3 * time.Second,
  27. 5 * time.Second,
  28. }...)
  29. for i := 0; i <= bk.Size(); i++ {
  30. assert.Equal(t, i, bk.MapDuration(time.Duration(i*2)*time.Second))
  31. }
  32. }
  33. func Test_durationBuckets_Size(t *testing.T) {
  34. var buckets []time.Duration
  35. for i := 1; i < 3; i++ {
  36. buckets = append(buckets, time.Duration(i)*time.Second)
  37. bk := NewDurationBuckets(buckets...)
  38. assert.Equal(t, i, bk.Size())
  39. }
  40. }
  41. func Test_durationBuckets_UpperBound(t *testing.T) {
  42. bk := NewDurationBuckets([]time.Duration{
  43. 1 * time.Second,
  44. 2 * time.Second,
  45. 3 * time.Second,
  46. }...)
  47. assert.Panics(t, func() { bk.UpperBound(999) })
  48. for i := 0; i < bk.Size()-1; i++ {
  49. assert.Equal(t, time.Duration(i+1)*time.Second, bk.UpperBound(i))
  50. }
  51. }
  52. func TestNewBuckets(t *testing.T) {
  53. bk := NewBuckets(1, 3, 5)
  54. expect := buckets{
  55. buckets: []float64{1, 3, 5},
  56. }
  57. assert.Equal(t, expect, bk)
  58. }
  59. func Test_buckets_MapValue(t *testing.T) {
  60. bk := NewBuckets(1, 3, 5)
  61. for i := 0; i <= bk.Size(); i++ {
  62. assert.Equal(t, i, bk.MapValue(float64(i*2)))
  63. }
  64. }
  65. func Test_buckets_Size(t *testing.T) {
  66. var buckets []float64
  67. for i := 1; i < 3; i++ {
  68. buckets = append(buckets, float64(i))
  69. bk := NewBuckets(buckets...)
  70. assert.Equal(t, i, bk.Size())
  71. }
  72. }
  73. func Test_buckets_UpperBound(t *testing.T) {
  74. bk := NewBuckets(1, 2, 3)
  75. assert.Panics(t, func() { bk.UpperBound(999) })
  76. for i := 0; i < bk.Size()-1; i++ {
  77. assert.Equal(t, float64(i+1), bk.UpperBound(i))
  78. }
  79. }
  80. func TestMakeLinearBuckets_CorrectParameters_NotPanics(t *testing.T) {
  81. assert.NotPanics(t, func() {
  82. assert.Equal(t,
  83. NewBuckets(0.0, 1.0, 2.0),
  84. MakeLinearBuckets(0, 1, 3),
  85. )
  86. })
  87. }
  88. func TestMakeLinearBucketsPanicsOnBadCount(t *testing.T) {
  89. assert.Panics(t, func() {
  90. MakeLinearBuckets(0, 1, 0)
  91. })
  92. }
  93. func TestMakeLinearDurationBuckets(t *testing.T) {
  94. assert.NotPanics(t, func() {
  95. assert.Equal(t,
  96. NewDurationBuckets(0, time.Second, 2*time.Second),
  97. MakeLinearDurationBuckets(0*time.Second, 1*time.Second, 3),
  98. )
  99. })
  100. }
  101. func TestMakeLinearDurationBucketsPanicsOnBadCount(t *testing.T) {
  102. assert.Panics(t, func() {
  103. MakeLinearDurationBuckets(0*time.Second, 1*time.Second, 0)
  104. })
  105. }
  106. func TestMakeExponentialBuckets(t *testing.T) {
  107. assert.NotPanics(t, func() {
  108. assert.Equal(
  109. t,
  110. NewBuckets(2, 4, 8),
  111. MakeExponentialBuckets(2, 2, 3),
  112. )
  113. })
  114. }
  115. func TestMakeExponentialBucketsPanicsOnBadCount(t *testing.T) {
  116. assert.Panics(t, func() {
  117. MakeExponentialBuckets(2, 2, 0)
  118. })
  119. }
  120. func TestMakeExponentialBucketsPanicsOnBadStart(t *testing.T) {
  121. assert.Panics(t, func() {
  122. MakeExponentialBuckets(0, 2, 2)
  123. })
  124. }
  125. func TestMakeExponentialBucketsPanicsOnBadFactor(t *testing.T) {
  126. assert.Panics(t, func() {
  127. MakeExponentialBuckets(2, 1, 2)
  128. })
  129. }
  130. func TestMakeExponentialDurationBuckets(t *testing.T) {
  131. assert.NotPanics(t, func() {
  132. assert.Equal(
  133. t,
  134. NewDurationBuckets(2*time.Second, 4*time.Second, 8*time.Second),
  135. MakeExponentialDurationBuckets(2*time.Second, 2, 3),
  136. )
  137. })
  138. }
  139. func TestMakeExponentialDurationBucketsPanicsOnBadCount(t *testing.T) {
  140. assert.Panics(t, func() {
  141. MakeExponentialDurationBuckets(2*time.Second, 2, 0)
  142. })
  143. }
  144. func TestMakeExponentialDurationBucketsPanicsOnBadStart(t *testing.T) {
  145. assert.Panics(t, func() {
  146. MakeExponentialDurationBuckets(0, 2, 2)
  147. })
  148. }
  149. func TestMakeExponentialDurationBucketsPanicsOnBadFactor(t *testing.T) {
  150. assert.Panics(t, func() {
  151. MakeExponentialDurationBuckets(2*time.Second, 1, 2)
  152. })
  153. }