benchmark_compile_times.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Extracts the compile times from the JSON files in the specified directory and inserts them into the database."""
  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) -> list[dict]:
  9. """Extracts the stats from the JSON data and returns them as a list of dictionaries.
  10. Args:
  11. json_file: The JSON file to extract the stats data from.
  12. Returns:
  13. list[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. # Initialize an empty list to store the stats for each test
  20. test_stats = []
  21. # Iterate over each test in the 'benchmarks' list
  22. for test in data.get("benchmarks", []):
  23. group = test.get("group", None)
  24. stats = test.get("stats", {})
  25. full_name = test.get("fullname")
  26. file_name = (
  27. full_name.split("/")[-1].split("::")[0].strip(".py") if full_name else None
  28. )
  29. test_name = test.get("name", "Unknown Test")
  30. test_stats.append(
  31. {
  32. "test_name": test_name,
  33. "group": group,
  34. "stats": stats,
  35. "full_name": full_name,
  36. "file_name": file_name,
  37. }
  38. )
  39. return test_stats
  40. def insert_benchmarking_data(
  41. os_type_version: str,
  42. python_version: str,
  43. performance_data: list[dict],
  44. commit_sha: str,
  45. pr_title: str,
  46. branch_name: str,
  47. event_type: str,
  48. pr_id: str,
  49. ):
  50. """Insert the benchmarking data into the database.
  51. Args:
  52. os_type_version: The OS type and version to insert.
  53. python_version: The Python version to insert.
  54. performance_data: The performance data of reflex web to insert.
  55. commit_sha: The commit SHA to insert.
  56. pr_title: The PR title to insert.
  57. branch_name: The name of the branch.
  58. event_type: Type of github event(push, pull request, etc).
  59. pr_id: Id of the PR.
  60. """
  61. # Prepare the event data
  62. properties = {
  63. "os": os_type_version,
  64. "python_version": python_version,
  65. "distinct_id": commit_sha,
  66. "pr_title": pr_title,
  67. "branch_name": branch_name,
  68. "event_type": event_type,
  69. "performance": performance_data,
  70. "pr_id": pr_id,
  71. }
  72. send_data_to_posthog("simple_app_benchmark", properties)
  73. def main():
  74. """Runs the benchmarks and inserts the results."""
  75. # Get the commit SHA and JSON directory from the command line arguments
  76. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  77. parser.add_argument(
  78. "--os", help="The OS type and version to insert into the database."
  79. )
  80. parser.add_argument(
  81. "--python-version", help="The Python version to insert into the database."
  82. )
  83. parser.add_argument(
  84. "--commit-sha", help="The commit SHA to insert into the database."
  85. )
  86. parser.add_argument(
  87. "--benchmark-json",
  88. help="The JSON file containing the benchmark results.",
  89. )
  90. parser.add_argument(
  91. "--pr-title",
  92. help="The PR title to insert into the database.",
  93. )
  94. parser.add_argument(
  95. "--branch-name",
  96. help="The current branch",
  97. required=True,
  98. )
  99. parser.add_argument(
  100. "--event-type",
  101. help="The github event type",
  102. required=True,
  103. )
  104. parser.add_argument(
  105. "--pr-id",
  106. help="ID of the PR.",
  107. required=True,
  108. )
  109. args = parser.parse_args()
  110. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  111. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  112. # Get the results of pytest benchmarks
  113. cleaned_benchmark_results = extract_stats_from_json(args.benchmark_json)
  114. # Insert the data into the database
  115. insert_benchmarking_data(
  116. os_type_version=args.os,
  117. python_version=args.python_version,
  118. performance_data=cleaned_benchmark_results,
  119. commit_sha=args.commit_sha,
  120. pr_title=pr_title,
  121. branch_name=args.branch_name,
  122. event_type=args.event_type,
  123. pr_id=args.pr_id,
  124. )
  125. if __name__ == "__main__":
  126. main()