histogram_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package metrics
  2. import "testing"
  3. func BenchmarkHistogram(b *testing.B) {
  4. h := NewHistogram(NewUniformSample(100))
  5. b.ResetTimer()
  6. for i := 0; i < b.N; i++ {
  7. h.Update(int64(i))
  8. }
  9. }
  10. func TestGetOrRegisterHistogram(t *testing.T) {
  11. r := NewRegistry()
  12. s := NewUniformSample(100)
  13. GetOrRegisterHistogram("foo", r, s).Update(47)
  14. if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
  15. t.Fatal(h)
  16. }
  17. }
  18. func TestHistogram10000(t *testing.T) {
  19. h := NewHistogram(NewUniformSample(100000))
  20. for i := 1; i <= 10000; i++ {
  21. h.Update(int64(i))
  22. }
  23. if count := h.Count(); 10000 != count {
  24. t.Errorf("h.Count(): 10000 != %v\n", count)
  25. }
  26. if min := h.Min(); 1 != min {
  27. t.Errorf("h.Min(): 1 != %v\n", min)
  28. }
  29. if max := h.Max(); 10000 != max {
  30. t.Errorf("h.Max(): 10000 != %v\n", max)
  31. }
  32. if mean := h.Mean(); 5000.5 != mean {
  33. t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
  34. }
  35. if stdDev := h.StdDev(); 2886.8956799071675 != stdDev {
  36. t.Errorf("h.StdDev(): 2886.8956799071675 != %v\n", stdDev)
  37. }
  38. ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
  39. if 5000.5 != ps[0] {
  40. t.Errorf("median: 5000.5 != %v\n", ps[0])
  41. }
  42. if 7500.75 != ps[1] {
  43. t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
  44. }
  45. if 9900.99 != ps[2] {
  46. t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
  47. }
  48. }
  49. func TestHistogramEmpty(t *testing.T) {
  50. h := NewHistogram(NewUniformSample(100))
  51. if count := h.Count(); 0 != count {
  52. t.Errorf("h.Count(): 0 != %v\n", count)
  53. }
  54. if min := h.Min(); 0 != min {
  55. t.Errorf("h.Min(): 0 != %v\n", min)
  56. }
  57. if max := h.Max(); 0 != max {
  58. t.Errorf("h.Max(): 0 != %v\n", max)
  59. }
  60. if mean := h.Mean(); 0.0 != mean {
  61. t.Errorf("h.Mean(): 0.0 != %v\n", mean)
  62. }
  63. if stdDev := h.StdDev(); 0.0 != stdDev {
  64. t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
  65. }
  66. ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
  67. if 0.0 != ps[0] {
  68. t.Errorf("median: 0.0 != %v\n", ps[0])
  69. }
  70. if 0.0 != ps[1] {
  71. t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
  72. }
  73. if 0.0 != ps[2] {
  74. t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
  75. }
  76. }