-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunexpt.py
172 lines (140 loc) · 4.96 KB
/
runexpt.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
from datetime import datetime
import json
import os
from threading import Thread
# macros
ERROR = -1
TIMEOUT = -2
FAILED = -3
# globals
corpus = open('corpus.txt', 'r')
startedReqs = {}
completedReqs = {}
results = []
killMonitor = False
def monitor(logdir='logs', lat_thresh=60):
while not killMonitor:
startedReqsCopy = startedReqs
for request in startedReqsCopy:
# check whether request has completed
if request in completedReqs:
continue
start = startedReqsCopy[request]
fname = '{dir}/{req}.log'.format(dir=logdir, req=request)
# check for error
with open(fname + '.err') as errfile:
if len(errfile.readlines()):
completedReqs[request] = ERROR
continue
# check for legit complete
with open(fname + '.out') as outfile:
lines = outfile.readlines()
if len(lines):
line = lines[-1]
try:
retval = line.split()[0]
except ValueError:
completedReqs[request] = FAILED
continue
completedReqs[request] = datetime.utcnow()
# request has not returned yet. check for timeout
if (datetime.utcnow() - start).seconds > lat_thresh:
completedReqs[request] = TIMEOUT
continue
for request in completedReqs:
if request not in startedReqsCopy:
# run has seen that it has completed
# accumulated its info and removed it from startedReqs
completedReqs.pop(request)
# inputs: [{'key':<>, 'arg':<>, 'func':<>}]
def run(inputs, logdir='logs', num_parallel=32, num_req=65000):
if len(inputs) <= num_parallel:
num_parallel = len(inputs) - 1
started = 0
finished = 0
nextInputIndex = 0
activeInputIndexes = [-1] * num_parallel
activeReqInfo = [{}] * num_parallel
while finished < num_req:
for loadgenId in range(num_parallel):
activeReq = activeInputIndexes[loadgenId] # currently running req's index in inputs
if (started < num_req and activeReq < 0):
# no active request in this load-generator + creation limit not reached
# create new request
newreq = inputs[nextInputIndex]
activeInputIndex[loadgenId] = nextInputIndex
# find next INACTIVE input
while (nextInputIndex in activeInputIndex):
nextInputIndex = (nextInputIndex + 1) % len(inputs)
# create the request
outfile = '{dir}/{req}.log'.format(dir=logdir, req=started)
key = newreq['key']
func = newreq['func']
arg = newreq['arg']
cmd = "inv -r faasmcli/faasmcli invoke ndp " + func + " -i '" + key + " " + arg + "' 1>" + outfile + ".out 2>" + outfile + ".err &"
os.system(cmd)
activeReqInfo[loadgenId] = {
'reqID' : started,
'key' : key,
'func' : func,
'arg' : arg,
'loadgenID' : loadgenId
'start' : datetime.utcnow()
'end' : -1
'latency' : -1
'timedout' : False
'failed' : False
'error' : False
}
startedReqs.append(started)
startedReqs[started] = activeReqInfo[loadgenId]['start']
started += 1
elif activeReq >= 0:
# check if request has completed
activeReqId = activeReqInfo[loadgenId]['reqID']
completedReqsCopy = completedReqs
if activeReqId in completedReqsCopy:
# request has completed
startedReqs.pop(activeReqId)
completedAt = completedReqsCopy[activeReqId]
if completedAt == ERROR:
activeReqInfo[loadgenId]['error'] = True
elif completedAt == TIMEOUT:
activeReqInfo[loadgenId]['timedout'] = True
elif completedAt == FAILED:
activeReqInfo[loadgenId]['failed'] = True
else:
activeReqInfo[loadgenId]['end'] = completedAt
activeReqInfo[loadgenId]['latency'] = completedAt - activeReqInfo[loadgenId]['start']
activeInputIndexes[loadgenId] = -1
results.append(activeReqInfo[loadgenId])
activeReqInfo[loadgenId] = {}
finished += 1
killMonitor = True
lines = corpus.readlines()
inputsFull = [json.loads(line.replace("'", "\"")) for line in lines]
inputs = [{
'key':'f' + str(inp['id']),
'arg': inp['author'].strip().split()[0],
'func': 'grep'
} for inp in inputsFull[100:1200] if inp['language'] == 'English'] +
[{
'key':'f' + str(inp['id']),
'arg': inp['author'].strip().split()[0],
'func': 'substr'
} for inp in inputsFull[120:1400] if inp['language'] == 'English'] +
logdir = 'logs'
num_parallel = 2
num_warm = 4
num_req = 40
lat_thresh = 60
if __name__ == "__main__":
t1 = Thread(target = run, args = [inputs, logdir, num_parallel, num_req])
t2 = Thread(target = monitor, args = [logdir, lat_thresh])
t1.start()
t2.start()
t1.join()
t2.join()
for res in results:
print(res)
# add post-processing to deal with warmup_reqs etc