-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
180 lines (159 loc) · 5.89 KB
/
driver.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import psycopg2
import csv
import sys
import datetime
from statistics import mean, median
import numpy as np
from first_four_trans import *
from last_four_trans import *
def process_transactions(input_params, conn):
if input_params[0] == 'N':
new_order(conn,
int(input_params[1]),
int(input_params[2]),
int(input_params[3]),
int(input_params[4]),
input_params[5],
)
elif input_params[0] == 'P':
payment(conn,
int(input_params[1]),
int(input_params[2]),
int(input_params[3]),
float(input_params[4]),
)
elif input_params[0] == 'D':
delivery(conn, int(input_params[1]), int(input_params[2]))
elif input_params[0] == 'O':
order_status(conn, int(input_params[1]), int(input_params[2]), int(input_params[3]))
elif input_params[0] == 'S':
get_stock_level_transaction(conn,
int(input_params[1]),
int(input_params[2]),
int(input_params[3]),
int(input_params[4]),
)
elif input_params[0] == 'I':
get_popular_items_transaction(conn,
int(input_params[1]),
int(input_params[2]),
int(input_params[3])
)
elif input_params[0] == 'T':
get_top_balance_transaction(conn)
elif input_params[0] == 'R':
get_related_customer_transaction(conn,
int(input_params[1]),
int(input_params[2]),
int(input_params[3]))
transaction_file = sys.argv[1]
output_fir = sys.argv[2]
host = sys.argv[3]
conn = psycopg2.connect(
database='wholesale',
user='root',
sslmode='verify-full',
sslrootcert='/temp/cs4224h/certs/ca.crt',
sslcert='/temp/cs4224h/certs/client.root.crt',
sslkey='/temp/cs4224h/certs/client.root.key',
# sslrootcert='../certs/ca.crt',
# sslcert='../certs/client.root.crt',
# sslkey='../certs/client.root.key',
port=26278,
host=host,
password='cs4224hadmin'
)
# transaction_file = '/Users/ruiyan/Desktop/MSc/SEM1AY2021:2022/CS5424DD/project/project_files_4/xact_files_A/0.txt'
throughput_for_all = []
clients_performance = []
# Get Client Number
char_position=transaction_file.rfind('.txt')
client_num = transaction_file[char_position - 2:char_position].replace('/', '')
try:
f = open(transaction_file, "r")
# append each line in the file to a list
temp_data = f.read().splitlines()
num_of_trxn = 0
total_trxn_time = 0
trxn_latency_lst = []
for line_num in range(len(temp_data)):
line = temp_data[line_num]
input_params = line.split(',')
if input_params[0] == 'N':
num_items = int(input_params[4])
items = []
end_line_num = line_num + num_items + 1
for i in range(line_num + 1, end_line_num):
items.append(temp_data[i].split(','))
input_params.append(items)
if input_params[0] in ('N', 'P', 'D', 'O', 'S', 'I', 'T', 'R'):
try:
start = datetime.datetime.now()
process_transactions(input_params, conn)
print(input_params)
time_diff = (datetime.datetime.now() - start).total_seconds()
total_trxn_time += time_diff
trxn_latency_lst.append(time_diff * 1000)
num_of_trxn += 1
except psycopg2.Error as e:
conn.rollback()
print("psycopg2 Exception: %s", e)
continue
except Exception as e:
conn.rollback()
print("General Exception: %s", e)
continue
except Exception as e:
print("Exception: %s", e)
finally:
print("Closing file & DB connection.")
f.close()
conn.close()
client_throughput = 0 if total_trxn_time == 0 else round(num_of_trxn / total_trxn_time, 2)
throughput_for_all.append(client_throughput)
trxn_latency_ndarr_dist = np.array(trxn_latency_lst)
client_performance_record = [
client_num,
num_of_trxn,
round(total_trxn_time, 2),
client_throughput,
round(mean(trxn_latency_lst), 2),
round(median(trxn_latency_lst), 2),
round(np.percentile(trxn_latency_ndarr_dist, 95), 2),
round(np.percentile(trxn_latency_ndarr_dist, 99), 2)
]
# print('This is file ', file, client_performance_record)
clients_performance.append(client_performance_record)
# Export client.csv
# print('clients_performance', clients_performance)
print(clients_performance)
print(clients_performance, file=sys.stderr)
try:
with open(output_fir + 'clients_' + client_num + '.csv', 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
csvwriter.writerows(clients_performance)
except Exception as e:
print("Exception in output clients {}: ".format(client_num), e)
# Export throughput.csv
throughput_data_frag = [[
min(throughput_for_all),
max(throughput_for_all),
round(mean(throughput_for_all), 2)]
]
print(throughput_data_frag)
print(throughput_data_frag, file=sys.stderr)
try:
with open(output_fir + 'throughput_' + client_num + '.csv', 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
csvwriter.writerows(
# [[
# min(throughput_for_all),
# max(throughput_for_all),
# round(mean(throughput_for_all), 2)]
# ]
throughput_data_frag
)
except Exception as e:
print("Exception in output throughput {}: ".format(client_num), e)