-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
377 lines (233 loc) · 10.4 KB
/
script.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
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
import os
import time
import math
import subprocess
import concurrent.futures
from sklearn.linear_model import LinearRegression
from utilities import *
PATH_TO_SCRIPT = os.path.join("multimedia", "hw-2", "script")
# project specification
SELECTED_SERVER_CITY = "Los Angeles"
INSTANCES = 25
STEP_BETWEEN_LENGTHS = 10
# script settings
SHOW_PLOTS = False
SAVE_TO_FILE = True
SAVE_IMAGES = True
def ping_server(server, instances, length):
filename = f"{LOGS_PATH}{city}-RTT.txt"
if not length % (STEP_BETWEEN_LENGTHS * 5) or length == payload_lengths[-1]:
print(f" Payload length: {length}", end="\r")
cmd = f"psping -n {instances} -l {length} -i 0 -w 0 {server}"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if SAVE_TO_FILE:
with open(f"{filename}", "a") as file:
file.write(result.stdout)
file.write("\n\n")
millisecs_vector = []
lines = result.stdout.split("\n")
for line in lines:
if "Reply from" in line:
duration = float(line.split(": ")[1].split("ms")[0])
millisecs_vector.append(duration)
return length, millisecs_vector
def get_links_from_tracert(server: str) -> int:
filename = f"{LOGS_PATH}{city}-links-tracert.txt"
# get number of links from tracert
cmd = f"tracert {server}"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if SAVE_TO_FILE:
with open(f"{filename}", "a") as file:
file.write(result.stdout)
last_link_line = result.stdout.split("\n")[-4]
return int(last_link_line.split(" ")[1])
def get_links_from_ping(server: str) -> int:
filename = f"{LOGS_PATH}{city}-links-ping.txt"
for ttl in range(20, 0, -1):
cmd = f"ping {server} -i {ttl}"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if SAVE_TO_FILE:
with open(f"{filename}", "a") as file:
file.write(result.stdout)
file.write("\n\n")
if "TTL scaduto durante il passaggio" in result.stdout:
return (ttl + 1)
def plot_all_data() -> None:
if not SAVE_IMAGES and not SHOW_PLOTS:
return
##### all latencies #####
# calculate the true number of retrived data in the case of some lost packages
exploit_lengths_bits = []
latencies = []
for key, value in stats.items():
for item in value:
# convert directly into bits whil building
exploit_lengths_bits.append( 8 * (key + 28))
latencies.append(item)
random_colors = np.random.rand(len(exploit_lengths_bits), 3)
plt.figure(figsize=(10, 6))
plt.scatter(exploit_lengths_bits, latencies, edgecolors=random_colors, facecolors="none", s=10)
plt.xlabel("Packet size - Bits")
plt.ylabel("Round-Trip-Time(k) - millisecs")
plt.title("Total gathered latencies")
plt.grid(True)
if SAVE_IMAGES:
plt.savefig(f"{IMGS_PATH}{city}-total-latencies.png")
##### max latencies #####
plt.figure(figsize=(10, 6))
plt.scatter(payload_lengths_bit, list(max_values.values()), edgecolors="magenta", facecolors="none", s=25)
plt.xlabel("Packet size - Bits")
plt.ylabel("Round-Trip-Time(k) - millisecs")
plt.title("Maximum latencies")
plt.grid(True)
if SAVE_IMAGES:
plt.savefig(f"{IMGS_PATH}{city}-max-latencies.png")
##### avg latencies #####
plt.figure(figsize=(10, 6))
plt.scatter(payload_lengths_bit, list(average_values.values()), edgecolors="lime", facecolors="none", s=25)
plt.xlabel("Packet size - Bits")
plt.ylabel("Round-Trip-Time(k) - millisecs")
plt.title("Average latencies")
plt.grid(True)
if SAVE_IMAGES:
plt.savefig(f"{IMGS_PATH}{city}-avg-latencies.png")
##### standard deviation #####
plt.figure(figsize=(10, 6))
plt.scatter(payload_lengths_bit, list(standard_deviations.values()), edgecolors="dodgerblue", facecolors="none", s=25)
plt.xlabel("Packet size - Bits")
plt.ylabel("Round-Trip-Time(k) - millisecs")
plt.title("Standard deviation")
plt.grid(True)
if SAVE_IMAGES:
plt.savefig(f"{IMGS_PATH}{city}-standard-deviation.png")
##### min latencies and predictions #####
# generate min and alpha-line
x_line = np.linspace(min(payload_lengths_bit), max(payload_lengths_bit), 100)
y_line = alpha * x_line + reg.intercept_
plt.figure(figsize=(10, 6))
plt.scatter(payload_lengths_bit, list(min_values.values()), edgecolors="red", facecolors="none", s=25)
plt.plot(x_line, y_line, color="blue")
plt.title("Minimum latencies and fitting")
plt.xlabel("Packet size - Bits")
plt.ylabel("Round-Trip-Time(k) - millisecs")
plt.grid(True)
if SAVE_IMAGES:
plt.savefig(f"{IMGS_PATH}{city}-min-latencies.png")
### displays all plots
if SHOW_PLOTS:
plt.show()
if __name__ == "__main__":
initial = time.time()
# Definitions of constants
SLEEP_TIME = 0.5
LOGS_PATH = os.path.join(PATH_TO_SCRIPT, "logs") + "\\"
IMGS_PATH = os.path.join(PATH_TO_SCRIPT, "imgs") + "\\"
SERVERS = {
"Atlanta" : "atl.speedtest.clouvider.net",
"New York City" : "nyc.speedtest.clouvider.net",
"London" : "lon.speedtest.clouvider.net",
"Los Angeles" : "la.speedtest.clouvider.net",
"Paris" : "paris.testdebit.info",
"Lillie" : "lille.testdebit.info",
"Lyon" : "lyon.testdebit.info",
"Aix-Marseille" : "aix-marseille.testdebit.info",
"Bordeaux" : "bordeaux.testdebit.info"
}
# set choosen options
server = SERVERS[SELECTED_SERVER_CITY]
city = server.split(".")[0]
payload_lengths = range(10, 1471, STEP_BETWEEN_LENGTHS)
print(f"\nServer: \033[1m\033[34m{server}\033[0m\n\n")
# create log dir if it does not exist
if SAVE_TO_FILE and not os.path.exists(LOGS_PATH):
os.makedirs(LOGS_PATH)
# delete old log files in the directory
delete_files_in_directory(LOGS_PATH, city)
# create img dir if does not exist
if SAVE_IMAGES and not os.path.exists(IMGS_PATH):
os.makedirs(IMGS_PATH)
# delete old img files in the directory
delete_files_in_directory(IMGS_PATH, city)
#### count number of links
print_task(1, number_color="red")
# using tracetr
print(f" Number of links found with \033[1mtracert\033[0m:", end="")
start = time.time()
tracert_links = get_links_from_tracert(server)
print(f" {tracert_links}")
print(f" { round(time.time() - start, 2) } sec")
# using multiple ping
start = time.time()
print(" Number of links found with muliple \033[1mping\033[0m:", end="")
ping_links = get_links_from_ping(server)
print(f" {ping_links}")
print(f" { round(time.time() - start, 2) } sec")
### Round Trip Time
print_task(2, number_color="red")
start = time.time()
stats = {}
# using ThreadPoolExecutor to imporve computational capabilites
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
futures = []
for length in payload_lengths:
future = executor.submit(ping_server, server, INSTANCES, length)
futures.append(future)
time.sleep(SLEEP_TIME)
for future in concurrent.futures.as_completed(futures):
length, millisecs_vector = future.result()
stats[length] = millisecs_vector
# order stats by length
stats = dict(sorted(stats.items()))
filename = f"{LOGS_PATH}{city}-RTT-total.txt"
if SAVE_TO_FILE:
save_dictionary(stats, filename)
print(f"\n Processed {len(payload_lengths) * INSTANCES} pings")
print(f" { round(time.time() - start, 2) } sec")
start = time.time()
# compute the min, max and avg of stats
print(" Computed Min, Max, Avg and StdDev")
max_values = {}
min_values = {}
average_values = {}
standard_deviations = {}
for key, value in stats.items():
max_values[key] = max(value)
min_values[key] = min(value)
average_values[key] = sum(value) / len(value)
standard_deviations[key] = math.sqrt(sum((x - average_values[key]) ** 2 for x in value) / len(value))
print(f" { round(time.time() - start, 2) } sec")
if SAVE_TO_FILE:
filename = f"{LOGS_PATH}{city}-RTT-min.txt"
save_dictionary(min_values, filename)
filename = f"{LOGS_PATH}{city}-RTT-max.txt"
save_dictionary(max_values, filename)
filename = f"{LOGS_PATH}{city}-RTT-avg.txt"
save_dictionary(average_values, filename)
filename = f"{LOGS_PATH}{city}-RTT-std.txt"
save_dictionary(standard_deviations, filename)
### alpha-coefficient and throughput
print_task(3, number_color="red")
start = time.time()
payload_lengths_bit = [8 * (length + 28) for length in payload_lengths]
# use linear regression to retrive alpha, need to transform list into np.arrays
reg = LinearRegression().fit(
np.array(payload_lengths_bit).reshape(-1, 1), # transpose of payload lengths
np.array(list(min_values.values()))
)
alpha = reg.coef_[0]
# compute throughput
throughput_identical_link = 2 * tracert_links / alpha
throughput_bottleneck = 2 / alpha
print(f" alpha = {round(alpha, 6)}")
print(f" Throughput with identical links = {round(throughput_identical_link, 3)}")
print(f" Throughput in a bottleneck scenario = {round(throughput_bottleneck, 3)}")
print(f" { round(time.time() - start, 2) } sec")
filename = f"{LOGS_PATH}{city}-throughput.txt"
if SAVE_TO_FILE:
with open(f"{filename}", "a") as file:
file.write(f"alpha = {round(alpha, 6)}\n")
file.write(f"Throughput with identical links = {round(throughput_identical_link, 3)}\n")
file.write(f"Throughput in a bottleneck scenario = {round(throughput_bottleneck, 3)}\n")
plot_all_data()
print(f"\n\n\nTotal execution time: { round(time.time() - initial, 2) } sec")
print(f" ~ { round((time.time() - initial) / 60, ) } min\n")