benchmark 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. # isort: skip_file
  3. """
  4. This script benchmarks the performance of issue owner assignment in Sentry.
  5. Usage: python benchmark_codeowners/benchmark <path_to_code_mapping_file> <path_to_event_data_file>
  6. """
  7. from sentry.runner import configure
  8. configure()
  9. import sys
  10. import random
  11. import string
  12. import time
  13. from sentry.models.organization import Organization
  14. from sentry.models.projectownership import ProjectOwnership
  15. from sentry.models.project import Project
  16. from sentry.utils import json
  17. def main(code_mapping_file, event_data_file):
  18. def get_code_mapping():
  19. with open(code_mapping_file) as f:
  20. return json.loads(f.read())
  21. def get_event_data():
  22. with open(event_data_file) as f:
  23. return json.loads(f.read())
  24. code_mapping = get_code_mapping()
  25. # create an organization
  26. org_id = random.randint(1, 1000000)
  27. org_name = "".join(random.choices(string.ascii_letters + string.digits, k=10))
  28. org_slug = "".join(random.choices(string.ascii_lowercase + string.digits, k=10))
  29. org, _ = Organization.objects.get_or_create(name=org_name, slug=org_slug, id=org_id)
  30. # create a project
  31. project_id = random.randint(1, 1000000)
  32. project_name = "".join(random.choices(string.ascii_letters + string.digits, k=10))
  33. project_slug = "".join(random.choices(string.ascii_lowercase + string.digits, k=10))
  34. project, _ = Project.objects.get_or_create(
  35. name=project_name, slug=project_slug, id=project_id, organization_id=org.id
  36. )
  37. # create a projectownership
  38. ProjectOwnership.objects.get_or_create(
  39. project_id=project.id,
  40. schema=code_mapping,
  41. )
  42. event_data = get_event_data()
  43. start = time.time()
  44. ProjectOwnership.get_issue_owners(project.id, event_data)
  45. elapsed_time = time.time() - start
  46. print(f"Time taken: {elapsed_time:.6f} seconds") # noqa
  47. if __name__ == "__main__":
  48. if len(sys.argv) != 3:
  49. print( # noqa
  50. "Usage: python benchmark_codeowners/benchmark <path_to_code_mapping_file> <path_to_event_data_file>"
  51. )
  52. sys.exit(1)
  53. main(sys.argv[1], sys.argv[2])