simple_app_benchmark_upload.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Runs the benchmarks and inserts the results into the database."""
  2. from __future__ import annotations
  3. import argparse
  4. import json
  5. from datetime import datetime
  6. import psycopg2
  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. stats = test.get("stats", {})
  23. test_name = test.get("name", "Unknown Test")
  24. min_value = stats.get("min", None)
  25. max_value = stats.get("max", None)
  26. mean_value = stats.get("mean", None)
  27. stdev_value = stats.get("stddev", None)
  28. test_stats.append(
  29. {
  30. "test_name": test_name,
  31. "min": min_value,
  32. "max": max_value,
  33. "mean": mean_value,
  34. "stdev": stdev_value,
  35. }
  36. )
  37. return test_stats
  38. def insert_benchmarking_data(
  39. db_connection_url: str,
  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. actor: str,
  48. ):
  49. """Insert the benchmarking data into the database.
  50. Args:
  51. db_connection_url: The URL to connect to the database.
  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. actor: Username of the user that triggered the run.
  60. """
  61. # Serialize the JSON data
  62. simple_app_performance_json = json.dumps(performance_data)
  63. # Get the current timestamp
  64. current_timestamp = datetime.now()
  65. # Connect to the database and insert the data
  66. with psycopg2.connect(db_connection_url) as conn, conn.cursor() as cursor:
  67. insert_query = """
  68. INSERT INTO simple_app_benchmarks (os, python_version, commit_sha, time, pr_title, branch_name, event_type, actor, performance)
  69. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);
  70. """
  71. cursor.execute(
  72. insert_query,
  73. (
  74. os_type_version,
  75. python_version,
  76. commit_sha,
  77. current_timestamp,
  78. pr_title,
  79. branch_name,
  80. event_type,
  81. actor,
  82. simple_app_performance_json,
  83. ),
  84. )
  85. # Commit the transaction
  86. conn.commit()
  87. def main():
  88. """Runs the benchmarks and inserts the results."""
  89. # Get the commit SHA and JSON directory from the command line arguments
  90. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  91. parser.add_argument(
  92. "--os", help="The OS type and version to insert into the database."
  93. )
  94. parser.add_argument(
  95. "--python-version", help="The Python version to insert into the database."
  96. )
  97. parser.add_argument(
  98. "--commit-sha", help="The commit SHA to insert into the database."
  99. )
  100. parser.add_argument(
  101. "--benchmark-json",
  102. help="The JSON file containing the benchmark results.",
  103. )
  104. parser.add_argument(
  105. "--db-url",
  106. help="The URL to connect to the database.",
  107. required=True,
  108. )
  109. parser.add_argument(
  110. "--pr-title",
  111. help="The PR title to insert into the database.",
  112. required=True,
  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 results of pytest benchmarks
  131. cleaned_benchmark_results = extract_stats_from_json(args.benchmark_json)
  132. # Insert the data into the database
  133. insert_benchmarking_data(
  134. db_connection_url=args.db_url,
  135. os_type_version=args.os,
  136. python_version=args.python_version,
  137. performance_data=cleaned_benchmark_results,
  138. commit_sha=args.commit_sha,
  139. pr_title=args.pr_title,
  140. branch_name=args.branch_name,
  141. event_type=args.event_type,
  142. actor=args.actor,
  143. )
  144. if __name__ == "__main__":
  145. main()