8
8
from cryptography import __version__ as cryptography_version
9
9
from urllib .parse import urlparse
10
10
import requests
11
+ from getpass import getpass
12
+
11
13
#import matplotlib.pyplot as plt
12
14
13
15
import oracledb
19
21
20
22
query_times = []
21
23
22
- # Oracle Database credentials
23
- oracle_un = 'admin'
24
- oracle_pw = 'your_password'
25
- oracle_cs = '(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.uk-london-1.oraclecloud.com))(connect_data=(service_name=m783q0lhgfda8ox_demoadb_high.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))'
26
-
27
- # PostgreSQL credentials
28
- pgsql_un = 'postgres'
29
- pgsql_pw = 'your_password'
30
- pgsql_host = 'localhost'
31
- pgsql_port = '5432'
32
- pgsql_db = 'postgres'
33
-
34
- # MySQL credentials
35
- mysql_un = 'mysql'
36
- mysql_pw = 'your_password'
37
- mysql_host = 'localhost'
38
- mysql_port = '3306'
39
- mysql_db = 'mysql'
40
-
41
-
42
- # URL for testing
43
- test_url = 'https://www.google.com'
44
-
45
24
def calculate_p99_latency ():
46
25
if len (query_times ) > 0 :
47
26
p99_latency = np .percentile (query_times , 99 )
@@ -59,13 +38,11 @@ def calculate_p99_latency():
59
38
print ("++++++++++++++++++++++" )
60
39
print ("Mean Latency: {:.2f} ms" .format (mean_latency ))
61
40
print ("++++++++++++++++++++++" )
62
-
63
41
else :
64
42
print ("No queries were executed." )
65
43
66
- def oracle_ping (interval , csvfile ):
67
-
68
- conn = oracledb .connect (user = oracle_un , password = oracle_pw , dsn = oracle_cs )
44
+ def oracle_ping (interval , csvfile , user , password , dsn ):
45
+ conn = oracledb .connect (user = user , password = password , dsn = dsn )
69
46
cursor = conn .cursor ()
70
47
cursor .execute ("select sys_context('USERENV','SID'), sys_context('USERENV','INSTANCE') from dual" )
71
48
sid , instance = cursor .fetchone ()
@@ -86,34 +63,8 @@ def oracle_ping(interval, csvfile):
86
63
cursor .close ()
87
64
conn .close ()
88
65
89
-
90
-
91
- def postgresql_ping (interval , csvfile ):
92
- conn = psycopg2 .connect (host = pgsql_host , port = pgsql_port , dbname = pgsql_db , user = pgsql_un , password = pgsql_pw )
93
-
94
- cursor = conn .cursor ()
95
-
96
- t0 = time .perf_counter ()
97
- cursor .execute ("SELECT 1" )
98
- cursor .fetchall ()
99
- t1 = time .perf_counter ()
100
-
101
- query_time = (t1 - t0 ) * 1000
102
- query_times .append (query_time )
103
-
104
- if csvfile is not None :
105
- writer = csv .writer (csvfile )
106
- timestamp = datetime .datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
107
- writer .writerow ([timestamp , query_time ])
108
-
109
- cursor .close ()
110
- conn .close ()
111
-
112
-
113
-
114
- def mysql_ping (interval , csvfile ):
115
- conn = pymysql .connect (host = mysql_host , port = int (mysql_port ), user = mysql_un , password = mysql_pw , db = mysql_db )
116
-
66
+ def postgresql_ping (interval , csvfile , user , password , host , port , db ):
67
+ conn = psycopg2 .connect (host = host , port = port , dbname = db , user = user , password = password )
117
68
cursor = conn .cursor ()
118
69
119
70
t0 = time .perf_counter ()
@@ -132,12 +83,8 @@ def mysql_ping(interval, csvfile):
132
83
cursor .close ()
133
84
conn .close ()
134
85
135
-
136
-
137
- def sql_server_ping (interval , csvfile ):
138
- conn_str = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={ sql_server_host } ,{ sql_server_port } ;DATABASE={ sql_server_db } ;UID={ sql_server_un } ;PWD={ sql_server_pw } '
139
- conn = pyodbc .connect (conn_str )
140
-
86
+ def mysql_ping (interval , csvfile , user , password , host , port , db ):
87
+ conn = pymysql .connect (host = host , port = int (port ), user = user , password = password , db = db )
141
88
cursor = conn .cursor ()
142
89
143
90
t0 = time .perf_counter ()
@@ -156,15 +103,11 @@ def sql_server_ping(interval, csvfile):
156
103
cursor .close ()
157
104
conn .close ()
158
105
159
-
160
-
161
- def url_ping (interval , csvfile ):
106
+ def url_ping (interval , csvfile , url ):
162
107
t0 = time .perf_counter ()
163
- response = requests .get (test_url )
108
+ response = requests .get (url )
164
109
t1 = time .perf_counter ()
165
110
166
- # (The rest of the function remains the same)
167
-
168
111
query_time = (t1 - t0 ) * 1000
169
112
query_times .append (query_time )
170
113
@@ -173,54 +116,63 @@ def url_ping(interval, csvfile):
173
116
timestamp = datetime .datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
174
117
writer .writerow ([timestamp , query_time ])
175
118
176
-
177
-
178
- # cmd-line arguements
179
119
parser = argparse .ArgumentParser (description = "Connect and run a query." )
180
120
parser .add_argument ("--interval" , type = float , help = "interval between each query, default 1" , default = 1 )
181
121
parser .add_argument ("--period" , type = int , help = "runtime in seconds; default 60" , default = 60 )
182
122
parser .add_argument ("--csvoutput" , help = "write timings to the named CSV file" )
183
- parser .add_argument ("--db" , choices = ['oracle' , 'postgresql' , 'mysql' , 'sqlserver' , 'url' ], required = True , help = "specify the database or url to test" )
123
+ parser .add_argument ("--db" , choices = ['oracle' , 'postgresql' , 'mysql' , 'url' ], required = True , help = "specify the database or url to test" )
124
+ parser .add_argument ("--user" , help = "Database username" )
125
+ parser .add_argument ("--password" , help = "Database password" )
126
+ parser .add_argument ("--host" , help = "Database host/DSN string" )
127
+ parser .add_argument ("--port" , help = "Database port" )
128
+ parser .add_argument ("--database" , help = "Database name" )
129
+ parser .add_argument ("--url" , help = "Test URL for latency" )
184
130
args = parser .parse_args ()
185
131
186
-
187
132
if args .csvoutput is not None :
188
133
csvfile = open (args .csvoutput , "w" , newline = "" )
189
134
writer = csv .writer (csvfile )
190
- writer .writerow (["Timestamp" , "Query time (ms)" , "SID" , "Instance" ])
135
+ if args .db == "oracle" :
136
+ writer .writerow (["Timestamp" , "Query time (ms)" , "SID" , "Instance" ])
137
+ else :
138
+ writer .writerow (["Timestamp" , "Query time (ms)" ])
191
139
else :
192
140
csvfile = None
193
141
194
-
195
142
start_time = time .perf_counter ()
196
143
end_time = start_time + args .period
197
144
198
- # Main loop
199
- while time .perf_counter () < end_time :
145
+ # Gather credentials if not provided
146
+ def prompt_if_none (val , prompt_text , secure = False ):
147
+ if val :
148
+ return val
149
+ if secure :
150
+ return getpass (prompt_text )
151
+ return input (prompt_text )
152
+
153
+ for _ in range (int (args .period // args .interval )):
200
154
if args .db == 'oracle' :
201
- oracle_ping (args .interval , csvfile )
155
+ user = prompt_if_none (args .user , "Oracle Username: " )
156
+ password = prompt_if_none (args .password , "Oracle Password: " , secure = True )
157
+ dsn = prompt_if_none (args .host , "Oracle DSN (connection string): " )
158
+ oracle_ping (args .interval , csvfile , user , password , dsn )
202
159
elif args .db == 'postgresql' :
203
- postgresql_ping (args .interval , csvfile )
160
+ user = prompt_if_none (args .user , "Postgres Username: " )
161
+ password = prompt_if_none (args .password , "Postgres Password: " , secure = True )
162
+ host = prompt_if_none (args .host , "Postgres Host: " )
163
+ port = prompt_if_none (args .port , "Postgres Port: " )
164
+ db = prompt_if_none (args .database , "Postgres DB Name: " )
165
+ postgresql_ping (args .interval , csvfile , user , password , host , port , db )
204
166
elif args .db == 'mysql' :
205
- mysql_ping (args .interval , csvfile )
206
- elif args .db == 'sqlserver' :
207
- sql_server_ping (args .interval , csvfile )
167
+ user = prompt_if_none (args .user , "MySQL Username: " )
168
+ password = prompt_if_none (args .password , "MySQL Password: " , secure = True )
169
+ host = prompt_if_none (args .host , "MySQL Host: " )
170
+ port = prompt_if_none (args .port , "MySQL Port: " )
171
+ db = prompt_if_none (args .database , "MySQL DB Name: " )
172
+ mysql_ping (args .interval , csvfile , user , password , host , port , db )
208
173
elif args .db == 'url' :
209
- url_ping (args .interval , csvfile )
174
+ url = prompt_if_none (args .url , "Test URL: " )
175
+ url_ping (args .interval , csvfile , url )
210
176
time .sleep (args .interval )
211
177
212
178
calculate_p99_latency ()
213
-
214
- # Plot the latencies on a graph
215
- #plt.figure(figsize=(10, 5))
216
- #plt.plot(query_times, marker='o')
217
- #plt.title('Latency Over Time')
218
- #plt.xlabel('Query Number')
219
- #plt.ylabel('Latency (ms)')
220
-
221
- # Save the plot to a file
222
- output_file = "latency_plot.png"
223
- #plt.savefig(output_file, bbox_inches='tight', dpi=300)
224
-
225
- # Display the plot
226
- #plt.show()
0 commit comments