forked from spcl/serverless-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
57 lines (50 loc) · 1.82 KB
/
function.py
File metadata and controls
57 lines (50 loc) · 1.82 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
import csv
import os.path
import socket
from datetime import datetime
from . 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 = []
i = 0
socket.setdefaulttimeout(3)
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
while i < repetitions + 1:
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 == 5:
print("Can't setup the connection")
break
continue
if i > 0:
times.append([i, send_begin, recv_end])
i += 1
consecutive_failures = 0
server_socket.settimeout(2)
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")
return {"result": key}