benchmark_detectors 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.io_main_thread_detector import (
  14. FileIOMainThreadDetector,
  15. )
  16. from sentry.utils.performance_issues.performance_detection import (
  17. get_detection_settings,
  18. run_detector_on_data,
  19. )
  20. sentry_sdk.init(None)
  21. def main():
  22. settings = get_detection_settings()
  23. # 10 events: 1 ignored, 1 matching, and 8 ignored
  24. events = [get_event("file-io-on-main-thread") for _ in range(0, 10)]
  25. events[0]["spans"][0]["data"]["file.path"] = "somethins/stuff/blah/yup/KBLayout_iPhone.dat"
  26. for i in range(2, 10):
  27. events[i]["spans"][0]["data"]["blocked_main_thread"] = False
  28. count = 100_000
  29. start = time.perf_counter()
  30. for _ in range(0, count):
  31. for event in events:
  32. detector = FileIOMainThreadDetector(settings, event)
  33. run_detector_on_data(detector, event)
  34. elapsed = time.perf_counter() - start
  35. ops = count * len(events)
  36. print(f"{ops:,} ops") # noqa
  37. print(f"{elapsed:.3f} s") # noqa
  38. print(f"{ops/elapsed:,.2f} ops/s") # noqa
  39. if __name__ == "__main__":
  40. main()