benchmark_compile_times.py 4.4 KB

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