|
| 1 | +import unittest |
| 2 | +import random |
| 3 | +import time |
| 4 | +import subprocess |
| 5 | +import signal |
| 6 | +import concurrent.futures |
| 7 | +import csv |
| 8 | +import os |
| 9 | +from selenium import webdriver |
| 10 | +from selenium.webdriver.firefox.options import Options as FirefoxOptions |
| 11 | +from selenium.webdriver.edge.options import Options as EdgeOptions |
| 12 | +from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 13 | +from selenium.webdriver.remote.client_config import ClientConfig |
| 14 | +from csv2md.table import Table |
| 15 | + |
| 16 | +BROWSER = { |
| 17 | + "chrome": ChromeOptions(), |
| 18 | + "firefox": FirefoxOptions(), |
| 19 | + "edge": EdgeOptions(), |
| 20 | +} |
| 21 | + |
| 22 | +SELENIUM_GRID_HOST = os.getenv("SELENIUM_GRID_HOST", "localhost") |
| 23 | + |
| 24 | +CLIENT_CONFIG = ClientConfig( |
| 25 | + remote_server_addr=f"http://{SELENIUM_GRID_HOST}/selenium/wd/hub", |
| 26 | + keep_alive=True, |
| 27 | + timeout=3600, |
| 28 | +) |
| 29 | + |
| 30 | +FIELD_NAMES = ["Iteration", "New request sessions", "Requests accepted time", "Sessions failed", "New scaled pods", "Total sessions", "Total pods", "Gaps"] |
| 31 | + |
| 32 | +def get_pod_count(): |
| 33 | + result = subprocess.run(["kubectl", "get", "pods", "-A", "--no-headers"], capture_output=True, text=True) |
| 34 | + return len([line for line in result.stdout.splitlines() if "selenium-node-" in line and "Running" in line]) |
| 35 | + |
| 36 | +def create_session(browser_name): |
| 37 | + return webdriver.Remote(command_executor=CLIENT_CONFIG.remote_server_addr, options=BROWSER[browser_name], client_config=CLIENT_CONFIG) |
| 38 | + |
| 39 | +def wait_for_count_matches(sessions, timeout=10, interval=5): |
| 40 | + elapsed = 0 |
| 41 | + while elapsed < timeout: |
| 42 | + pod_count = get_pod_count() |
| 43 | + if pod_count == len(sessions): |
| 44 | + break |
| 45 | + print(f"VALIDATING: Waiting for pods to match sessions... ({elapsed}/{timeout} seconds elapsed)") |
| 46 | + time.sleep(interval) |
| 47 | + elapsed += interval |
| 48 | + if pod_count != len(sessions): |
| 49 | + print(f"WARN: Mismatch between pod count and session count after {timeout} seconds. Gaps: {pod_count - len(sessions)}") |
| 50 | + else: |
| 51 | + print(f"PASS: Pod count matches session count after {elapsed} seconds.") |
| 52 | + |
| 53 | +def close_all_sessions(sessions): |
| 54 | + for session in sessions: |
| 55 | + session.quit() |
| 56 | + sessions.clear() |
| 57 | + return sessions |
| 58 | + |
| 59 | +def create_sessions_in_parallel(new_request_sessions): |
| 60 | + failed_jobs = 0 |
| 61 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 62 | + futures = [executor.submit(create_session, random.choice(list(BROWSER.keys()))) for _ in range(new_request_sessions)] |
| 63 | + sessions = [] |
| 64 | + for future in concurrent.futures.as_completed(futures): |
| 65 | + try: |
| 66 | + sessions.append(future.result()) |
| 67 | + except Exception as e: |
| 68 | + print(f"ERROR: Failed to create session: {e}") |
| 69 | + failed_jobs += 1 |
| 70 | + print(f"Total failed jobs: {failed_jobs}") |
| 71 | + return sessions |
| 72 | + |
| 73 | +def randomly_quit_sessions(sessions, sublist_size): |
| 74 | + if sessions: |
| 75 | + sessions_to_quit = random.sample(sessions, min(sublist_size, len(sessions))) |
| 76 | + for session in sessions_to_quit: |
| 77 | + session.quit() |
| 78 | + sessions.remove(session) |
| 79 | + print(f"QUIT: {len(sessions_to_quit)} sessions have been randomly quit.") |
| 80 | + return sessions |
| 81 | + |
| 82 | +def export_results_to_csv(output_file, field_names, results): |
| 83 | + with open(output_file, mode="w") as csvfile: |
| 84 | + writer = csv.DictWriter(csvfile, fieldnames=field_names) |
| 85 | + writer.writeheader() |
| 86 | + writer.writerows(results) |
| 87 | + |
| 88 | +def export_results_csv_to_md(csv_file, md_file): |
| 89 | + with open(csv_file) as f: |
| 90 | + table = Table.parse_csv(f) |
| 91 | + with open(md_file, mode="w") as f: |
| 92 | + f.write(table.markdown()) |
0 commit comments