metrics_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) registerForAllBrokers(broker *Broker, validator *metricValidator) {
  45. m.register(validator)
  46. m.registerForBroker(broker, validator)
  47. }
  48. func (m metricValidators) run(t *testing.T, r metrics.Registry) {
  49. for _, metricValidator := range m {
  50. metric := r.Get(metricValidator.name)
  51. if metric == nil {
  52. t.Error("No metric named", metricValidator.name)
  53. } else {
  54. metricValidator.validator(t, metric)
  55. }
  56. }
  57. }
  58. func meterValidator(name string, extraValidator func(*testing.T, metrics.Meter)) *metricValidator {
  59. return &metricValidator{
  60. name: name,
  61. validator: func(t *testing.T, metric interface{}) {
  62. if meter, ok := metric.(metrics.Meter); !ok {
  63. t.Errorf("Expected meter metric for '%s', got %T", name, metric)
  64. } else {
  65. extraValidator(t, meter)
  66. }
  67. },
  68. }
  69. }
  70. func countMeterValidator(name string, expectedCount int) *metricValidator {
  71. return meterValidator(name, func(t *testing.T, meter metrics.Meter) {
  72. count := meter.Count()
  73. if count != int64(expectedCount) {
  74. t.Errorf("Expected meter metric '%s' count = %d, got %d", name, expectedCount, count)
  75. }
  76. })
  77. }
  78. func minCountMeterValidator(name string, minCount int) *metricValidator {
  79. return meterValidator(name, func(t *testing.T, meter metrics.Meter) {
  80. count := meter.Count()
  81. if count < int64(minCount) {
  82. t.Errorf("Expected meter metric '%s' count >= %d, got %d", name, minCount, count)
  83. }
  84. })
  85. }
  86. func histogramValidator(name string, extraValidator func(*testing.T, metrics.Histogram)) *metricValidator {
  87. return &metricValidator{
  88. name: name,
  89. validator: func(t *testing.T, metric interface{}) {
  90. if histogram, ok := metric.(metrics.Histogram); !ok {
  91. t.Errorf("Expected histogram metric for '%s', got %T", name, metric)
  92. } else {
  93. extraValidator(t, histogram)
  94. }
  95. },
  96. }
  97. }
  98. func countHistogramValidator(name string, expectedCount int) *metricValidator {
  99. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  100. count := histogram.Count()
  101. if count != int64(expectedCount) {
  102. t.Errorf("Expected histogram metric '%s' count = %d, got %d", name, expectedCount, count)
  103. }
  104. })
  105. }
  106. func minCountHistogramValidator(name string, minCount int) *metricValidator {
  107. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  108. count := histogram.Count()
  109. if count < int64(minCount) {
  110. t.Errorf("Expected histogram metric '%s' count >= %d, got %d", name, minCount, count)
  111. }
  112. })
  113. }
  114. func minMaxHistogramValidator(name string, expectedMin int, expectedMax int) *metricValidator {
  115. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  116. min := int(histogram.Min())
  117. if min != expectedMin {
  118. t.Errorf("Expected histogram metric '%s' min = %d, got %d", name, expectedMin, min)
  119. }
  120. max := int(histogram.Max())
  121. if max != expectedMax {
  122. t.Errorf("Expected histogram metric '%s' max = %d, got %d", name, expectedMax, max)
  123. }
  124. })
  125. }
  126. func minValHistogramValidator(name string, minMin int) *metricValidator {
  127. return histogramValidator(name, func(t *testing.T, histogram metrics.Histogram) {
  128. min := int(histogram.Min())
  129. if min < minMin {
  130. t.Errorf("Expected histogram metric '%s' min >= %d, got %d", name, minMin, min)
  131. }
  132. })
  133. }