benchmark_detectors 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. # isort: skip_file
  3. """
  4. This script benchmarks the performance of issue detectors in Sentry.
  5. NOTE: This currently only supports FileIOMainThreadDetector.
  6. Usage: python benchmark_detectors
  7. """
  8. from sentry.runner import configure
  9. configure()
  10. import time
  11. import sentry_sdk
  12. from sentry.testutils.performance_issues.event_generators import get_event # noqa: S007
  13. from sentry.utils.performance_issues.detectors import FileIOMainThreadDetector
  14. from sentry.utils.performance_issues.performance_detection import (
  15. get_detection_settings,
  16. run_detector_on_data,
  17. )
  18. sentry_sdk.init(None)
  19. def main():
  20. settings = get_detection_settings()
  21. # 10 events: 1 ignored, 1 matching, and 8 ignored
  22. events = [get_event("file-io-on-main-thread") for _ in range(0, 10)]
  23. events[0]["spans"][0]["data"]["file.path"] = "somethins/stuff/blah/yup/KBLayout_iPhone.dat"
  24. for i in range(2, 10):
  25. events[i]["spans"][0]["data"]["blocked_main_thread"] = False
  26. count = 100_000
  27. start = time.perf_counter()
  28. for _ in range(0, count):
  29. for event in events:
  30. detector = FileIOMainThreadDetector(settings, event)
  31. run_detector_on_data(detector, event)
  32. elapsed = time.perf_counter() - start
  33. ops = count * len(events)
  34. print(f"{ops:,} ops") # noqa
  35. print(f"{elapsed:.3f} s") # noqa
  36. print(f"{ops/elapsed:,.2f} ops/s") # noqa
  37. if __name__ == "__main__":
  38. main()