test_metric.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from tests import unittest
  16. from synapse.metrics.metric import (
  17. CounterMetric, CallbackMetric, DistributionMetric, CacheMetric
  18. )
  19. class CounterMetricTestCase(unittest.TestCase):
  20. def test_scalar(self):
  21. counter = CounterMetric("scalar")
  22. self.assertEquals(counter.render(), [
  23. 'scalar 0',
  24. ])
  25. counter.inc()
  26. self.assertEquals(counter.render(), [
  27. 'scalar 1',
  28. ])
  29. counter.inc_by(2)
  30. self.assertEquals(counter.render(), [
  31. 'scalar 3'
  32. ])
  33. def test_vector(self):
  34. counter = CounterMetric("vector", labels=["method"])
  35. # Empty counter doesn't yet know what values it has
  36. self.assertEquals(counter.render(), [])
  37. counter.inc("GET")
  38. self.assertEquals(counter.render(), [
  39. 'vector{method="GET"} 1',
  40. ])
  41. counter.inc("GET")
  42. counter.inc("PUT")
  43. self.assertEquals(counter.render(), [
  44. 'vector{method="GET"} 2',
  45. 'vector{method="PUT"} 1',
  46. ])
  47. class CallbackMetricTestCase(unittest.TestCase):
  48. def test_scalar(self):
  49. d = dict()
  50. metric = CallbackMetric("size", lambda: len(d))
  51. self.assertEquals(metric.render(), [
  52. 'size 0',
  53. ])
  54. d["key"] = "value"
  55. self.assertEquals(metric.render(), [
  56. 'size 1',
  57. ])
  58. def test_vector(self):
  59. vals = dict()
  60. metric = CallbackMetric("values", lambda: vals, labels=["type"])
  61. self.assertEquals(metric.render(), [])
  62. # Keys have to be tuples, even if they're 1-element
  63. vals[("foo",)] = 1
  64. vals[("bar",)] = 2
  65. self.assertEquals(metric.render(), [
  66. 'values{type="bar"} 2',
  67. 'values{type="foo"} 1',
  68. ])
  69. class DistributionMetricTestCase(unittest.TestCase):
  70. def test_scalar(self):
  71. metric = DistributionMetric("thing")
  72. self.assertEquals(metric.render(), [
  73. 'thing:count 0',
  74. 'thing:total 0',
  75. ])
  76. metric.inc_by(500)
  77. self.assertEquals(metric.render(), [
  78. 'thing:count 1',
  79. 'thing:total 500',
  80. ])
  81. def test_vector(self):
  82. metric = DistributionMetric("queries", labels=["verb"])
  83. self.assertEquals(metric.render(), [])
  84. metric.inc_by(300, "SELECT")
  85. metric.inc_by(200, "SELECT")
  86. metric.inc_by(800, "INSERT")
  87. self.assertEquals(metric.render(), [
  88. 'queries:count{verb="INSERT"} 1',
  89. 'queries:count{verb="SELECT"} 2',
  90. 'queries:total{verb="INSERT"} 800',
  91. 'queries:total{verb="SELECT"} 500',
  92. ])
  93. class CacheMetricTestCase(unittest.TestCase):
  94. def test_cache(self):
  95. d = dict()
  96. metric = CacheMetric("cache", lambda: len(d))
  97. self.assertEquals(metric.render(), [
  98. 'cache:hits 0',
  99. 'cache:total 0',
  100. 'cache:size 0',
  101. ])
  102. metric.inc_misses()
  103. d["key"] = "value"
  104. self.assertEquals(metric.render(), [
  105. 'cache:hits 0',
  106. 'cache:total 1',
  107. 'cache:size 1',
  108. ])
  109. metric.inc_hits()
  110. self.assertEquals(metric.render(), [
  111. 'cache:hits 1',
  112. 'cache:total 2',
  113. 'cache:size 1',
  114. ])