1
0

benchmark_imports.py 3.6 KB

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