-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathfunction.py
More file actions
74 lines (68 loc) · 2.53 KB
/
Copy pathfunction.py
File metadata and controls
74 lines (68 loc) · 2.53 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
import csv
import json
import os
import socket
from datetime import datetime
from time import sleep
import storage
def handler(event):
request_id = event['request-id']
address = event['server-address']
port = event['server-port']
repetitions = event['repetitions']
output_bucket = event.get('bucket').get('bucket')
output_prefix = event.get('bucket').get('output')
times = []
print("Starting communication with {}:{}".format(address, port))
i = 0
socket.setdefaulttimeout(4)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', 0))
message = request_id.encode('utf-8')
adr = (address, port)
consecutive_failures = 0
measurements_not_smaller = 0
cur_min = 0
while i < 1000:
try:
send_begin = datetime.now().timestamp()
server_socket.sendto(message, adr)
msg, addr = server_socket.recvfrom(1024)
recv_end = datetime.now().timestamp()
except socket.timeout:
i += 1
consecutive_failures += 1
if consecutive_failures == 7:
print("Can't setup the connection")
break
continue
if i > 0:
times.append([i, send_begin, recv_end])
cur_time = recv_end - send_begin
print("Time {} Min Time {} NotSmaller {}".format(cur_time, cur_min, measurements_not_smaller))
if cur_time > cur_min and cur_min > 0:
measurements_not_smaller += 1
if measurements_not_smaller == repetitions:
message = "stop".encode('utf-8')
server_socket.sendto(message, adr)
break
else:
cur_min = cur_time
measurements_not_smaller = 0
i += 1
consecutive_failures = 0
server_socket.settimeout(4)
server_socket.close()
if consecutive_failures != 5:
with open('/tmp/data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(["id", "client_send", "client_rcv"])
for row in times:
writer.writerow(row)
client = storage.storage.get_instance()
filename = 'results-{}.csv'.format(request_id)
key = client.upload(output_bucket, os.path.join(output_prefix, filename), '/tmp/data.csv')
else:
key = None
return { 'result': {'bucket-key': key, 'timestamp': event['income-timestamp']} }