forked from openwallet-foundation/owl-akrida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocustClient.py
451 lines (377 loc) · 13.9 KB
/
locustClient.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from locust import events
from json.decoder import JSONDecodeError
import time
import inspect
import json
from typing import Any, Optional
import fcntl
import os
import requests
import signal
from gevent import subprocess
from gevent import select
from gevent import lock as gevent_lock
from uuid import uuid4
SHUTDOWN_TIMEOUT_SECONDS = 10
READ_TIMEOUT_SECONDS = 120 # stdout feedback
ERRORS_BEFORE_RESTART = 10
# How long to wait for verified = true state
VERIFIED_TIMEOUT_SECONDS = int(os.getenv("VERIFIED_TIMEOUT_SECONDS", 20))
START_PORT = json.loads(os.getenv("START_PORT"))
END_PORT = json.loads(os.getenv("END_PORT"))
# Message to send mediator, defaults to "ping"
MESSAGE_TO_SEND = os.getenv("MESSAGE_TO_SEND", "ping")
class PortManager:
def __init__(self):
self.lock = gevent_lock.BoundedSemaphore()
self.ports = list(range(START_PORT, END_PORT))
def getPort(self):
self.lock.acquire()
try:
port = self.ports.pop(0)
return port
finally:
self.lock.release()
def returnPort(self, port):
self.lock.acquire()
try:
self.ports.append(port)
finally:
self.lock.release()
portmanager = PortManager()
def stopwatch(func):
def wrapper(*args, **kwargs):
# get task's function name
previous_frame = inspect.currentframe().f_back
file_name, _, task_name, _, _ = inspect.getframeinfo(previous_frame)
start = time.time()
result = None
try:
result = func(*args, **kwargs)
except Exception as e:
total = int((time.time() - start) * 1000)
events.request_failure.fire(
request_type="TYPE",
name=file_name + "_" + task_name,
response_time=total,
exception=e,
response_length=0,
)
else:
total = int((time.time() - start) * 1000)
events.request_success.fire(
request_type="TYPE",
name=file_name + "_" + task_name,
response_time=total,
response_length=0,
)
return result
return wrapper
class CustomClient:
def __init__(self, host):
self.host = host
self.agent = None
self.errors = 0
self.port = None
self.withMediation = None
_locust_environment = None
@stopwatch
def startup(self, withMediation=True, reinstantiate=False):
if (not self.withMediation) and self.withMediation is None:
self.withMediation = withMediation
try:
if self.port is not None:
portmanager.returnPort(self.port)
self.port = None
self.port = portmanager.getPort()
self.errors = 0
self.agent = subprocess.Popen(
["ts-node", "agent.ts"],
bufsize=0,
universal_newlines=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=False,
)
self.run_command(
{
"cmd": "start",
"withMediation": self.withMediation,
"port": self.port,
"agentConfig": self.agentConfig if reinstantiate else None,
}
)
# Create the wallet for the first time
self.agentConfig = self.readjsonline()["result"]
# self.agentConfig = self.agent.stdout.readline()
# we tried to start the agent and failed
if self.agent is None or self.agent.poll() is not None:
raise Exception("unable to start")
except Exception as e:
self.shutdown()
raise e
def shutdown(self):
# Read output until process closes
try:
if self.port:
portmanager.returnPort(self.port)
self.port = None
# We write the command by hand here because if we call the cmd function
# above we could end up in an infinite loop on shutdown
self.agent.stdin.write(json.dumps({"cmd": "shutdown"}))
self.agent.stdin.write("\n")
self.agent.stdin.flush()
self.agent.communicate(timeout=SHUTDOWN_TIMEOUT_SECONDS)
except Exception as e:
pass
finally:
try:
os.kill(self.agent.pid, signal.SIGTERM)
except Exception as e:
pass
self.agent = None
def ensure_is_running(self):
# Is the agent started?
if not self.agent:
self.startup()
# is the agent still running?
elif self.agent.poll() is None:
# check for closed Pipes
if self.agent.stdout.closed or self.agent.stdin.closed:
self.startup()
else:
return True
else:
self.startup()
def is_running(self):
# Is the agent started?
if not self.agent:
return False
# is the agent still running?
elif self.agent.poll() is None:
# check for closed Pipes
if self.agent.stdout.closed or self.agent.stdin.closed:
return False
else:
return True
else:
return False
return False
def run_command(self, command):
try:
self.agent.stdin.write(json.dumps(command))
self.agent.stdin.write("\n")
self.agent.stdin.flush()
except Exception as e:
# if we get an exception here, we cannot run any new commands
self.shutdown()
raise e
def readjsonline(self):
try:
line = None
raw_line_stdout = None
if not self.agent.stdout.closed:
q = select.poll()
q.register(self.agent.stdout, select.POLLIN)
if q.poll(READ_TIMEOUT_SECONDS * 1000):
raw_line_stdout = self.agent.stdout.readline()
try:
line = json.loads(raw_line_stdout)
except Exception as e:
raise Exception(
"An error was encountered while parsing stdout: ", e
)
else:
raise Exception("Read Timeout")
if not line:
raise Exception("Invalid read.")
if line["error"] != 0:
raise Exception("Error encountered within load testing agent: ", line)
return line
except Exception as e:
self.errors += 1
if self.errors > ERRORS_BEFORE_RESTART:
self.shutdown() ## if we are in bad state we may need to restart...
raise e
@stopwatch
def ping_mediator(self):
self.run_command({"cmd": "ping_mediator"})
line = self.readjsonline()
@stopwatch
def issuer_getinvite(self):
headers = json.loads(os.getenv("ISSUER_HEADERS"))
headers["Content-Type"] = "application/json"
# r = requests.post(
# os.getenv("ISSUER_URL") + "/out-of-band/create-invitation?auto_accept=true",
# json={
# "metadata": {},
# "handshake_protocols": ["https://didcomm.org/didexchange/1.0"],
# "use_public_did": False
# },
# headers=headers
# )
#print("r is ", r, " and r.json is ", r.json())
r = requests.post(
os.getenv("ISSUER_URL") + "/connections/create-invitation?auto_accept=true",
json={"metadata": {}, "my_label": "Test"},
headers=headers,
)
try:
try_var = r.json()["invitation_url"]
except Exception:
raise Exception("Failed to get invitation url. Request: ", r.json())
if r.status_code != 200:
raise Exception(r.content)
r = r.json()
return r
@stopwatch
def issuer_getliveness(self):
headers = json.loads(os.getenv("ISSUER_HEADERS"))
headers["Content-Type"] = "application/json"
r = requests.get(
os.getenv("ISSUER_URL") + "/status",
json={"metadata": {}, "my_label": "Test"},
headers=headers,
)
if r.status_code != 200:
raise Exception(r.content)
r = r.json()
return r
@stopwatch
def accept_invite(self, invite):
try:
self.run_command({"cmd": "receiveInvitation", "invitationUrl": invite})
except Exception:
self.run_command({"cmd": "receiveInvitation", "invitationUrl": invite})
line = self.readjsonline()
return line["connection"]
@stopwatch
def receive_credential(self, connection_id):
self.run_command({"cmd": "receiveCredential"})
headers = json.loads(os.getenv("ISSUER_HEADERS"))
headers["Content-Type"] = "application/json"
issuer_did = os.getenv("CRED_DEF").split(":")[0]
schema_parts = os.getenv("SCHEMA").split(":")
r = requests.post(
os.getenv("ISSUER_URL") + "/issue-credential/send",
json={
"auto_remove": True,
"comment": "Performance Issuance",
"connection_id": connection_id,
"cred_def_id": os.getenv("CRED_DEF"),
"credential_proposal": {
"@type": "issue-credential/1.0/credential-preview",
"attributes": json.loads(os.getenv("CRED_ATTR")),
},
"issuer_did": issuer_did,
"schema_id": os.getenv("SCHEMA"),
"schema_issuer_did": schema_parts[0],
"schema_name": schema_parts[2],
"schema_version": schema_parts[3],
"trace": True,
},
headers=headers,
)
if r.status_code != 200:
raise Exception(r.content)
r = r.json()
line = self.readjsonline()
return r
@stopwatch
def presentation_exchange(self, connection_id):
self.run_command({"cmd": "presentationExchange"})
# From verification side
headers = json.loads(os.getenv("ISSUER_HEADERS")) # headers same
headers["Content-Type"] = "application/json"
verifier_did = os.getenv("CRED_DEF").split(":")[0]
schema_parts = os.getenv("SCHEMA").split(":")
# Might need to change nonce
# TO DO: Generalize schema parts
r = requests.post(
os.getenv("ISSUER_URL") + "/present-proof/send-request",
json={
"auto_remove": False,
"auto_verify": True,
"comment": "Performance Verification",
"connection_id": connection_id,
"proof_request": {
"name": "PerfScore",
"requested_attributes": {
item["name"]: {"name": item["name"]}
for item in json.loads(os.getenv("CRED_ATTR"))
},
"requested_predicates": {},
"version": "1.0",
},
"trace": True,
},
headers=headers,
)
try:
if r.status_code != 200:
raise Exception("Request was not successful: ", r.content)
except JSONDecodeError as e:
raise Exception(
"Encountered JSONDecodeError while parsing the request: ", r
)
line = self.readjsonline()
r = r.json()
pres_ex_id = r["presentation_exchange_id"]
# Want to do a for loop
iteration = 0
try:
while iteration < VERIFIED_TIMEOUT_SECONDS:
g = requests.get(
os.getenv("ISSUER_URL") + f"/present-proof/records/{pres_ex_id}",
headers=headers,
)
if (
g.json()["state"] != "request_sent"
and g.json()["state"] != "presentation_received"
):
"request_sent" and g.json()["state"] != "presentation_received"
break
iteration += 1
time.sleep(1)
if g.json()["verified"] != "true":
raise AssertionError(
f"Presentation was not successfully verified. Presentation in state {g.json()['state']}"
)
except JSONDecodeError as e:
raise Exception(
"Encountered JSONDecodeError while getting the presentation record: ", g
)
@stopwatch
def revoke_credential(self, credential):
headers = json.loads(os.getenv("ISSUER_HEADERS"))
headers["Content-Type"] = "application/json"
issuer_did = os.getenv("CRED_DEF").split(":")[0]
schema_parts = os.getenv("SCHEMA").split(":")
time.sleep(1)
r = requests.post(
os.getenv("ISSUER_URL") + "/revocation/revoke",
json={
"comment": "load test",
"connection_id": credential["connection_id"],
"cred_ex_id": credential["credential_exchange_id"],
"notify": True,
"notify_version": "v1_0",
"publish": True,
},
headers=headers,
)
if r.status_code != 200:
raise Exception(r.content)
@stopwatch
def msg_client(self, connection_id):
self.run_command({"cmd": "receiveMessage"})
headers = json.loads(os.getenv("ISSUER_HEADERS"))
headers["Content-Type"] = "application/json"
r = requests.post(
os.getenv("ISSUER_URL") + "/connections/" + connection_id + "/send-message",
json={"content": MESSAGE_TO_SEND},
headers=headers,
)
r = r.json()
line = self.readjsonline()
return line