benchmark_package_size.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, get_python_version, send_data_to_posthog
  6. def get_package_size(venv_path: Path, os_name):
  7. """Get the size of a specified package.
  8. Args:
  9. venv_path: The path to the venv.
  10. os_name: Name of os.
  11. Returns:
  12. The total size of the package in bytes.
  13. Raises:
  14. ValueError: when venv does not exist or python version is None.
  15. """
  16. python_version = get_python_version(venv_path, os_name)
  17. print("Python version:", python_version) # noqa: T201
  18. if python_version is None:
  19. raise ValueError("Error: Failed to determine Python version.")
  20. is_windows = "windows" in os_name
  21. package_dir: Path = (
  22. venv_path / "lib" / f"python{python_version}" / "site-packages"
  23. if not is_windows
  24. else venv_path / "Lib" / "site-packages"
  25. )
  26. if not package_dir.exists():
  27. raise ValueError(
  28. "Error: Virtual environment does not exist or is not activated."
  29. )
  30. total_size = get_directory_size(package_dir)
  31. return total_size
  32. def insert_benchmarking_data(
  33. os_type_version: str,
  34. python_version: str,
  35. commit_sha: str,
  36. pr_title: str,
  37. branch_name: str,
  38. pr_id: str,
  39. path: str,
  40. ):
  41. """Insert the benchmarking data into PostHog.
  42. Args:
  43. os_type_version: The OS type and version to insert.
  44. python_version: The Python version to insert.
  45. commit_sha: The commit SHA to insert.
  46. pr_title: The PR title to insert.
  47. branch_name: The name of the branch.
  48. pr_id: The id of the PR.
  49. path: The path to the dir or file to check size.
  50. """
  51. if "./dist" in path:
  52. size = get_directory_size(Path(path))
  53. else:
  54. size = get_package_size(Path(path), os_type_version)
  55. # Prepare the event data
  56. properties = {
  57. "path": path,
  58. "os": os_type_version,
  59. "python_version": python_version,
  60. "distinct_id": commit_sha,
  61. "pr_title": pr_title,
  62. "branch_name": branch_name,
  63. "pr_id": pr_id,
  64. "size_mb": round(
  65. size / (1024 * 1024), 3
  66. ), # save size in MB and round to 3 places
  67. }
  68. send_data_to_posthog("package_size", properties)
  69. def main():
  70. """Runs the benchmarks and inserts the results."""
  71. parser = argparse.ArgumentParser(description="Run benchmarks and process results.")
  72. parser.add_argument(
  73. "--os", help="The OS type and version to insert into the database."
  74. )
  75. parser.add_argument(
  76. "--python-version", help="The Python version to insert into the database."
  77. )
  78. parser.add_argument(
  79. "--commit-sha", help="The commit SHA to insert into the database."
  80. )
  81. parser.add_argument(
  82. "--pr-title",
  83. help="The PR title to insert into the database.",
  84. )
  85. parser.add_argument(
  86. "--branch-name",
  87. help="The current branch",
  88. required=True,
  89. )
  90. parser.add_argument(
  91. "--pr-id",
  92. help="The pr id",
  93. required=True,
  94. )
  95. parser.add_argument(
  96. "--path",
  97. help="The path to the vnenv.",
  98. required=True,
  99. )
  100. args = parser.parse_args()
  101. # Get the PR title from env or the args. For the PR merge or push event, there is no PR title, leaving it empty.
  102. pr_title = args.pr_title or os.getenv("PR_TITLE", "")
  103. # Insert the data into the database
  104. insert_benchmarking_data(
  105. os_type_version=args.os,
  106. python_version=args.python_version,
  107. commit_sha=args.commit_sha,
  108. pr_title=pr_title,
  109. branch_name=args.branch_name,
  110. pr_id=args.pr_id,
  111. path=args.path,
  112. )
  113. if __name__ == "__main__":
  114. main()