forked from openwallet-foundation/acapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance.py
697 lines (624 loc) · 23.5 KB
/
performance.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import asyncio
import logging
import os
import random
import sys
from typing import Tuple
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from runners.support.agent import ( # noqa:E402
DemoAgent,
default_genesis_txns,
start_mediator_agent,
connect_wallet_to_mediator,
)
from runners.support.utils import ( # noqa:E402
check_requires,
log_msg,
log_timer,
progress,
)
CRED_PREVIEW_TYPE = "https://didcomm.org/issue-credential/2.0/credential-preview"
LOGGER = logging.getLogger(__name__)
TAILS_FILE_COUNT = int(os.getenv("TAILS_FILE_COUNT", 100))
class BaseAgent(DemoAgent):
def __init__(
self,
ident: str,
port: int,
prefix: str = None,
**kwargs,
):
if prefix is None:
prefix = ident
super().__init__(ident, port, port + 1, prefix=prefix, **kwargs)
self._connection_id = None
self._connection_ready = None
self.credential_state = {}
self.credential_event = asyncio.Event()
self.revocations = []
self.ping_state = {}
self.ping_event = asyncio.Event()
self.sent_pings = set()
@property
def connection_id(self) -> str:
return self._connection_id
@connection_id.setter
def connection_id(self, conn_id: str):
self._connection_id = conn_id
self._connection_ready = asyncio.Future()
async def detect_connection(self):
if not self._connection_ready:
raise Exception("No connection to await")
await self._connection_ready
self._connection_ready = None
async def handle_oob_invitation(self, message):
pass
async def handle_connections(self, payload):
conn_id = payload["connection_id"]
if (not self.connection_id) and (payload["state"] in ("invitation", "request")):
self.connection_id = conn_id
if conn_id == self.connection_id:
if payload["state"] == "active" and not self._connection_ready.done():
self.log("Connected")
self._connection_ready.set_result(True)
async def handle_issue_credential_v2_0(self, payload):
cred_ex_id = payload["cred_ex_id"]
self.credential_state[cred_ex_id] = payload["state"]
self.credential_event.set()
async def handle_issue_credential_v2_0_indy(self, payload):
rev_reg_id = payload.get("rev_reg_id")
cred_rev_id = payload.get("cred_rev_id")
if rev_reg_id and cred_rev_id:
self.revocations.append((rev_reg_id, cred_rev_id))
async def handle_issuer_cred_rev(self, message):
pass
async def handle_ping(self, payload):
thread_id = payload["thread_id"]
if thread_id in self.sent_pings or (
payload["state"] == "received"
and payload["comment"]
and payload["comment"].startswith("test-ping")
):
self.ping_state[thread_id] = payload["state"]
self.ping_event.set()
async def check_received_creds(self) -> Tuple[int, int]:
while True:
self.credential_event.clear()
pending = 0
total = len(self.credential_state)
for result in self.credential_state.values():
if result != "done":
pending += 1
if self.credential_event.is_set():
continue
return pending, total
async def update_creds(self):
await self.credential_event.wait()
async def check_received_pings(self) -> Tuple[int, int]:
while True:
self.ping_event.clear()
result = {}
for thread_id, state in self.ping_state.items():
if not result.get(state):
result[state] = set()
result[state].add(thread_id)
if self.ping_event.is_set():
continue
return result
async def update_pings(self):
await self.ping_event.wait()
async def send_ping(self, ident: str = None) -> str:
resp = await self.admin_POST(
f"/connections/{self.connection_id}/send-ping",
{"comment": f"test-ping {ident}"},
)
self.sent_pings.add(resp["thread_id"])
def check_task_exception(self, fut: asyncio.Task):
if fut.done():
try:
exc = fut.exception()
except asyncio.CancelledError as e:
exc = e
if exc:
self.log(f"Task raised exception: {str(exc)}")
class AliceAgent(BaseAgent):
def __init__(self, port: int, **kwargs):
super().__init__("Alice", port, seed=None, **kwargs)
self.extra_args = [
"--auto-accept-invites",
"--auto-accept-requests",
"--auto-respond-credential-offer",
"--auto-store-credential",
"--monitor-ping",
]
self.timing_log = "logs/alice_perf.log"
async def fetch_credential_definition(self, cred_def_id):
return await self.admin_GET(f"/credential-definitions/{cred_def_id}")
async def propose_credential(
self,
cred_attrs: dict,
cred_def_id: str,
comment: str = None,
auto_remove: bool = True,
):
cred_preview = {
"attributes": [{"name": n, "value": v} for (n, v) in cred_attrs.items()]
}
await self.admin_POST(
"/issue-credential/send-proposal",
{
"connection_id": self.connection_id,
"cred_def_id": cred_def_id,
"credential_proposal": cred_preview,
"comment": comment,
"auto_remove": auto_remove,
},
)
class FaberAgent(BaseAgent):
def __init__(self, port: int, **kwargs):
super().__init__("Faber", port, seed="random", **kwargs)
self.extra_args = [
"--auto-accept-invites",
"--auto-accept-requests",
"--monitor-ping",
"--auto-respond-credential-proposal",
"--auto-respond-credential-request",
]
self.schema_id = None
self.credential_definition_id = None
self.revocation_registry_id = None
async def publish_defs(self, support_revocation: bool = False):
# create a schema
self.log("Publishing test schema")
version = format(
"%d.%d.%d"
% (random.randint(1, 101), random.randint(1, 101), random.randint(1, 101))
)
schema_body = {
"schema_name": "degree schema",
"schema_version": version,
"attributes": ["name", "date", "degree", "age"],
}
schema_response = await self.admin_POST("/schemas", schema_body)
self.schema_id = schema_response["schema_id"]
self.log(f"Schema ID: {self.schema_id}")
# create a cred def for the schema
self.log("Publishing test credential definition")
credential_definition_body = {
"schema_id": self.schema_id,
"support_revocation": support_revocation,
"revocation_registry_size": TAILS_FILE_COUNT,
}
credential_definition_response = await self.admin_POST(
"/credential-definitions", credential_definition_body
)
self.credential_definition_id = credential_definition_response[
"credential_definition_id"
]
self.log(f"Credential Definition ID: {self.credential_definition_id}")
async def send_credential(
self, cred_attrs: dict, comment: str = None, auto_remove: bool = True
):
cred_preview = {
"@type": CRED_PREVIEW_TYPE,
"attributes": [{"name": n, "value": v} for (n, v) in cred_attrs.items()],
}
await self.admin_POST(
"/issue-credential-2.0/send",
{
"filter": {"indy": {"cred_def_id": self.credential_definition_id}},
"auto_remove": auto_remove,
"comment": comment,
"connection_id": self.connection_id,
"credential_preview": cred_preview,
},
)
async def revoke_credential(self, cred_ex_id: str):
await self.admin_POST(
"/revocation/revoke",
{
"cred_ex_id": cred_ex_id,
"publish": True,
},
)
async def main(
start_port: int,
threads: int = 20,
action: str = None,
show_timing: bool = False,
multitenant: bool = False,
mediation: bool = False,
multi_ledger: bool = False,
use_did_exchange: bool = False,
revocation: bool = False,
tails_server_base_url: str = None,
issue_count: int = 300,
wallet_type: str = None,
):
if multi_ledger:
genesis = None
multi_ledger_config_path = "./demo/multi_ledger_config.yml"
else:
genesis = await default_genesis_txns()
multi_ledger_config_path = None
if not genesis:
print("Error retrieving ledger genesis transactions")
sys.exit(1)
alice = None
faber = None
alice_mediator_agent = None
faber_mediator_agent = None
run_timer = log_timer("Total runtime:")
run_timer.start()
try:
alice = AliceAgent(
start_port,
genesis_data=genesis,
genesis_txn_list=multi_ledger_config_path,
timing=show_timing,
multitenant=multitenant,
mediation=mediation,
wallet_type=wallet_type,
)
await alice.listen_webhooks(start_port + 2)
faber = FaberAgent(
start_port + 3,
genesis_data=genesis,
genesis_txn_list=multi_ledger_config_path,
timing=show_timing,
tails_server_base_url=tails_server_base_url,
multitenant=multitenant,
mediation=mediation,
wallet_type=wallet_type,
)
await faber.listen_webhooks(start_port + 5)
await faber.register_did()
with log_timer("Startup duration:"):
await alice.start_process()
await faber.start_process()
if mediation:
alice_mediator_agent = await start_mediator_agent(
start_port + 8, genesis, multi_ledger_config_path
)
if not alice_mediator_agent:
raise Exception("Mediator agent returns None :-(")
faber_mediator_agent = await start_mediator_agent(
start_port + 11, genesis, multi_ledger_config_path
)
if not faber_mediator_agent:
raise Exception("Mediator agent returns None :-(")
else:
alice_mediator_agent = None
faber_mediator_agent = None
with log_timer("Connect duration:"):
if multitenant:
# create an initial managed sub-wallet (also mediated)
await alice.register_or_switch_wallet(
"Alice.initial",
webhook_port=None,
mediator_agent=alice_mediator_agent,
)
await faber.register_or_switch_wallet(
"Faber.initial",
public_did=True,
webhook_port=None,
mediator_agent=faber_mediator_agent,
)
elif mediation:
# we need to pre-connect the agent(s) to their mediator (use the same
# mediator for both)
if not await connect_wallet_to_mediator(alice, alice_mediator_agent):
log_msg("Mediation setup FAILED :-(")
raise Exception("Mediation setup FAILED :-(")
if not await connect_wallet_to_mediator(faber, faber_mediator_agent):
log_msg("Mediation setup FAILED :-(")
raise Exception("Mediation setup FAILED :-(")
invite = await faber.get_invite(use_did_exchange)
await alice.receive_invite(invite["invitation"])
await asyncio.wait_for(faber.detect_connection(), 30)
if action != "ping":
with log_timer("Publish duration:"):
await faber.publish_defs(revocation)
# cache the credential definition
await alice.fetch_credential_definition(faber.credential_definition_id)
if show_timing:
await alice.reset_timing()
await faber.reset_timing()
if mediation:
await alice_mediator_agent.reset_timing()
await faber_mediator_agent.reset_timing()
batch_size = 100
semaphore = asyncio.Semaphore(threads)
def done_propose(fut: asyncio.Task):
semaphore.release()
alice.check_task_exception(fut)
def done_send(fut: asyncio.Task):
semaphore.release()
faber.check_task_exception(fut)
def test_cred(index: int) -> dict:
return {
"name": "Alice Smith",
"date": f"{2020+index}-05-28",
"degree": "Maths",
"age": "24",
}
async def propose_credential(index: int):
await semaphore.acquire()
comment = f"propose test credential {index}"
attributes = test_cred(index)
asyncio.ensure_future(
alice.propose_credential(
attributes, faber.credential_definition_id, comment, not revocation
)
).add_done_callback(done_propose)
async def send_credential(index: int):
await semaphore.acquire()
comment = f"issue test credential {index}"
attributes = test_cred(index)
asyncio.ensure_future(
faber.send_credential(attributes, comment, not revocation)
).add_done_callback(done_send)
async def check_received_creds(agent, issue_count, pb):
reported = 0
iter_pb = iter(pb) if pb else None
while True:
pending, total = await agent.check_received_creds()
complete = total - pending
if reported == complete:
await asyncio.wait_for(agent.update_creds(), 30)
continue
if iter_pb and complete > reported:
try:
while next(iter_pb) < complete:
pass
except StopIteration:
iter_pb = None
reported = complete
if reported == issue_count:
break
async def send_ping(index: int):
await semaphore.acquire()
asyncio.ensure_future(faber.send_ping(str(index))).add_done_callback(
done_send
)
async def check_received_pings(agent, issue_count, pb):
reported = 0
iter_pb = iter(pb) if pb else None
while True:
pings = await agent.check_received_pings()
complete = sum(len(tids) for tids in pings.values())
if complete == reported:
await asyncio.wait_for(agent.update_pings(), 30)
continue
if iter_pb and complete > reported:
try:
while next(iter_pb) < complete:
pass
except StopIteration:
iter_pb = None
reported = complete
if reported >= issue_count:
break
if action == "ping":
recv_timer = faber.log_timer(f"Completed {issue_count} ping exchanges in")
batch_timer = faber.log_timer(f"Started {batch_size} ping exchanges in")
else:
recv_timer = faber.log_timer(
f"Completed {issue_count} credential exchanges in"
)
batch_timer = faber.log_timer(
f"Started {batch_size} credential exchanges in"
)
recv_timer.start()
batch_timer.start()
with progress() as pb:
receive_task = None
try:
if action == "ping":
issue_pg = pb(range(issue_count), label="Sending pings")
receive_pg = pb(range(issue_count), label="Responding pings")
check_received = check_received_pings
send = send_ping
completed = f"Done sending {issue_count} pings in"
else:
issue_pg = pb(range(issue_count), label="Issuing credentials")
receive_pg = pb(range(issue_count), label="Receiving credentials")
check_received = check_received_creds
if action == "propose":
send = propose_credential
else:
send = send_credential
completed = f"Done starting {issue_count} credential exchanges in"
issue_task = asyncio.ensure_future(
check_received(faber, issue_count, issue_pg)
)
issue_task.add_done_callback(faber.check_task_exception)
receive_task = asyncio.ensure_future(
check_received(alice, issue_count, receive_pg)
)
receive_task.add_done_callback(alice.check_task_exception)
with faber.log_timer(completed):
for idx in range(0, issue_count):
await send(idx + 1)
if not (idx + 1) % batch_size and idx < issue_count - 1:
batch_timer.reset()
await issue_task
await receive_task
except KeyboardInterrupt:
if receive_task:
receive_task.cancel()
print("Cancelled")
recv_timer.stop()
avg = recv_timer.duration / issue_count
item_short = "ping" if action == "ping" else "cred"
item_long = "ping exchange" if action == "ping" else "credential"
faber.log(f"Average time per {item_long}: {avg:.2f}s ({1/avg:.2f}/s)")
if alice.postgres:
await alice.collect_postgres_stats(f"{issue_count} {item_short}s")
for line in alice.format_postgres_stats():
alice.log(line)
if faber.postgres:
await faber.collect_postgres_stats(f"{issue_count} {item_short}s")
for line in faber.format_postgres_stats():
faber.log(line)
if revocation and faber.revocations:
(rev_reg_id, cred_rev_id) = next(iter(faber.revocations))
print(
f"Revoking and publishing cred rev id {cred_rev_id} "
f"from rev reg id {rev_reg_id}"
)
if show_timing:
timing = await alice.fetch_timing()
if timing:
for line in alice.format_timing(timing):
alice.log(line)
timing = await faber.fetch_timing()
if timing:
for line in faber.format_timing(timing):
faber.log(line)
if mediation:
timing = await alice_mediator_agent.fetch_timing()
if timing:
for line in alice_mediator_agent.format_timing(timing):
alice_mediator_agent.log(line)
timing = await faber_mediator_agent.fetch_timing()
if timing:
for line in faber_mediator_agent.format_timing(timing):
faber_mediator_agent.log(line)
finally:
terminated = True
try:
if alice:
await alice.terminate()
except Exception:
LOGGER.exception("Error terminating agent:")
terminated = False
try:
if faber:
await faber.terminate()
except Exception:
LOGGER.exception("Error terminating agent:")
terminated = False
try:
if alice_mediator_agent:
await alice_mediator_agent.terminate()
if faber_mediator_agent:
await faber_mediator_agent.terminate()
except Exception:
LOGGER.exception("Error terminating agent:")
terminated = False
run_timer.stop()
await asyncio.sleep(0.1)
if not terminated:
os._exit(1)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Runs an automated credential issuance performance demo."
)
parser.add_argument(
"-c",
"--count",
type=int,
default=300,
help="Set the number of credentials to issue",
)
parser.add_argument(
"-p",
"--port",
type=int,
default=8030,
metavar=("<port>"),
help="Choose the starting port number to listen on",
)
parser.add_argument(
"--ping",
action="store_true",
default=False,
help="Only send ping messages between the agents",
)
parser.add_argument(
"--multitenant", action="store_true", help="Enable multitenancy options"
)
parser.add_argument(
"--mediation", action="store_true", help="Enable mediation functionality"
)
parser.add_argument(
"--multi-ledger",
action="store_true",
help=(
"Enable multiple ledger mode, config file can be found "
"here: ./demo/multi_ledger_config.yml"
),
)
parser.add_argument(
"--did-exchange",
action="store_true",
help="Use DID-Exchange protocol for connections",
)
parser.add_argument(
"--proposal",
action="store_true",
default=False,
help="Start credential exchange with a credential proposal from Alice",
)
parser.add_argument(
"--revocation", action="store_true", help="Enable credential revocation"
)
parser.add_argument(
"--tails-server-base-url",
type=str,
metavar="<tails-server-base-url>",
help="Tails server base url",
)
parser.add_argument(
"-t",
"--threads",
type=int,
default=10,
help="Set the number of concurrent exchanges to start",
)
parser.add_argument(
"--timing", action="store_true", help="Enable detailed timing report"
)
parser.add_argument(
"--wallet-type",
type=str,
metavar="<wallet-type>",
help="Set the agent wallet type",
)
args = parser.parse_args()
if args.did_exchange and args.mediation:
raise Exception(
"DID-Exchange connection protocol is not (yet) compatible with mediation"
)
tails_server_base_url = args.tails_server_base_url or os.getenv("PUBLIC_TAILS_URL")
if args.revocation and not tails_server_base_url:
raise Exception(
"If revocation is enabled, --tails-server-base-url must be provided"
)
action = "issue"
if args.proposal:
action = "propose"
if args.ping:
action = "ping"
check_requires(args)
try:
asyncio.get_event_loop().run_until_complete(
main(
args.port,
args.threads,
action,
args.timing,
args.multitenant,
args.mediation,
args.multi_ledger,
args.did_exchange,
args.revocation,
tails_server_base_url,
args.count,
args.wallet_type,
)
)
except KeyboardInterrupt:
os._exit(1)