benchmark_web_size.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Checks the size of a specific directory and uploads result to Posthog."""
  2. import argparse
  3. import os
  4. from utils import get_directory_size, send_data_to_posthog
  5. def insert_benchmarking_data(
  6. os_type_version: str,
  7. python_version: str,
  8. app_name: str,
  9. commit_sha: str,
  10. pr_title: str,
  11. branch_name: str,
  12. pr_id: str,
  13. path: str,
  14. ):
  15. """Insert the benchmarking data into PostHog.
  16. Args:
  17. app_name: The name of the app being measured.
  18. os_type_version: The OS type and version to insert.
  19. python_version: The Python version to insert.
  20. commit_sha: The commit SHA to insert.
  21. pr_title: The PR title to insert.
  22. branch_name: The name of the branch.
  23. pr_id: The id of the PR.
  24. path: The path to the dir or file to check size.
  25. """
  26. size = get_directory_size(path)
  27. # Prepare the event data
  28. properties = {
  29. "app_name": app_name,
  30. "os": os_type_version,
  31. "python_version": python_version,
  32. "distinct_id": commit_sha,
  33. "pr_title": pr_title,
  34. "branch_name": branch_name,
  35. "pr_id": pr_id,
  36. "size_mb": round(
  37. size / (1024 * 1024), 3
  38. ), # save size in MB and round to 3 places
  39. }
  40. send_data_to_posthog("web-size", properties)
  41. def main():
  42. """Runs the benchmarks and inserts the results."""
  43. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  44. parser.add_argument(
  45. "--os", help="The OS type and version to insert into the database."
  46. )
  47. parser.add_argument(
  48. "--python-version", help="The Python version to insert into the database."
  49. )
  50. parser.add_argument(
  51. "--commit-sha", help="The commit SHA to insert into the database."
  52. )
  53. parser.add_argument(
  54. "--pr-title",
  55. help="The PR title to insert into the database.",
  56. )
  57. parser.add_argument(
  58. "--branch-name",
  59. help="The current branch",
  60. required=True,
  61. )
  62. parser.add_argument(
  63. "--app-name",
  64. help="The name of the app measured.",
  65. required=True,
  66. )
  67. parser.add_argument(
  68. "--pr-id",
  69. help="The pr id",
  70. required=True,
  71. )
  72. parser.add_argument(
  73. "--path",
  74. help="The current path to app to check.",
  75. required=True,
  76. )
  77. args = parser.parse_args()
  78. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  79. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  80. # Insert the data into the database
  81. insert_benchmarking_data(
  82. app_name=args.app_name,
  83. os_type_version=args.os,
  84. python_version=args.python_version,
  85. commit_sha=args.commit_sha,
  86. pr_title=pr_title,
  87. branch_name=args.branch_name,
  88. pr_id=args.pr_id,
  89. path=args.path,
  90. )
  91. if __name__ == "__main__":
  92. main()