timer_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package metrics
  2. import (
  3. "fmt"
  4. "math"
  5. "testing"
  6. "time"
  7. )
  8. func BenchmarkTimer(b *testing.B) {
  9. tm := NewTimer()
  10. b.ResetTimer()
  11. for i := 0; i < b.N; i++ {
  12. tm.Update(1)
  13. }
  14. }
  15. func TestGetOrRegisterTimer(t *testing.T) {
  16. r := NewRegistry()
  17. NewRegisteredTimer("foo", r).Update(47)
  18. if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() {
  19. t.Fatal(tm)
  20. }
  21. }
  22. func TestTimerExtremes(t *testing.T) {
  23. tm := NewTimer()
  24. tm.Update(math.MaxInt64)
  25. tm.Update(0)
  26. if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev {
  27. t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev)
  28. }
  29. }
  30. func TestTimerStop(t *testing.T) {
  31. l := len(arbiter.meters)
  32. tm := NewTimer()
  33. if len(arbiter.meters) != l+1 {
  34. t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
  35. }
  36. tm.Stop()
  37. if len(arbiter.meters) != l {
  38. t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
  39. }
  40. }
  41. func TestTimerFunc(t *testing.T) {
  42. tm := NewTimer()
  43. tm.Time(func() { time.Sleep(50e6) })
  44. if max := tm.Max(); 35e6 > max || max > 95e6 {
  45. t.Errorf("tm.Max(): 35e6 > %v || %v > 95e6\n", max, max)
  46. }
  47. }
  48. func TestTimerZero(t *testing.T) {
  49. tm := NewTimer()
  50. if count := tm.Count(); 0 != count {
  51. t.Errorf("tm.Count(): 0 != %v\n", count)
  52. }
  53. if min := tm.Min(); 0 != min {
  54. t.Errorf("tm.Min(): 0 != %v\n", min)
  55. }
  56. if max := tm.Max(); 0 != max {
  57. t.Errorf("tm.Max(): 0 != %v\n", max)
  58. }
  59. if mean := tm.Mean(); 0.0 != mean {
  60. t.Errorf("tm.Mean(): 0.0 != %v\n", mean)
  61. }
  62. if stdDev := tm.StdDev(); 0.0 != stdDev {
  63. t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev)
  64. }
  65. ps := tm.Percentiles([]float64{0.5, 0.75, 0.99})
  66. if 0.0 != ps[0] {
  67. t.Errorf("median: 0.0 != %v\n", ps[0])
  68. }
  69. if 0.0 != ps[1] {
  70. t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
  71. }
  72. if 0.0 != ps[2] {
  73. t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
  74. }
  75. if rate1 := tm.Rate1(); 0.0 != rate1 {
  76. t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1)
  77. }
  78. if rate5 := tm.Rate5(); 0.0 != rate5 {
  79. t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5)
  80. }
  81. if rate15 := tm.Rate15(); 0.0 != rate15 {
  82. t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15)
  83. }
  84. if rateMean := tm.RateMean(); 0.0 != rateMean {
  85. t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean)
  86. }
  87. }
  88. func ExampleGetOrRegisterTimer() {
  89. m := "account.create.latency"
  90. t := GetOrRegisterTimer(m, nil)
  91. t.Update(47)
  92. fmt.Println(t.Max()) // Output: 47
  93. }