1
- import unittest
2
- import random
3
- import time
4
- import subprocess
5
- import signal
6
1
import concurrent .futures
7
2
import csv
8
3
import os
4
+ import random
5
+ import signal
6
+ import subprocess
7
+ import time
8
+ import unittest
9
+
10
+ from csv2md .table import Table
9
11
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
12
from selenium .webdriver .chrome .options import Options as ChromeOptions
13
+ from selenium .webdriver .edge .options import Options as EdgeOptions
14
+ from selenium .webdriver .firefox .options import Options as FirefoxOptions
13
15
from selenium .webdriver .remote .client_config import ClientConfig
14
- from csv2md .table import Table
15
16
16
17
BROWSER = {
17
18
"chrome" : ChromeOptions (),
27
28
timeout = 3600 ,
28
29
)
29
30
30
- FIELD_NAMES = ["Iteration" , "New request sessions" , "Sessions created time" , "Sessions failed to create" , "New pods scaled up" , "Total running sessions" , "Total running pods" , "Max sessions per pod" , "Gaps" , "Sessions closed" ]
31
+ FIELD_NAMES = [
32
+ "Iteration" ,
33
+ "New request sessions" ,
34
+ "Sessions created time" ,
35
+ "Sessions failed to create" ,
36
+ "New pods scaled up" ,
37
+ "Total running sessions" ,
38
+ "Total running pods" ,
39
+ "Max sessions per pod" ,
40
+ "Gaps" ,
41
+ "Sessions closed" ,
42
+ ]
43
+
31
44
32
45
def get_pod_count ():
33
46
result = subprocess .run (["kubectl" , "get" , "pods" , "-A" , "--no-headers" ], capture_output = True , text = True )
34
47
return len ([line for line in result .stdout .splitlines () if "selenium-node-" in line and "Running" in line ])
35
48
49
+
36
50
def create_session (browser_name ):
37
51
options = BROWSER [browser_name ]
38
52
options .set_capability ("platformName" , "Linux" )
39
- return webdriver .Remote (command_executor = CLIENT_CONFIG .remote_server_addr , options = options , client_config = CLIENT_CONFIG )
53
+ return webdriver .Remote (
54
+ command_executor = CLIENT_CONFIG .remote_server_addr , options = options , client_config = CLIENT_CONFIG
55
+ )
56
+
40
57
41
58
def wait_for_count_matches (sessions , timeout = 10 , interval = 5 ):
42
59
elapsed = 0
@@ -48,20 +65,26 @@ def wait_for_count_matches(sessions, timeout=10, interval=5):
48
65
time .sleep (interval )
49
66
elapsed += interval
50
67
if pod_count != len (sessions ):
51
- print (f"WARN: Mismatch between pod count and session count after { timeout } seconds. Gaps: { pod_count - len (sessions )} " )
68
+ print (
69
+ f"WARN: Mismatch between pod count and session count after { timeout } seconds. Gaps: { pod_count - len (sessions )} "
70
+ )
52
71
else :
53
72
print (f"PASS: Pod count matches session count after { elapsed } seconds." )
54
73
74
+
55
75
def close_all_sessions (sessions ):
56
76
for session in sessions :
57
77
session .quit ()
58
78
sessions .clear ()
59
79
return sessions
60
80
81
+
61
82
def create_sessions_in_parallel (new_request_sessions ):
62
83
failed_jobs = 0
63
84
with concurrent .futures .ThreadPoolExecutor () as executor :
64
- futures = [executor .submit (create_session , random .choice (list (BROWSER .keys ()))) for _ in range (new_request_sessions )]
85
+ futures = [
86
+ executor .submit (create_session , random .choice (list (BROWSER .keys ()))) for _ in range (new_request_sessions )
87
+ ]
65
88
sessions = []
66
89
for future in concurrent .futures .as_completed (futures ):
67
90
try :
@@ -72,6 +95,7 @@ def create_sessions_in_parallel(new_request_sessions):
72
95
print (f"Total failed jobs: { failed_jobs } " )
73
96
return sessions
74
97
98
+
75
99
def randomly_quit_sessions (sessions , sublist_size ):
76
100
if sessions :
77
101
sessions_to_quit = random .sample (sessions , min (sublist_size , len (sessions )))
@@ -82,15 +106,18 @@ def randomly_quit_sessions(sessions, sublist_size):
82
106
return len (sessions_to_quit )
83
107
return 0
84
108
109
+
85
110
def get_result_file_name ():
86
111
return f"tests/autoscaling_results"
87
112
113
+
88
114
def export_results_to_csv (output_file , field_names , results ):
89
115
with open (output_file , mode = "w" ) as csvfile :
90
116
writer = csv .DictWriter (csvfile , fieldnames = field_names )
91
117
writer .writeheader ()
92
118
writer .writerows (results )
93
119
120
+
94
121
def export_results_csv_to_md (csv_file , md_file ):
95
122
with open (csv_file ) as f :
96
123
table = Table .parse_csv (f )
0 commit comments