-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_data.py
More file actions
164 lines (144 loc) · 9.11 KB
/
Copy pathcreate_data.py
File metadata and controls
164 lines (144 loc) · 9.11 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
import numpy as np
import random
import csv
from scipy import stats
tot_time = 0
test = ""
real_proces_dict = {}
class Server():
def __init__(self,item):
self.name = item[0]
self.time = item[1]["time"]
self.next_servers = item[1]["next_servers"]
class Network(object):
def __init__(self, client_destinations, server_dict):
self.servers = {}
for item in server_dict.items():
self.servers[item[0]] = (Server( item))
self.client_destinations = client_destinations
self.file = open('res/process_log.txt', mode='w', newline='')
self.file.write('<origin_server,destination_server,time,request/response,process_id>\n')
self.file2 = open('res/process_overview.csv', mode='w', newline='')
self.process_writer = csv.writer(self.file2)
self.process_writer.writerow(['process_id', 'server_order'])
def handle_server(self,current_server, process_id, time, server_order, success = True):
if success == False:
return False
global tot_time
server_order.append(current_server.name)
next_servers = current_server.next_servers
if current_server.name != "null" and random.randint(0, 50) == 1: # The process randomly cancels early sometimes
return False
if next_servers == []:
return True
for server in next_servers: # If the task can go to one of multiple server a random one should be chosen
random.shuffle(server)
for s in next_servers:
if not len(s) > 1:
#next_server_in_real.append(server)
real_proces_dict[process_id] = f"{real_proces_dict[process_id]}{s[0]}"
next_servers = [self.servers[server_name[0]] for server_name in next_servers]
for next_server in next_servers:
self.file.write('<'+current_server.name+','+ next_server.name+','+ str(tot_time)+ ',request,' +str(process_id)+'>\n')
new_time = np.random.normal(next_server.time[0], next_server.time[1])
while new_time <= 0:
new_time = np.random.normal(next_server.time[0], next_server.time[1])
tot_time += new_time
success = self.handle_server(next_server, process_id,time, server_order, success)
self.file.write('<'+next_server.name+','+ current_server.name+','+ str(tot_time)+',response,'+ str(process_id)+'>\n')
new_time = np.random.normal(0.2, 0.05)
while new_time <= 0:
new_time = np.random.normal(0.2, 0.05)
tot_time += new_time
if success == False:
return False
def get_process(self, process_id, time):
temp = self.client_destinations.copy()
random.shuffle(temp)
client_destination = [temp[0]]
if not process_id in real_proces_dict:
real_proces_dict[process_id] = ""
real_proces_dict[process_id] = f"{real_proces_dict[process_id]}{self.client_destinations.index(client_destination[0])}"
current_server = Server(["null", {"time": (1,1), "next_servers": client_destination, "capacity": 1, "wait": 1}])
server_order = []
self.handle_server(current_server, process_id, time,server_order)
self.process_writer.writerow([process_id, server_order])
def manual_servers():
return {"buy_book": {"time": (10,3), "next_servers": [["check_availability1","check_availability2","check_availability3"],["verify_email"],["verify_address"],["verify_payment"]]},
"check_availability1": {"time": (12,4), "next_servers": []},
"check_availability2": {"time": (12,2), "next_servers": []},
"check_availability3": {"time": (12,3), "next_servers": []},
"verify_email": {"time": (3,1), "next_servers": []},
"verify_address": {"time": (6,3), "next_servers": []},
"verify_payment": {"time": (7,2), "next_servers": [["credit_check1","credit_check2"],["pay"]]},
"credit_check1": {"time": (6,2), "next_servers": []},
"credit_check2": {"time": (6,2), "next_servers": []},
"pay": {"time": (4,1), "next_servers": [["VISA1","VISA2","ApplePay","PayPal"], ["send_receipt"]]},
"VISA1": {"time": (10,2), "next_servers": []},
"VISA2": {"time": (10,4), "next_servers": []},
"ApplePay": {"time": (10,2), "next_servers": []},
"PayPal": {"time": (8,1), "next_servers": []},
"send_receipt": {"time": (7,4), "next_servers": []},
"get_spotify_subscription": {"time": (3,1), "next_servers": [["verify_email"],["verify_phone1","verify_phone2"],["family","student","regular"],["verify_payment"]]},
"verify_phone1": {"time": (3,2), "next_servers": [["send_sms"],["verify_sms_code"]]},
"verify_phone2": {"time": (3,1), "next_servers": [["send_sms"],["verify_sms_code"]]},
"send_sms": {"time": (5,2), "next_servers": []},
"verify_sms_code": {"time": (6,3), "next_servers": []},
"family": {"time": (5,2), "next_servers": [["verify_address"]]},
"student": {"time": (6,3), "next_servers": [["verify_student"]]},
"verify_student": {"time": (5,2), "next_servers": [["check_university"]]},
"check_university": {"time": (3,1), "next_servers": [["Leiden","Utrecht","Amsterdam","Rotterdam"]]},
"Leiden": {"time": (3,2), "next_servers": []},
"Utrecht": {"time": (2,3), "next_servers": []},
"Amsterdam": {"time": (4,1), "next_servers": []},
"Rotterdam": {"time": (3,2), "next_servers": []},
"regular": {"time": (5,1), "next_servers": []},
"find_facebook_friend": {"time": (3,1), "next_servers": [["search_name"], ["send_friend_request"]]},
"search_name": {"time": (7,3), "next_servers": [["get_search_results1","get_search_results2","get_search_results3"]]},
"get_search_results1": {"time": (5,2), "next_servers": []},
"get_search_results2": {"time": (5,3), "next_servers": []},
"get_search_results3": {"time": (6,2), "next_servers": []},
"send_friend_request": {"time": (2,1), "next_servers": []},
"course_registration": {"time": (3,1), "next_servers": [["login_osiris1","login_osiris2"],["list_courses"],["show_course_5438","show_course_7435","show_course_6437","show_course_7429","show_course_5915"],["enroll"],["send_email"]]},
"login_osiris1": {"time": (4,2), "next_servers": [["authenticate"]]},
"login_osiris2": {"time": (4,2), "next_servers": [["authenticate"]]},
"authenticate": {"time": (7,3), "next_servers": []},
"list_courses": {"time": (5,2), "next_servers": []},
"show_course_5438": {"time": (2,1), "next_servers": []},
"show_course_7435": {"time": (2,1), "next_servers": []},
"show_course_6437": {"time": (3,1), "next_servers": []},
"show_course_7429": {"time": (2,2), "next_servers": []},
"show_course_5915": {"time": (3,1), "next_servers": []},
"enroll": {"time": (6,2), "next_servers": []},
"send_email": {"time": (3,2), "next_servers": []},
"check_rain": {"time": (4,2), "next_servers": [["find_location"],["knmi1","knmi2","knmi3"],["show_map"]]},
"find_location": {"time": (2,1), "next_servers": [["get_gps_data"]]},
"get_gps_data": {"time": (5,2), "next_servers": []},
"knmi1": {"time": (6,2), "next_servers": []},
"knmi2": {"time": (6,1), "next_servers": []},
"knmi3": {"time": (6,2), "next_servers": []},
"show_map": {"time": (4,2), "next_servers": []},
"buy_movie_tickets": {"time": (5,2), "next_servers": [["check_availability1","check_availability2","check_availability3"],["choose_seats"],["verify_email"],["verify_payment"]]},
"choose_seats": {"time": (3,1), "next_servers": []}
}
def run():
client_destinations = [["buy_book"], ["get_spotify_subscription"], ["find_facebook_friend"], ["course_registration"], ["check_rain"], ["buy_movie_tickets"]]
#TODO: create more servers and more pocesses
servers = manual_servers()
network = Network(client_destinations, servers)
process_ids = [str(number) for number in range(1,300)]
global tot_time
network.get_process("0", tot_time)
for process_id in process_ids:
tot_time = random.uniform(0.1, 200)
network.get_process(process_id, tot_time)
network.file.close()
network.file2.close()
# sorting the log file in order of time
import pandas as pd
df = pd.DataFrame(list(real_proces_dict.values()), index=real_proces_dict.keys(), columns=['ProcessValue'])
df.to_csv('res/real_process.csv', index_label='ProcessID')
df = pd.read_csv('res/process_log.txt')
df = df.sort_values('time')
df.to_csv('res/output.txt', index=False)
run()