simple_app_benchmark_upload.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """Runs the benchmarks and inserts the results into the database."""
  2. from __future__ import annotations
  3. import argparse
  4. import json
  5. import os
  6. from datetime import datetime
  7. import psycopg2
  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 open(json_file, "r") 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. db_connection_url: str,
  42. os_type_version: str,
  43. python_version: str,
  44. performance_data: list[dict],
  45. commit_sha: str,
  46. pr_title: str,
  47. branch_name: str,
  48. event_type: str,
  49. actor: str,
  50. pr_id: str,
  51. ):
  52. """Insert the benchmarking data into the database.
  53. Args:
  54. db_connection_url: The URL to connect to the database.
  55. os_type_version: The OS type and version to insert.
  56. python_version: The Python version to insert.
  57. performance_data: The performance data of reflex web to insert.
  58. commit_sha: The commit SHA to insert.
  59. pr_title: The PR title to insert.
  60. branch_name: The name of the branch.
  61. event_type: Type of github event(push, pull request, etc)
  62. actor: Username of the user that triggered the run.
  63. pr_id: Id of the PR.
  64. """
  65. # Serialize the JSON data
  66. simple_app_performance_json = json.dumps(performance_data)
  67. # Get the current timestamp
  68. current_timestamp = datetime.now()
  69. # Connect to the database and insert the data
  70. with psycopg2.connect(db_connection_url) as conn, conn.cursor() as cursor:
  71. insert_query = """
  72. INSERT INTO simple_app_benchmarks (os, python_version, commit_sha, time, pr_title, branch_name, event_type, actor, performance, pr_id)
  73. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  74. """
  75. cursor.execute(
  76. insert_query,
  77. (
  78. os_type_version,
  79. python_version,
  80. commit_sha,
  81. current_timestamp,
  82. pr_title,
  83. branch_name,
  84. event_type,
  85. actor,
  86. simple_app_performance_json,
  87. pr_id,
  88. ),
  89. )
  90. # Commit the transaction
  91. conn.commit()
  92. def main():
  93. """Runs the benchmarks and inserts the results."""
  94. # Get the commit SHA and JSON directory from the command line arguments
  95. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  96. parser.add_argument(
  97. "--os", help="The OS type and version to insert into the database."
  98. )
  99. parser.add_argument(
  100. "--python-version", help="The Python version to insert into the database."
  101. )
  102. parser.add_argument(
  103. "--commit-sha", help="The commit SHA to insert into the database."
  104. )
  105. parser.add_argument(
  106. "--benchmark-json",
  107. help="The JSON file containing the benchmark results.",
  108. )
  109. parser.add_argument(
  110. "--db-url",
  111. help="The URL to connect to the database.",
  112. required=True,
  113. )
  114. parser.add_argument(
  115. "--pr-title",
  116. help="The PR title to insert into the database.",
  117. )
  118. parser.add_argument(
  119. "--branch-name",
  120. help="The current branch",
  121. required=True,
  122. )
  123. parser.add_argument(
  124. "--event-type",
  125. help="The github event type",
  126. required=True,
  127. )
  128. parser.add_argument(
  129. "--actor",
  130. help="Username of the user that triggered the run.",
  131. required=True,
  132. )
  133. parser.add_argument(
  134. "--pr-id",
  135. help="ID of the PR.",
  136. required=True,
  137. )
  138. args = parser.parse_args()
  139. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  140. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  141. # Get the results of pytest benchmarks
  142. cleaned_benchmark_results = extract_stats_from_json(args.benchmark_json)
  143. # Insert the data into the database
  144. insert_benchmarking_data(
  145. db_connection_url=args.db_url,
  146. os_type_version=args.os,
  147. python_version=args.python_version,
  148. performance_data=cleaned_benchmark_results,
  149. commit_sha=args.commit_sha,
  150. pr_title=pr_title,
  151. branch_name=args.branch_name,
  152. event_type=args.event_type,
  153. actor=args.actor,
  154. pr_id=args.pr_id,
  155. )
  156. if __name__ == "__main__":
  157. main()