-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_rw.py
executable file
·64 lines (48 loc) · 1.71 KB
/
app_rw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import mysql.connector
import time
from datetime import datetime
def truncate(n, decimals=0):
multiplier = 10 ** decimals
return int(n * multiplier) / multiplier
def main():
connection = "null"
db_server = "null"
write_performed = "null"
try:
cnx = mysql.connector.connect(user='root', password='msandbox', host='localhost', port=6446, connection_timeout=5, autocommit=True)
cursor = cnx.cursor()
# Check to which instances the queries are sent
query = ("SELECT @@hostname, @@port")
cursor.execute(query)
for (host, port) in cursor:
db_server = str(host) + ":" + str(port)
# Write some data
query = ("CREATE SCHEMA IF NOT EXISTS test")
cursor.execute(query)
query = ("CREATE TABLE IF NOT EXISTS test.data (a int primary key auto_increment, data longtext)")
cursor.execute(query)
start_time = time.time()
query = ("INSERT INTO test.data values (default, repeat('x', 8))")
cursor.execute(query)
elapsed_time = time.time() - start_time
elapsed_time_ms = truncate(elapsed_time * 1000, 3)
cursor.close()
cnx.close()
connection = "OK"
write_performed = str(elapsed_time_ms) + "ms"
except mysql.connector.Error as e:
connection = "ERROR: " + e.msg
except Exception as e:
print("Unexpected error:", e)
return connection, db_server, write_performed
i=-1
while True:
i+=1
if i % 15 == 0:
# Print header
print("TIME\t\tCONNECTION\t\tDB SERVER\t\tWRITE PERFORMED")
result = main()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("{0}\t\t{1}\t\t{2}\t\t{3}".format(current_time, result[0], result[1], result[2]))
time.sleep(1)