benchmark_web_size.py 2.9 KB

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