forked from prathyushpoduval/DataBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
508 lines (329 loc) · 14.9 KB
/
Copy pathtest.py
File metadata and controls
508 lines (329 loc) · 14.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from multiprocessing import Process
import node
import socket
import time
import transactions_sql as trans
import numpy as np
import json
from utils import *
import random
import os
import signal
from subprocess import Popen, PIPE
import pandas as pd
NUM_NODE=3
def run_node(node_id, port,main_port):
node.start_node(node_id, port,main_port)
def kill_listener(port):
process = Popen(["lsof", "-i", ":{0}".format(port)], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
for process in str(stdout.decode("utf-8")).split("\n")[1:]:
data = [x for x in process.split(" ") if x != '']
if (len(data) <= 1):
continue
os.kill(int(data[1]), signal.SIGKILL)
def make_transaction(transaction, user_node_dict,node_ports,first_hop_only=False,send=True, **kwargs):
# create user
response_req=False
if transaction == "create_user":
user_id=kwargs["user_id"]
user_name=kwargs["user_name"]
user_node=kwargs["user_node"]
user_node_dict[user_id]=user_node
timestamp=time.time()
hops,node=trans.create_user(user_id,user_name,timestamp)
#print(node)
#print(user_node_dict)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if len(resp)!=0:
return -1
# create friendship
elif transaction == "create_friendship":
user_id1=kwargs["user_id1"]
user_id2=kwargs["user_id2"]
timestamp=time.time()
hops,node=trans.create_friendship(user_id1,user_id2,timestamp)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if len(resp)!=0:
return -1
# create post
elif transaction == "create_post":
post_id=kwargs["post_id"]
user_id=kwargs["user_id"]
content=kwargs["content"]
timestamp=time.time()
hops,node=trans.create_post(post_id,user_id, timestamp, content)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if len(resp)!=0:
return -1
# like post
elif transaction == "like_post":
user_id=kwargs["user_id"]
post_user_id=kwargs["post_user_id"]
post_id=kwargs["post_id"]
timestamp=time.time()
hops,node=trans.like_post(user_id,post_user_id, post_id, timestamp)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if len(resp)==0:
return -1
# edit post
elif transaction == "edit_post":
user_id=kwargs["user_id"]
post_id=kwargs["post_id"]
content=kwargs["content"]
timestamp=time.time()
hops,node=trans.edit_post(user_id, post_id, content)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if len(resp)==0:
return -1
# timeline query
# THIS DOES NOT MATCH THE TRANSACTION DEFINITION
elif transaction == "timeline":
user_id=kwargs["user_id"]
#uncertain about node
timestamp=time.time()
node=user_node_dict[user_id]
hops=trans.timeline_query(user_id)
resp,hops,node=perform_first_hop(hops,[node_ports[user_node_dict[user_id]],0],send=send)
if send:
friendlist=[node_ports[user_node_dict[i[0]]] for i in resp]
hop_list=[]
node_list=[]
for i in resp:
user_id_friend=i[0]
node_friend=user_node_dict[user_id_friend]
hops_friend=trans.timeline_query(user_id_friend)
hops_friend=[hops_friend[1]]
node_list.append(node_ports[node_friend])
hop_list.append(hops_friend)
hops,node=compress_hops_nodes(hop_list,node_list)
response_req=True
elif transaction == "print_all_tables":
hops=trans.print_all_tables()
node=list(range(NUM_NODE))
node=[node_ports[i] for i in node]
hop_list=[hops for i in range(NUM_NODE)]
hops=hop_list
response_req=True
elif transaction == "remove_user":
user_id=kwargs["user_id"]
timestamp = time.time()
hops,node=trans.remove_user(user_id)
node=[node_ports[user_node_dict[i]] for i in node]
resp,hops,node=perform_first_hop(hops,node,send=send)
if not first_hop_only and send:
for h,n in zip(hops,node):
h = list(h)
send_transaction(n,h)
#del user_node_dict[user_id]
return 1
else:
print("Invalid transaction.")
return -1
########
if not first_hop_only and send:
for h,n in zip(hops,node):
# Objects of type ndarray are not JSON serializable
h = list(h)
if response_req:
resp=send_transaction(n,h,True)
resp=json.loads(resp)
#print(resp)
else:
send_transaction(n,h)
#print(f"Hop Executed on Node {n}:\n")
def main(DB_SIZE=100, num_itr=100,transaction_to_run="create_user", debug=False,user_node_dict=None,first_hop_only=False,send=True):
main_port=2900
node_ports= [2100+i for i in range(NUM_NODE)]
processes = []
for i, port in enumerate(node_ports):
p = Process(target=run_node, args=(i, port,main_port))
p.daemon = True
p.start()
processes.append(p)
break_while=False
while not break_while:
break_while=True
time.sleep(0.1)
for port in node_ports:
if send_message(port,"test")==-1:
break_while = False
for port in node_ports:
send_message(port,"start")
print("Ports started....")
# Print PIDs of all started processes
if debug:
for i, process in enumerate(processes):
print(f"Process {i} started with PID: {process.pid}")
# ASKS FOR TRANSACTIONS
#FILL
#user_node_dict={}
#Get User dict
#for i in range(NUM_NODE):
# transaction=f"SELECT user_id FROM Users"
# resp=send_transaction(node_ports[i],[transaction],True)
# resp=json.loads(resp)
# #print(resp)
# resp=resp[0]
# for j in resp:
# user_node_dict[j[0]]=i
#print("User Node dict loaded....")
#COMPLETE
######
test = True
# Create 100 dummy users on randomNodes
#check if template_{DB_SIZE}_i.db exists, and copy to node_i.db
#if not, create template_{DB_SIZE}_i.db using dummy
if test == True and user_node_dict==None:
user_node_dict={}
for x in range(DB_SIZE):
timestamp=time.time()
curr_node=random.randint(0,NUM_NODE-1)
user_node_dict[str(x)]=curr_node
kwargs={"user_id":str(x),"user_name":f"tester_{x}_{curr_node}","user_node":curr_node}
make_transaction("create_user",user_node_dict,node_ports,**kwargs)
if debug:
print("DUMMIES USERS CREATED")
print("DUMMY POST CREATED")
for x in range(DB_SIZE//2):
#Insert random friendship
#Chose user_id1 and 2 randomly from user node dict
user1=random.choice(list(user_node_dict.keys()))
user2=random.choice(list(user_node_dict.keys()))
while user2==user1:
user2=random.choice(list(user_node_dict.keys()))
kwargs={"user_id1":user1,"user_id2":user2}
make_transaction("create_friendship",user_node_dict,node_ports, **kwargs)
if debug:
print("DUMMY FRIENDSHIPS CREATED")
for x in range(DB_SIZE):
#random likes being inserted
user_id=random.choice(list(user_node_dict.keys()))
post_user_id=random.choice(list(user_node_dict.keys()))
while post_user_id==user_id:
post_user_id=random.choice(list(user_node_dict.keys()))
post_id=f'post_0_{post_user_id}'
kwargs={"user_id":user_id,"post_user_id":post_user_id,"post_id":post_id}
make_transaction("like_post",user_node_dict,node_ports,**kwargs)
print("DUMMY LIKES CREATED")
test=True
current_user_id_count=DB_SIZE+1
time_start=time.time()
for i in range(num_itr):
transaction = transaction_to_run
if transaction=="create_user":
user_id=str(current_user_id_count)
current_user_id_count+=1
node=random.randint(0,NUM_NODE-1)
user_name=f"latency_{current_user_id_count}_{node}"
kwargs={"user_id":user_id,"user_name":user_name,"user_node":node}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="create_friendship":
user1=random.choice(list(user_node_dict.keys()))
user2=random.choice(list(user_node_dict.keys()))
while user2==user1:
user2=random.choice(list(user_node_dict.keys()))
kwargs={"user_id1":user1,"user_id2":user2}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="create_post":
user_id=random.choice(list(user_node_dict.keys()))
content=f"latency_{i}_{user_id}"
post_id=f"post_{i}_{user_id}"
kwargs={"post_id":post_id,"user_id":user_id,"content":content}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="like_post":
user_id=random.choice(list(user_node_dict.keys()))
post_user_id=random.choice(list(user_node_dict.keys()))
while post_user_id==user_id:
post_user_id=random.choice(list(user_node_dict.keys()))
post_id=f'post_{i}_{post_user_id}'
kwargs={"user_id":user_id,"post_user_id":post_user_id,"post_id":post_id}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="edit_post":
user_id=random.choice(list(user_node_dict.keys()))
post_id=f'post_{i}_{user_id}'
content=f"edited_latency_{i}_{user_id}"
kwargs={"user_id":user_id,"post_id":post_id,"content":content}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="timeline":
user_id=random.choice(list(user_node_dict.keys()))
kwargs={"user_id":user_id}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
elif transaction=="remove_user":
user_id=random.choice(list(user_node_dict.keys()))
kwargs={"user_id":user_id}
make_transaction(transaction,user_node_dict,node_ports,first_hop_only=first_hop_only,send=send,**kwargs)
del user_node_dict[user_id]
time_end=time.time()
print(f"Time taken for {num_itr} {transaction} transactions: {time_end-time_start} seconds")
print(f"Average time per transaction: {(time_end-time_start)/num_itr} seconds")
return user_node_dict,node_ports,processes,(time_end-time_start)/num_itr
if __name__ == "__main__":
transaction_list=["create_user","create_friendship","create_post","like_post","edit_post","timeline","remove_user"]
database_length=[100,1000,10000]
num_itr=50
time_first_hop=np.zeros((len(database_length),len(transaction_list)))
print("Running first hop transactions\n\n\n\n")
for db_size in database_length:
user_node_dict=None
#delete *.db files
for i in range(NUM_NODE):
database_name = f"node_{i}.db"
try:
os.remove(database_name)
except OSError:
pass
for transaction in transaction_list:
print(f"Running {transaction} transactions")
user_node_dict,node_ports,processes,latency=main(DB_SIZE=db_size, num_itr=num_itr,transaction_to_run=transaction,first_hop_only=True,user_node_dict=user_node_dict)
time_first_hop[database_length.index(db_size),transaction_list.index(transaction)]=latency
for process in processes:
process.terminate()
process.join()
df=pd.DataFrame(time_first_hop,index=database_length,columns=transaction_list)
df.to_csv("time_first_hop.csv")
time_constant=np.zeros((len(database_length),len(transaction_list)))
print("Running constant transactions\n\n\n\n")
for db_size in database_length:
user_node_dict=None
#delete *.db files
for i in range(NUM_NODE):
database_name = f"node_{i}.db"
try:
os.remove(database_name)
except OSError:
pass
for transaction in transaction_list:
print(f"Running {transaction} transactions")
user_node_dict,node_ports,processes,latency=main(DB_SIZE=db_size, num_itr=num_itr,transaction_to_run=transaction,send=False,user_node_dict=user_node_dict)
time_constant[database_length.index(db_size),transaction_list.index(transaction)]=latency
for process in processes:
process.terminate()
process.join()
df=pd.DataFrame(time_constant,index=database_length,columns=transaction_list)
df.to_csv("time_constant.csv")
time_total_arr=np.zeros((len(database_length),len(transaction_list)))
print("Running total transactions\n\n\n\n")
for db_size in database_length:
user_node_dict=None
#delete *.db files
for i in range(NUM_NODE):
database_name = f"node_{i}.db"
try:
os.remove(database_name)
except OSError:
pass
for transaction in transaction_list:
print(f"\n\nRunning {transaction} transactions")
user_node_dict,node_ports,processes,latency=main(DB_SIZE=db_size, num_itr=num_itr,transaction_to_run=transaction,user_node_dict=user_node_dict)
time_total_arr[database_length.index(db_size),transaction_list.index(transaction)]=latency
for process in processes:
process.terminate()
process.join()
df=pd.DataFrame(time_total_arr,index=database_length,columns=transaction_list)
df.to_csv("time_total.csv")