metrics_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package sarama
  2. import (
  3. "testing"
  4. "github.com/rcrowley/go-metrics"
  5. )
  6. func TestGetOrRegisterHistogram(t *testing.T) {
  7. metricRegistry := metrics.NewRegistry()
  8. histogram := getOrRegisterHistogram("name", metricRegistry)
  9. if histogram == nil {
  10. t.Error("Unexpected nil histogram")
  11. }
  12. // Fetch the metric
  13. foundHistogram := metricRegistry.Get("name")
  14. if foundHistogram != histogram {
  15. t.Error("Unexpected different histogram", foundHistogram, histogram)
  16. }
  17. // Try to register the metric again
  18. sameHistogram := getOrRegisterHistogram("name", metricRegistry)
  19. if sameHistogram != histogram {
  20. t.Error("Unexpected different histogram", sameHistogram, histogram)
  21. }
  22. }
  23. func TestGetMetricNameForBroker(t *testing.T) {
  24. metricName := getMetricNameForBroker("name", &Broker{id: 1})
  25. if metricName != "name-for-broker-1" {
  26. t.Error("Unexpected metric name", metricName)
  27. }
  28. }
  29. // Common type and functions for metric validation
  30. type metricValidator struct {
  31. name string
  32. validator func(*testing.T, interface{})
  33. }
  34. type metricValidators []*metricValidator
  35. func newMetricValidators() metricValidators {
  36. return make([]*metricValidator, 0, 32)
  37. }
  38. func (m *metricValidators) register(validator *metricValidator) {
  39. *m = append(*m, validator)
  40. }
  41. func (m *metricValidators) registerForBroker(broker *Broker, validator *metricValidator) {
  42. m.register(&metricValidator{getMetricNameForBroker(validator.name, broker), validator.validator})
  43. }
  44. func (m *metricValidators) registerForGlobalAndTopic(topic string, validator *metricValidator) {
  45. m.register(&metricValidator{validator.name, validator.validator})
  46. m.register(&metricValidator{getMetricNameForTopic(validator.name, topic), validator.validator})
  47. }
  48. func (m *metricValidators) registerForAllBrokers(broker *Broker, validator *metricValidator) {
  49. m.register(validator)
  50. m.registerForBroker(broker, validator)
  51. }
  52. func (m metricValidators) run(t *testing.T, r metrics.Registry) {
  53. for _, metricValidator := range m {
  54. metric := r.Get(metricValidator.name)
  55. if metric == nil {
  56. t.Error("No metric named", metricValidator.name)
  57. } else {
  58. metricValidator.validator(t, metric)
  59. }
  60. }
  61. }
  62. func meterValidator(name string, extraValidator func(*testing.T, metrics.Meter)) *metricValidator {
  63. return &metricValidator{
  64. name: name,
  65. validator: func(t *testing.T, metric interface{}) {
  66. if meter, ok := metric.(metrics.Meter); !ok {
  67. t.Errorf("Expected meter metric for '%s', got %T", name, metric)
  68. } else {
  69. extraValidator(t, meter)
  70. }
  71. },
  72. }
  73. }
  74. func countMeterValidator(name string, expectedCount int) *metricValidator {
  75. return meterValidator(name, func(t *testing.T, meter metrics.Meter) {
  76. count := meter.Count()
  77. if count != int64(expectedCount) {
  78. t.Errorf("Expected meter metric '%s' count = %d, got %d", name, expectedCount, count)
  79. }
  80. })
  81. }
  82. func minCountMeterValidator(name string, minCount int) *metricValidator {
  83. return meterValidator(name, func(t *testing.T, meter metrics.Meter) {
  84. count := meter.Count()
  85. if count < int64(minCount) {
  86. t.Errorf("Expected meter metric '%s' count >= %d, got %d", name, minCount, count)
  87. }
  88. })
  89. }
  90. func histogramValidator(name string, extraValidator func(*testing.T, metrics.Histogram)) *metricValidator {
  91. return &metricValidator{
  92. name: name,
  93. validator: func(t *testing.T, metric interface{}) {
  94. if histogram, ok := metric.(metrics.Histogram); !ok {
  95. t.Errorf("Expected histogram metric for '%s', got %T", name, metric)
  96. } else {
  97. extraValidator(t, histogram)
  98. }
  99. },
  100. }
  101. }
  102. func countHistogramValidator(name string, expectedCount int) *metricValidator {
  103. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  104. count := histogram.Count()
  105. if count != int64(expectedCount) {
  106. t.Errorf("Expected histogram metric '%s' count = %d, got %d", name, expectedCount, count)
  107. }
  108. })
  109. }
  110. func minCountHistogramValidator(name string, minCount int) *metricValidator {
  111. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  112. count := histogram.Count()
  113. if count < int64(minCount) {
  114. t.Errorf("Expected histogram metric '%s' count >= %d, got %d", name, minCount, count)
  115. }
  116. })
  117. }
  118. func minMaxHistogramValidator(name string, expectedMin int, expectedMax int) *metricValidator {
  119. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  120. min := int(histogram.Min())
  121. if min != expectedMin {
  122. t.Errorf("Expected histogram metric '%s' min = %d, got %d", name, expectedMin, min)
  123. }
  124. max := int(histogram.Max())
  125. if max != expectedMax {
  126. t.Errorf("Expected histogram metric '%s' max = %d, got %d", name, expectedMax, max)
  127. }
  128. })
  129. }
  130. func minValHistogramValidator(name string, minMin int) *metricValidator {
  131. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  132. min := int(histogram.Min())
  133. if min < minMin {
  134. t.Errorf("Expected histogram metric '%s' min >= %d, got %d", name, minMin, min)
  135. }
  136. })
  137. }
  138. func maxValHistogramValidator(name string, maxMax int) *metricValidator {
  139. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  140. max := int(histogram.Max())
  141. if max > maxMax {
  142. t.Errorf("Expected histogram metric '%s' max <= %d, got %d", name, maxMax, max)
  143. }
  144. })
  145. }
  146. func counterValidator(name string, expectedCount int) *metricValidator {
  147. return &metricValidator{
  148. name: name,
  149. validator: func(t *testing.T, metric interface{}) {
  150. if counter, ok := metric.(metrics.Counter); !ok {
  151. t.Errorf("Expected counter metric for '%s', got %T", name, metric)
  152. } else {
  153. count := counter.Count()
  154. if count != int64(expectedCount) {
  155. t.Errorf("Expected counter metric '%s' count = %d, got %d", name, expectedCount, count)
  156. }
  157. }
  158. },
  159. }
  160. }