test_phone_home.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 synapse.app.phone_stats_home import phone_stats_home
  17. from tests.unittest import HomeserverTestCase
  18. class PhoneHomeStatsTestCase(HomeserverTestCase):
  19. def test_performance_frozen_clock(self):
  20. """
  21. If time doesn't move, don't error out.
  22. """
  23. past_stats = [
  24. (self.hs.get_clock().time(), resource.getrusage(resource.RUSAGE_SELF))
  25. ]
  26. stats = {}
  27. self.get_success(phone_stats_home(self.hs, stats, past_stats))
  28. self.assertEqual(stats["cpu_average"], 0)
  29. def test_performance_100(self):
  30. """
  31. 1 second of usage over 1 second is 100% CPU usage.
  32. """
  33. real_res = resource.getrusage(resource.RUSAGE_SELF)
  34. old_resource = mock.Mock(spec=real_res)
  35. old_resource.ru_utime = real_res.ru_utime - 1
  36. old_resource.ru_stime = real_res.ru_stime
  37. old_resource.ru_maxrss = real_res.ru_maxrss
  38. past_stats = [(self.hs.get_clock().time(), old_resource)]
  39. stats = {}
  40. self.reactor.advance(1)
  41. self.get_success(phone_stats_home(self.hs, stats, past_stats))
  42. self.assertApproximates(stats["cpu_average"], 100, tolerance=2.5)