1
0

simple_app_benchmark_upload.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. stats = test.get("stats", {})
  24. test_name = test.get("name", "Unknown Test")
  25. min_value = stats.get("min", None)
  26. max_value = stats.get("max", None)
  27. mean_value = stats.get("mean", None)
  28. stdev_value = stats.get("stddev", None)
  29. test_stats.append(
  30. {
  31. "test_name": test_name,
  32. "min": min_value,
  33. "max": max_value,
  34. "mean": mean_value,
  35. "stdev": stdev_value,
  36. }
  37. )
  38. return test_stats
  39. def insert_benchmarking_data(
  40. db_connection_url: str,
  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. actor: str,
  49. ):
  50. """Insert the benchmarking data into the database.
  51. Args:
  52. db_connection_url: The URL to connect to the database.
  53. os_type_version: The OS type and version to insert.
  54. python_version: The Python version to insert.
  55. performance_data: The performance data of reflex web to insert.
  56. commit_sha: The commit SHA to insert.
  57. pr_title: The PR title to insert.
  58. branch_name: The name of the branch.
  59. event_type: Type of github event(push, pull request, etc)
  60. actor: Username of the user that triggered the run.
  61. """
  62. # Serialize the JSON data
  63. simple_app_performance_json = json.dumps(performance_data)
  64. # Get the current timestamp
  65. current_timestamp = datetime.now()
  66. # Connect to the database and insert the data
  67. with psycopg2.connect(db_connection_url) as conn, conn.cursor() as cursor:
  68. insert_query = """
  69. INSERT INTO simple_app_benchmarks (os, python_version, commit_sha, time, pr_title, branch_name, event_type, actor, performance)
  70. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);
  71. """
  72. cursor.execute(
  73. insert_query,
  74. (
  75. os_type_version,
  76. python_version,
  77. commit_sha,
  78. current_timestamp,
  79. pr_title,
  80. branch_name,
  81. event_type,
  82. actor,
  83. simple_app_performance_json,
  84. ),
  85. )
  86. # Commit the transaction
  87. conn.commit()
  88. def main():
  89. """Runs the benchmarks and inserts the results."""
  90. # Get the commit SHA and JSON directory from the command line arguments
  91. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  92. parser.add_argument(
  93. "--os", help="The OS type and version to insert into the database."
  94. )
  95. parser.add_argument(
  96. "--python-version", help="The Python version to insert into the database."
  97. )
  98. parser.add_argument(
  99. "--commit-sha", help="The commit SHA to insert into the database."
  100. )
  101. parser.add_argument(
  102. "--benchmark-json",
  103. help="The JSON file containing the benchmark results.",
  104. )
  105. parser.add_argument(
  106. "--db-url",
  107. help="The URL to connect to the database.",
  108. required=True,
  109. )
  110. parser.add_argument(
  111. "--pr-title",
  112. help="The PR title to insert into the database.",
  113. )
  114. parser.add_argument(
  115. "--branch-name",
  116. help="The current branch",
  117. required=True,
  118. )
  119. parser.add_argument(
  120. "--event-type",
  121. help="The github event type",
  122. required=True,
  123. )
  124. parser.add_argument(
  125. "--actor",
  126. help="Username of the user that triggered the run.",
  127. required=True,
  128. )
  129. args = parser.parse_args()
  130. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  131. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  132. # Get the results of pytest benchmarks
  133. cleaned_benchmark_results = extract_stats_from_json(args.benchmark_json)
  134. # Insert the data into the database
  135. insert_benchmarking_data(
  136. db_connection_url=args.db_url,
  137. os_type_version=args.os,
  138. python_version=args.python_version,
  139. performance_data=cleaned_benchmark_results,
  140. commit_sha=args.commit_sha,
  141. pr_title=pr_title,
  142. branch_name=args.branch_name,
  143. event_type=args.event_type,
  144. actor=args.actor,
  145. )
  146. if __name__ == "__main__":
  147. main()