benchmark_imports.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Extract and upload benchmarking data to PostHog."""
  2. from __future__ import annotations
  3. import argparse
  4. import json
  5. import os
  6. from pathlib import Path
  7. from utils import send_data_to_posthog
  8. def extract_stats_from_json(json_file: str) -> dict:
  9. """Extracts the stats from the JSON data and returns them as dictionaries.
  10. Args:
  11. json_file: The JSON file to extract the stats data from.
  12. Returns:
  13. dict: The stats for each test.
  14. """
  15. with Path(json_file).open() as file:
  16. json_data = json.load(file)
  17. # Load the JSON data if it is a string, otherwise assume it's already a dictionary
  18. data = json.loads(json_data) if isinstance(json_data, str) else json_data
  19. result = data.get("results", [{}])[0]
  20. return {
  21. k: v
  22. for k, v in result.items()
  23. if k in ("mean", "stddev", "median", "min", "max")
  24. }
  25. def insert_benchmarking_data(
  26. os_type_version: str,
  27. python_version: str,
  28. performance_data: dict,
  29. commit_sha: str,
  30. pr_title: str,
  31. branch_name: str,
  32. pr_id: str,
  33. app_name: str,
  34. ):
  35. """Insert the benchmarking data into the database.
  36. Args:
  37. os_type_version: The OS type and version to insert.
  38. python_version: The Python version to insert.
  39. performance_data: The imports performance data to insert.
  40. commit_sha: The commit SHA to insert.
  41. pr_title: The PR title to insert.
  42. branch_name: The name of the branch.
  43. pr_id: Id of the PR.
  44. app_name: The name of the app being measured.
  45. """
  46. properties = {
  47. "os": os_type_version,
  48. "python_version": python_version,
  49. "distinct_id": commit_sha,
  50. "pr_title": pr_title,
  51. "branch_name": branch_name,
  52. "pr_id": pr_id,
  53. "performance": performance_data,
  54. "app_name": app_name,
  55. }
  56. send_data_to_posthog("import_benchmark", properties)
  57. def main():
  58. """Runs the benchmarks and inserts the results."""
  59. # Get the commit SHA and JSON directory from the command line arguments
  60. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  61. parser.add_argument(
  62. "--os", help="The OS type and version to insert into the database."
  63. )
  64. parser.add_argument(
  65. "--python-version", help="The Python version to insert into the database."
  66. )
  67. parser.add_argument(
  68. "--commit-sha", help="The commit SHA to insert into the database."
  69. )
  70. parser.add_argument(
  71. "--benchmark-json",
  72. help="The JSON file containing the benchmark results.",
  73. )
  74. parser.add_argument(
  75. "--pr-title",
  76. help="The PR title to insert into the database.",
  77. )
  78. parser.add_argument(
  79. "--branch-name",
  80. help="The current branch",
  81. required=True,
  82. )
  83. parser.add_argument(
  84. "--app-name",
  85. help="The name of the app measured.",
  86. required=True,
  87. )
  88. parser.add_argument(
  89. "--pr-id",
  90. help="ID of the PR.",
  91. required=True,
  92. )
  93. args = parser.parse_args()
  94. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  95. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  96. cleaned_benchmark_results = extract_stats_from_json(args.benchmark_json)
  97. # Insert the data into the database
  98. insert_benchmarking_data(
  99. os_type_version=args.os,
  100. python_version=args.python_version,
  101. performance_data=cleaned_benchmark_results,
  102. commit_sha=args.commit_sha,
  103. pr_title=pr_title,
  104. branch_name=args.branch_name,
  105. app_name=args.app_name,
  106. pr_id=args.pr_id,
  107. )
  108. if __name__ == "__main__":
  109. main()