-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] Signed-off-by: Viet Nguyen Duc <[email protected]>
- Loading branch information
Showing
12 changed files
with
293 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import unittest | ||
import random | ||
import time | ||
import subprocess | ||
import signal | ||
import concurrent.futures | ||
import csv | ||
import os | ||
from selenium import webdriver | ||
from selenium.webdriver.firefox.options import Options as FirefoxOptions | ||
from selenium.webdriver.edge.options import Options as EdgeOptions | ||
from selenium.webdriver.chrome.options import Options as ChromeOptions | ||
from selenium.webdriver.remote.client_config import ClientConfig | ||
from csv2md.table import Table | ||
|
||
BROWSER = { | ||
"chrome": ChromeOptions(), | ||
"firefox": FirefoxOptions(), | ||
"edge": EdgeOptions(), | ||
} | ||
|
||
CLIENT_CONFIG = ClientConfig( | ||
remote_server_addr=f"http://localhost/selenium/wd/hub", | ||
keep_alive=True, | ||
timeout=3600, | ||
) | ||
|
||
FIELD_NAMES = ["Iteration", "New request sessions", "Requests accepted time", "Sessions failed", "New scaled pods", "Total sessions", "Total pods", "Gaps"] | ||
|
||
def get_pod_count(): | ||
result = subprocess.run(["kubectl", "get", "pods", "-A", "--no-headers"], capture_output=True, text=True) | ||
return len([line for line in result.stdout.splitlines() if "selenium-node-" in line and "Running" in line]) | ||
|
||
def create_session(browser_name): | ||
return webdriver.Remote(command_executor=CLIENT_CONFIG.remote_server_addr, options=BROWSER[browser_name], client_config=CLIENT_CONFIG) | ||
|
||
def wait_for_count_matches(sessions, timeout=10, interval=5): | ||
elapsed = 0 | ||
while elapsed < timeout: | ||
pod_count = get_pod_count() | ||
if pod_count == len(sessions): | ||
break | ||
print(f"VALIDATING: Waiting for pods to match sessions... ({elapsed}/{timeout} seconds elapsed)") | ||
time.sleep(interval) | ||
elapsed += interval | ||
if pod_count != len(sessions): | ||
print(f"WARN: Mismatch between pod count and session count after {timeout} seconds. Gaps: {pod_count - len(sessions)}") | ||
else: | ||
print(f"PASS: Pod count matches session count after {elapsed} seconds.") | ||
|
||
def close_all_sessions(sessions): | ||
for session in sessions: | ||
session.quit() | ||
sessions.clear() | ||
return sessions | ||
|
||
def create_sessions_in_parallel(new_request_sessions): | ||
failed_jobs = 0 | ||
with concurrent.futures.ThreadPoolExecutor() as executor: | ||
futures = [executor.submit(create_session, random.choice(list(BROWSER.keys()))) for _ in range(new_request_sessions)] | ||
sessions = [] | ||
for future in concurrent.futures.as_completed(futures): | ||
try: | ||
sessions.append(future.result()) | ||
except Exception as e: | ||
print(f"ERROR: Failed to create session: {e}") | ||
failed_jobs += 1 | ||
print(f"Total failed jobs: {failed_jobs}") | ||
return sessions | ||
|
||
def randomly_quit_sessions(sessions, sublist_size): | ||
if sessions: | ||
sessions_to_quit = random.sample(sessions, min(sublist_size, len(sessions))) | ||
for session in sessions_to_quit: | ||
session.quit() | ||
sessions.remove(session) | ||
print(f"QUIT: {len(sessions_to_quit)} sessions have been randomly quit.") | ||
return sessions | ||
|
||
def export_results_to_csv(output_file, field_names, results): | ||
with open(output_file, mode="w") as csvfile: | ||
writer = csv.DictWriter(csvfile, fieldnames=field_names) | ||
writer.writeheader() | ||
writer.writerows(results) | ||
|
||
def export_results_csv_to_md(csv_file, md_file): | ||
with open(csv_file) as f: | ||
table = Table.parse_csv(f) | ||
with open(md_file, mode="w") as f: | ||
f.write(table.markdown()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import unittest | ||
import random | ||
import time | ||
import signal | ||
import csv | ||
from csv2md.table import Table | ||
from .common import * | ||
|
||
SESSIONS = [] | ||
RESULTS = [] | ||
|
||
def signal_handler(signum, frame): | ||
print("Signal received, quitting all sessions...") | ||
close_all_sessions(SESSIONS) | ||
|
||
signal.signal(signal.SIGTERM, signal_handler) | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
class SeleniumAutoscalingTests(unittest.TestCase): | ||
def test_run_tests(self): | ||
try: | ||
for iteration in range(10): | ||
new_request_sessions = random.randint(2, 15) | ||
start_time = time.time() | ||
start_pods = get_pod_count() | ||
new_sessions = create_sessions_in_parallel(new_request_sessions) | ||
failed_sessions = new_request_sessions - len(new_sessions) | ||
end_time = time.time() | ||
stop_pods = get_pod_count() | ||
SESSIONS.extend(new_sessions) | ||
elapsed_time = end_time - start_time | ||
new_scaled_pods = stop_pods - start_pods | ||
total_sessions = len(SESSIONS) | ||
total_pods = get_pod_count() | ||
RESULTS.append({ | ||
FIELD_NAMES[0]: iteration + 1, | ||
FIELD_NAMES[1]: new_request_sessions, | ||
FIELD_NAMES[2]: f"{elapsed_time:.2f} s", | ||
FIELD_NAMES[3]: failed_sessions, | ||
FIELD_NAMES[4]: new_scaled_pods, | ||
FIELD_NAMES[5]: total_sessions, | ||
FIELD_NAMES[6]: total_pods, | ||
FIELD_NAMES[7]: total_pods - total_sessions, | ||
}) | ||
print(f"ADDING: Created {new_request_sessions} new sessions in {elapsed_time:.2f} seconds.") | ||
print(f"INFO: Total sessions: {total_sessions}") | ||
print(f"INFO: Total pods: {total_pods}") | ||
randomly_quit_sessions(SESSIONS, 10) | ||
time.sleep(15) | ||
finally: | ||
print(f"FINISH: Closing {len(SESSIONS)} sessions.") | ||
close_all_sessions(SESSIONS) | ||
output_file = f"tests/scale_up_results_{random.randint(1, 10000)}" | ||
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS) | ||
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import unittest | ||
import random | ||
import time | ||
import signal | ||
import csv | ||
from csv2md.table import Table | ||
from .common import * | ||
|
||
SESSIONS = [] | ||
RESULTS = [] | ||
|
||
def signal_handler(signum, frame): | ||
print("Signal received, quitting all sessions...") | ||
close_all_sessions(SESSIONS) | ||
|
||
signal.signal(signal.SIGTERM, signal_handler) | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
class SeleniumAutoscalingTests(unittest.TestCase): | ||
def test_run_tests(self): | ||
try: | ||
for iteration in range(10): | ||
new_request_sessions = random.randint(1, 3) | ||
start_time = time.time() | ||
start_pods = get_pod_count() | ||
new_sessions = create_sessions_in_parallel(new_request_sessions) | ||
failed_sessions = new_request_sessions - len(new_sessions) | ||
end_time = time.time() | ||
stop_pods = get_pod_count() | ||
SESSIONS.extend(new_sessions) | ||
elapsed_time = end_time - start_time | ||
new_scaled_pods = stop_pods - start_pods | ||
total_sessions = len(SESSIONS) | ||
total_pods = get_pod_count() | ||
RESULTS.append({ | ||
FIELD_NAMES[0]: iteration + 1, | ||
FIELD_NAMES[1]: new_request_sessions, | ||
FIELD_NAMES[2]: f"{elapsed_time:.2f} s", | ||
FIELD_NAMES[3]: failed_sessions, | ||
FIELD_NAMES[4]: new_scaled_pods, | ||
FIELD_NAMES[5]: total_sessions, | ||
FIELD_NAMES[6]: total_pods, | ||
FIELD_NAMES[7]: total_pods - total_sessions, | ||
}) | ||
print(f"ADDING: Created {new_request_sessions} new sessions in {elapsed_time:.2f} seconds.") | ||
print(f"INFO: Total sessions: {total_sessions}") | ||
print(f"INFO: Total pods: {total_pods}") | ||
if iteration % 4 == 0: | ||
randomly_quit_sessions(SESSIONS, 15) | ||
time.sleep(15) | ||
finally: | ||
print(f"FINISH: Closing {len(SESSIONS)} sessions.") | ||
close_all_sessions(SESSIONS) | ||
output_file = f"tests/scale_up_results_{random.randint(1, 10000)}" | ||
export_results_to_csv(f"{output_file}.csv", FIELD_NAMES, RESULTS) | ||
export_results_csv_to_md(f"{output_file}.csv", f"{output_file}.md") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.