1
0

test_phone_home.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2019 Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import resource
  15. from unittest import mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.app.phone_stats_home import phone_stats_home
  18. from synapse.rest import admin
  19. from synapse.rest.client import login, sync
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict
  22. from synapse.util import Clock
  23. from tests.unittest import HomeserverTestCase
  24. class PhoneHomeStatsTestCase(HomeserverTestCase):
  25. def test_performance_frozen_clock(self) -> None:
  26. """
  27. If time doesn't move, don't error out.
  28. """
  29. past_stats = [
  30. (self.hs.get_clock().time(), resource.getrusage(resource.RUSAGE_SELF))
  31. ]
  32. stats: JsonDict = {}
  33. self.get_success(phone_stats_home(self.hs, stats, past_stats))
  34. self.assertEqual(stats["cpu_average"], 0)
  35. def test_performance_100(self) -> None:
  36. """
  37. 1 second of usage over 1 second is 100% CPU usage.
  38. """
  39. real_res = resource.getrusage(resource.RUSAGE_SELF)
  40. old_resource = mock.Mock(spec=real_res)
  41. old_resource.ru_utime = real_res.ru_utime - 1
  42. old_resource.ru_stime = real_res.ru_stime
  43. old_resource.ru_maxrss = real_res.ru_maxrss
  44. past_stats = [(self.hs.get_clock().time(), old_resource)]
  45. stats: JsonDict = {}
  46. self.reactor.advance(1)
  47. # `old_resource` has type `Mock` instead of `struct_rusage`
  48. self.get_success(
  49. phone_stats_home(self.hs, stats, past_stats) # type: ignore[arg-type]
  50. )
  51. self.assertApproximates(stats["cpu_average"], 100, tolerance=2.5)
  52. class CommonMetricsTestCase(HomeserverTestCase):
  53. servlets = [
  54. admin.register_servlets,
  55. login.register_servlets,
  56. sync.register_servlets,
  57. ]
  58. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  59. self.metrics_manager = hs.get_common_usage_metrics_manager()
  60. self.get_success(self.metrics_manager.setup())
  61. def test_dau(self) -> None:
  62. """Tests that the daily active users count is correctly updated."""
  63. self._assert_metric_value("daily_active_users", 0)
  64. self.register_user("user", "password")
  65. tok = self.login("user", "password")
  66. self.make_request("GET", "/sync", access_token=tok)
  67. self.pump(1)
  68. self._assert_metric_value("daily_active_users", 1)
  69. def _assert_metric_value(self, metric_name: str, expected: int) -> None:
  70. """Compare the given value to the current value of the common usage metric with
  71. the given name.
  72. Args:
  73. metric_name: The metric to look up.
  74. expected: Expected value for this metric.
  75. """
  76. metrics = self.get_success(self.metrics_manager.get_metrics())
  77. value = getattr(metrics, metric_name)
  78. self.assertEqual(value, expected)