-
Notifications
You must be signed in to change notification settings - Fork 0
/
expt.py
201 lines (159 loc) · 5.67 KB
/
expt.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
from datetime import datetime
import json
import os
import pycurl
import random
import sys
from threading import Thread
# macros
SUCCESS = "SUCCESS"
RUNNING = "RUNNING"
CREATED = "CREATED"
ERROR = "ERROR"
TIMEOUT = "TIMEOUT"
FAILED = "FAILED"
# globals
corpusFile = open('corpus.txt', 'r')
EXPTTS = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
workerPort = '8080'
workerHost = 'worker'
timeout = 15
def perform_curl_request(request):
try:
request.start = datetime.utcnow()
request.curl.perform()
except:
request.status = ERROR
if (datetime.utcnow() - request.start).seconds >= timeout:
request.status = TIMEOUT
class Request:
nextRequestID = 0
def __init__(self, inp, loadgenID):
self.requestID = Request.nextRequestID
Request.nextRequestID += 1
self.inputKey = inp['key']
self.inputFunc = inp['func']
self.inputArg = inp['arg']
self.loadgenID = loadgenID
self.start = -1
self.end = -1
self.latency = -1
self.status = CREATED
self.response = None
self.curl = None
def writeback(self, buf):
if self.status == ERROR or self.status == TIMEOUT:
return
self.response = buf.decode('utf-8')
self.end = datetime.utcnow()
self.latency = self.end - self.start
try:
i = int(self.response.strip().split()[0])
self.status = SUCCESS
except:
self.status = FAILED
def init_curl(self):
payload = '{"async": false, "user": "ndp", "function": "' + self.inputFunc + '", "input_data": "' + self.inputKey + ' ' + self.inputArg + '"}'
url = "http://" + workerHost + ":" + workerPort + "/f/"
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.POSTFIELDS, payload)
c.setopt(c.HTTPHEADER, ["Content-Type: application/json"])
c.setopt(c.WRITEFUNCTION, self.writeback)
c.setopt(c.TIMEOUT, timeout)
self.curl = c
def show(self):
if self.status == SUCCESS:
self.response = self.response.split()
if len(self.response) > 3:
self.response = self.response[:3] + ['...']
self.latency = self.latency.seconds + (self.latency.microseconds / 10**6)
print("{id} {func} {key} {arg} {status} {latency} {resp}".format(id=self.requestID, func=self.inputFunc, key=self.inputKey, arg=self.inputArg, status=self.status, latency=self.latency, resp=self.response))
def run(exptCfg):
name = exptCfg['name']
inputs = exptCfg['inputs']
num_parallel = exptCfg['num_parallel']
num_req = exptCfg['num_req']
print("Starting Expt {name} req={req} par={par}".format(name=name, req=num_req, par=num_parallel))
finishedRequests = []
activeRequests = [None] * num_parallel
nextInputIndex = 0
threads = []
sTime = datetime.utcnow()
while len(finishedRequests) < num_req:
for loadgenID in range(num_parallel):
if activeRequests[loadgenID] == None:
# create new request
if Request.nextRequestID < num_req:
request = Request(inputs[nextInputIndex], loadgenID)
request.init_curl()
nextInputIndex = (nextInputIndex + 1) % len(inputs)
t = Thread(target=perform_curl_request, args=[request])
threads.append(t)
t.start()
request.status = RUNNING
activeRequests[loadgenID] = request
elif activeRequests[loadgenID].status != RUNNING:
finishedRequests.append(activeRequests[loadgenID])
activeRequests[loadgenID] = None
print("\rStarted:{started}\tCompleted:{completed}\tRemaining:{remaining:05d}".format(started=Request.nextRequestID, completed=len(finishedRequests), remaining=(num_req - Request.nextRequestID)), end='')
print("\nFinished!")
for t in threads:
t.join()
eTime = datetime.utcnow()
return finishedRequests, (eTime - sTime).seconds + ((eTime - sTime).microseconds / 10**6)
def postprocess(cfg, results, e2etime):
statsfile = "logs/stats.{name}.{dt}".format(name=cfg['name'], dt=EXPTTS)
os.makedirs('logs', exist_ok=True)
num_err = 0
num_timeout = 0
num_failed = 0
num_succ = 0
latencies = []
for res in results:
res.show()
if res.status == ERROR:
num_err += 1
elif res.status == TIMEOUT:
num_timeout += 1
elif res.status == FAILED:
num_failed += 1
else:
num_succ += 1
latencies.append(res.latency)
with open(statsfile, 'w') as stats:
stats.write('REQS {reqs}\n'.format(reqs=cfg['num_req']))
stats.write('LOADGENS {par}\n'.format(par=cfg['num_parallel']))
stats.write('SUCCESS {succ}\n'.format(succ=num_succ))
stats.write('FAILED {fail}\n'.format(fail=num_failed))
stats.write('TIMEOUT {tmo}\n'.format(tmo=num_timeout))
stats.write('ERROR {err}\n'.format(err=num_err))
stats.write('E2E {e2e}\n'.format(e2e=e2etime))
stats.write('AVGLAT {avg:.06f}\n'.format(avg=(sum(latencies) / num_succ)))
stats.write('LATENCIES {lat}\n'.format(lat=" ".join([str(l) for l in latencies])))
if __name__ == "__main__":
corpus = []
for line in corpusFile.readlines():
try:
corpus.append(json.loads(line.replace("'", "\"")))
except:
pass
inputs = []
for entry in corpus:
try:
key = "f" + str(entry["id"])
arg = entry["author"].strip().split()[0]
payload = '{"data": "' + key + ' ' + arg + '"}'
c = pycurl.Curl()
c.setopt(c.POSTFIELDS, payload)
assert(entry["language"] == "English")
inputs.append({'key':key, 'arg':arg, 'func':"substr"})
inputs.append({'key':key, 'arg':arg, 'func':"grep"})
except:
pass
random.shuffle(inputs)
CFG_DIFF_OBJ = {'name':'DIFF_OBJ', 'inputs':inputs, 'num_req':100, 'num_parallel':4}
CFG_SAME_OBJ = {'name':'SAME_OBJ', 'inputs':[inputs[0]], 'num_req':100, 'num_parallel':4}
cfg = CFG_DIFF_OBJ
results, e2e = run(cfg)
postprocess(cfg, results, e2e)