|
1 | 1 | import argparse
|
2 | 2 | import json
|
3 | 3 | import os
|
4 |
| -import shutil |
| 4 | +import time |
5 | 5 |
|
6 | 6 | from selenium import webdriver
|
7 | 7 | from selenium.common.exceptions import TimeoutException
|
8 |
| -from selenium.webdriver.chrome.service import Service |
9 | 8 | from selenium.webdriver.common.by import By
|
10 | 9 | from selenium.webdriver.support import expected_conditions as EC
|
11 | 10 | from selenium.webdriver.support.ui import WebDriverWait
|
12 | 11 |
|
13 | 12 | from dyana import Profiler # type: ignore[attr-defined]
|
14 | 13 |
|
| 14 | +CHROMIUM_BROWSER_PATH = "/usr/bin/chromium-browser" |
| 15 | +CHROMIUM_DRIVER_PATH = "/usr/lib/chromium/chromedriver" |
| 16 | + |
| 17 | + |
| 18 | +def setup_chrome_options(performance_log: bool) -> webdriver.ChromeOptions: |
| 19 | + chrome_options = webdriver.ChromeOptions() |
| 20 | + |
| 21 | + chrome_options.add_argument("--window-size=1920,1080") |
| 22 | + chrome_options.add_argument("--headless=new") |
| 23 | + chrome_options.add_argument("--no-sandbox") |
| 24 | + chrome_options.add_argument("--disable-dev-shm-usage") |
| 25 | + chrome_options.add_argument("--disable-gpu") |
| 26 | + |
| 27 | + # disable Google services and non-critical features that can cause hangs |
| 28 | + chrome_options.add_argument("--disable-sync") |
| 29 | + chrome_options.add_argument("--disable-extensions") |
| 30 | + chrome_options.add_argument("--disable-background-networking") |
| 31 | + chrome_options.add_argument("--disable-domain-reliability") |
| 32 | + chrome_options.add_argument("--disable-client-side-phishing-detection") |
| 33 | + chrome_options.add_argument("--disable-component-update") |
| 34 | + chrome_options.binary_location = CHROMIUM_BROWSER_PATH |
| 35 | + |
| 36 | + # force DNS lookups for each request |
| 37 | + chrome_options.add_argument("--dns-prefetch-disable") |
| 38 | + chrome_options.add_argument("--disable-http-cache") |
| 39 | + chrome_options.add_argument("--disable-browser-side-navigation") |
| 40 | + chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"]) |
| 41 | + |
| 42 | + if performance_log: |
| 43 | + # network logging prefs |
| 44 | + chrome_options.set_capability("goog:loggingPrefs", {"performance": "ALL", "browser": "ALL", "network": "ALL"}) |
| 45 | + |
| 46 | + return chrome_options |
| 47 | + |
| 48 | + |
15 | 49 | if __name__ == "__main__":
|
16 | 50 | parser = argparse.ArgumentParser(description="Profile website performance")
|
17 | 51 | parser.add_argument("--url", help="URL to open", required=True)
|
|
23 | 57 | parser.add_argument("--performance-log", help="Enable performance logging", action="store_true")
|
24 | 58 | args = parser.parse_args()
|
25 | 59 |
|
26 |
| - # Normalize URL by adding https:// if protocol is missing |
| 60 | + # normalize URL - https:// if protocol is missing |
27 | 61 | if "://" not in args.url:
|
28 | 62 | args.url = f"https://{args.url}"
|
29 | 63 |
|
30 | 64 | profiler: Profiler = Profiler()
|
| 65 | + driver: webdriver.Chrome | None = None |
31 | 66 |
|
32 | 67 | try:
|
33 |
| - chrome_options = webdriver.ChromeOptions() |
34 |
| - chrome_options.add_argument("--no-sandbox") |
35 |
| - chrome_options.add_argument("--headless=new") |
36 |
| - chrome_options.add_argument("--disable-dev-shm-usage") |
37 |
| - chrome_options.add_argument("--window-size=1920,1080") |
38 |
| - |
39 |
| - if args.performance_log: |
40 |
| - chrome_options.set_capability("goog:loggingPrefs", {"performance": "ALL"}) |
41 |
| - |
42 |
| - service = Service(executable_path=shutil.which("chromedriver")) |
43 |
| - service.start() |
44 |
| - |
| 68 | + chrome_options = setup_chrome_options(args.performance_log) |
| 69 | + service = webdriver.ChromeService(executable_path=CHROMIUM_DRIVER_PATH) |
45 | 70 | driver = webdriver.Chrome(options=chrome_options, service=service)
|
46 |
| - driver.implicitly_wait(10) |
| 71 | + |
| 72 | + # set shorter timeouts |
| 73 | + driver.set_page_load_timeout(15) |
| 74 | + driver.implicitly_wait(5) |
47 | 75 |
|
48 | 76 | profiler.track_memory("before_load")
|
| 77 | + try: |
| 78 | + profiler.track("dns_start", time.time()) |
| 79 | + driver.get(args.url) |
| 80 | + profiler.track("dns_end", time.time()) |
| 81 | + except TimeoutException: |
| 82 | + profiler.track_error("page_load", f"Timeout loading page: {args.url}") |
| 83 | + # continue execution to capture any partial data |
49 | 84 |
|
50 |
| - driver.get(args.url) |
| 85 | + if args.performance_log: |
| 86 | + network_logs = driver.get_log("performance") |
| 87 | + profiler.track_extra("network_logs", network_logs) |
| 88 | + browser_logs = driver.get_log("browser") |
| 89 | + profiler.track_extra("browser_logs", browser_logs) |
| 90 | + |
| 91 | + profiler.track_memory("after_load") |
51 | 92 |
|
52 | 93 | if args.wait_for:
|
53 |
| - # Wait for specific element if requested |
54 | 94 | try:
|
55 | 95 | WebDriverWait(driver, args.wait_for_timeout).until(
|
56 | 96 | EC.presence_of_element_located((By.CSS_SELECTOR, args.wait_for))
|
57 | 97 | )
|
58 | 98 | except TimeoutException:
|
59 | 99 | profiler.track_error("wait", f"Timeout waiting for element: {args.wait_for}")
|
60 | 100 |
|
61 |
| - profiler.track_memory("after_load") |
62 |
| - |
63 |
| - if args.performance_log: |
64 |
| - profiler.track_extra("performance_log", driver.get_log("performance")) |
65 |
| - |
66 | 101 | if args.screenshot:
|
67 | 102 | try:
|
68 | 103 | driver.get_screenshot_as_file("/tmp/screenshot.png")
|
|
76 | 111 | profiler.track_error("chrome", str(e))
|
77 | 112 | finally:
|
78 | 113 | try:
|
79 |
| - driver.quit() |
80 |
| - profiler.track_memory("after_quit") |
81 |
| - except Exception: |
| 114 | + if driver: |
| 115 | + driver.quit() |
| 116 | + except Exception as _: |
82 | 117 | pass
|
83 | 118 |
|
| 119 | + # ensure we always output something |
84 | 120 | print(json.dumps(profiler.as_dict()))
|
0 commit comments