__main__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. import sys
  16. from contextlib import redirect_stderr
  17. from io import StringIO
  18. import pyperf
  19. from synmark import make_reactor
  20. from synmark.suites import SUITES
  21. from twisted.internet.defer import ensureDeferred
  22. from twisted.logger import globalLogBeginner, textFileLogObserver
  23. from twisted.python.failure import Failure
  24. from tests.utils import setupdb
  25. def make_test(main):
  26. """
  27. Take a benchmark function and wrap it in a reactor start and stop.
  28. """
  29. def _main(loops):
  30. reactor = make_reactor()
  31. file_out = StringIO()
  32. with redirect_stderr(file_out):
  33. d = ensureDeferred(main(reactor, loops))
  34. def on_done(_):
  35. if isinstance(_, Failure):
  36. _.printTraceback()
  37. print(file_out.getvalue())
  38. reactor.stop()
  39. return _
  40. d.addBoth(on_done)
  41. reactor.run()
  42. return d.result
  43. return _main
  44. if __name__ == "__main__":
  45. def add_cmdline_args(cmd, args):
  46. if args.log:
  47. cmd.extend(["--log"])
  48. runner = pyperf.Runner(
  49. processes=3, min_time=2, show_name=True, add_cmdline_args=add_cmdline_args
  50. )
  51. runner.argparser.add_argument("--log", action="store_true")
  52. runner.parse_args()
  53. orig_loops = runner.args.loops
  54. runner.args.inherit_environ = ["SYNAPSE_POSTGRES"]
  55. if runner.args.worker:
  56. if runner.args.log:
  57. globalLogBeginner.beginLoggingTo(
  58. [textFileLogObserver(sys.__stdout__)], redirectStandardIO=False
  59. )
  60. setupdb()
  61. for suite, loops in SUITES:
  62. if loops:
  63. runner.args.loops = loops
  64. else:
  65. runner.args.loops = orig_loops
  66. loops = "auto"
  67. runner.bench_time_func(
  68. suite.__name__ + "_" + str(loops), make_test(suite.main),
  69. )