-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload-scale.py
150 lines (115 loc) · 3.75 KB
/
load-scale.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
#!/usr/bin/env python3
import sys
import typing
import urllib.request
import urllib.error
import time
import threading
import multiprocessing as mp
import tqdm
def invoke(
thread: int,
endpoint: str,
function: str,
parameter: str,
load_time: float,
q: "mp.Queue[typing.Tuple[int, float, float, int]]",
) -> None:
# run the function
data = (str(thread) + "-" + parameter).encode("ascii")
start_time = time.perf_counter()
end_time = start_time + load_time
while time.perf_counter() < end_time:
try:
start = time.perf_counter()
r = urllib.request.Request(
endpoint,
data=data,
headers={"Content-Type": "text/plain"},
)
res = urllib.request.urlopen(r)
end = time.perf_counter()
# get the result
q.put((thread, start, end, res.status))
except urllib.error.HTTPError as e:
res = e.code
end = time.perf_counter()
# get the result
q.put((thread, start, end, res))
except Exception as e:
print(e)
q.put((thread, 0, 0, 0))
q.put((thread, -1, -1, -1))
return
if __name__ == "__main__":
# get args
# tinyfaas endpoint
# function name
# parameter
# time to run (in seconds)
# number of threads
# frequency of requests (per second per thread)
# output file
if len(sys.argv) != 8:
print(
"usage: python3 load-scale.py <endpoint> <function> <parameter> <load time s> <threads> <output <timeout_s>"
)
sys.exit(1)
try:
host = sys.argv[1]
function = sys.argv[2]
parameter = sys.argv[3]
load_time = int(sys.argv[4])
threads = int(sys.argv[5])
output = sys.argv[6]
timeout_s = int(sys.argv[7])
if threads < 1 or load_time < 1 or timeout_s < 1:
raise ValueError("invalid arguments")
except:
print("invalid arguments")
print(
"usage: python3 load-scale.py <endpoint> <function> <parameter> <load time s> <threads> <output <timeout_s>"
)
sys.exit(1)
print(
f"parameters: host={host}, function={function}, parameter={parameter}, load_time={load_time}, threads={threads}, output={output}, timeout_s={timeout_s}"
)
# run the function
q: "mp.Queue[typing.Tuple[int, float, float, int]]" = mp.Queue()
p: typing.Dict[int, mp.Process] = {}
for i in range(threads):
# staggered start
time.sleep(1 / threads)
p[i] = mp.Process(
target=invoke,
args=(
i,
host,
function,
parameter,
load_time + (1 / threads * (threads - 1)),
q,
),
)
p[i].start()
start_time = time.perf_counter()
with open(output, "w") as f:
f.write("start,end,res\n")
with tqdm.tqdm(total=float("inf"), position=1, miniters=500) as pbar:
while time.perf_counter() - start_time <= load_time:
try:
t, start, end, res = q.get(timeout=0.1)
if start == -1:
# thread is done
p[t].join()
del p[t]
else:
# print(f"{t}: {end-start:.3f}s")
f.write(f"{start},{end},{res}\n")
pbar.update(1)
except:
# note that this timeout will not work if results still come in
pass
for t in p:
p[t].terminate()
print("experiment finished in {:.3f}s".format(time.perf_counter() - start_time))