Skip to content

Commit f67b641

Browse files
Merge pull request #11 from shadab-mohammad-oracle/patch-4
Update delta.py
2 parents b50169c + 3a812d1 commit f67b641

File tree

1 file changed

+48
-96
lines changed

1 file changed

+48
-96
lines changed

delta.py

Lines changed: 48 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from cryptography import __version__ as cryptography_version
99
from urllib.parse import urlparse
1010
import requests
11+
from getpass import getpass
12+
1113
#import matplotlib.pyplot as plt
1214

1315
import oracledb
@@ -19,29 +21,6 @@
1921

2022
query_times = []
2123

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-
4524
def calculate_p99_latency():
4625
if len(query_times) > 0:
4726
p99_latency = np.percentile(query_times, 99)
@@ -59,13 +38,11 @@ def calculate_p99_latency():
5938
print("++++++++++++++++++++++")
6039
print("Mean Latency: {:.2f} ms".format(mean_latency))
6140
print("++++++++++++++++++++++")
62-
6341
else:
6442
print("No queries were executed.")
6543

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)
6946
cursor = conn.cursor()
7047
cursor.execute("select sys_context('USERENV','SID'), sys_context('USERENV','INSTANCE') from dual")
7148
sid, instance = cursor.fetchone()
@@ -86,34 +63,8 @@ def oracle_ping(interval, csvfile):
8663
cursor.close()
8764
conn.close()
8865

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)
11768
cursor = conn.cursor()
11869

11970
t0 = time.perf_counter()
@@ -132,12 +83,8 @@ def mysql_ping(interval, csvfile):
13283
cursor.close()
13384
conn.close()
13485

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)
14188
cursor = conn.cursor()
14289

14390
t0 = time.perf_counter()
@@ -156,15 +103,11 @@ def sql_server_ping(interval, csvfile):
156103
cursor.close()
157104
conn.close()
158105

159-
160-
161-
def url_ping(interval, csvfile):
106+
def url_ping(interval, csvfile, url):
162107
t0 = time.perf_counter()
163-
response = requests.get(test_url)
108+
response = requests.get(url)
164109
t1 = time.perf_counter()
165110

166-
# (The rest of the function remains the same)
167-
168111
query_time = (t1 - t0) * 1000
169112
query_times.append(query_time)
170113

@@ -173,54 +116,63 @@ def url_ping(interval, csvfile):
173116
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
174117
writer.writerow([timestamp, query_time])
175118

176-
177-
178-
# cmd-line arguements
179119
parser = argparse.ArgumentParser(description="Connect and run a query.")
180120
parser.add_argument("--interval", type=float, help="interval between each query, default 1", default=1)
181121
parser.add_argument("--period", type=int, help="runtime in seconds; default 60", default=60)
182122
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")
184130
args = parser.parse_args()
185131

186-
187132
if args.csvoutput is not None:
188133
csvfile = open(args.csvoutput, "w", newline="")
189134
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)"])
191139
else:
192140
csvfile = None
193141

194-
195142
start_time = time.perf_counter()
196143
end_time = start_time + args.period
197144

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)):
200154
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)
202159
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)
204166
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)
208173
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)
210176
time.sleep(args.interval)
211177

212178
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

Comments
 (0)