forked from openwallet-foundation/acapy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.py
More file actions
363 lines (303 loc) · 12 KB
/
Copy pathperformance.py
File metadata and controls
363 lines (303 loc) · 12 KB
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
import asyncio
import logging
import os
import random
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # noqa
from runners.support.agent import DemoAgent, default_genesis_txns
from runners.support.utils import log_timer, progress, require_indy
LOGGER = logging.getLogger(__name__)
class BaseAgent(DemoAgent):
def __init__(
self,
ident: str,
port: int,
prefix: str = None,
use_routing: bool = False,
**kwargs,
):
if prefix is None:
prefix = ident
super().__init__(ident, port, port + 1, prefix=prefix, **kwargs)
self._connection_id = None
self._connection_ready = None
@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 get_invite(self, accept: str = "auto"):
result = await self.admin_POST(
"/connections/create-invitation", params={"accept": accept}
)
self.connection_id = result["connection_id"]
return result["invitation"]
async def receive_invite(self, invite, accept: str = "auto"):
result = await self.admin_POST(
"/connections/receive-invitation", invite, params={"accept": accept}
)
self.connection_id = result["connection_id"]
return self.connection_id
async def accept_invite(self, conn_id: str):
await self.admin_POST(f"/connections/{conn_id}/accept-invitation")
async def establish_inbound(self, conn_id: str, router_conn_id: str):
await self.admin_POST(
f"/connections/{conn_id}/establish-inbound/{router_conn_id}"
)
async def detect_connection(self):
if not self._connection_ready:
raise Exception("No connection to await")
await self._connection_ready
async def handle_connections(self, payload):
if payload["connection_id"] == self.connection_id:
if payload["state"] == "active" and not self._connection_ready.done():
self.log("Connected")
self._connection_ready.set_result(True)
class AliceAgent(BaseAgent):
def __init__(self, port: int, **kwargs):
super().__init__("Alice", port, seed=None, **kwargs)
self.credential_state = {}
self.credential_event = asyncio.Event()
self.extra_args = ["--auto-respond-credential-offer", "--auto-store-credential"]
async def handle_credentials(self, payload):
cred_id = payload["credential_exchange_id"]
self.credential_state[cred_id] = payload["state"]
self.credential_event.set()
def check_received_creds(self) -> (int, int):
self.credential_event.clear()
pending = 0
total = len(self.credential_state)
for result in self.credential_state.values():
if result != "stored":
pending += 1
return pending, total
async def update_creds(self):
await self.credential_event.wait()
class FaberAgent(BaseAgent):
def __init__(self, port: int, **kwargs):
super().__init__("Faber", port, **kwargs)
self.credential_state = {}
self.credential_event = asyncio.Event()
self.schema_id = None
self.credential_definition_id = None
async def handle_credentials(self, payload):
cred_id = payload["credential_exchange_id"]
self.credential_state[cred_id] = payload["state"]
self.credential_event.set()
def check_received_creds(self) -> (int, int):
self.credential_event.clear()
pending = 0
total = len(self.credential_state)
for result in self.credential_state.values():
if result != "stored":
pending += 1
return pending, total
async def update_creds(self):
await self.credential_event.wait()
async def publish_defs(self):
# 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}
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 = {
"name": "Alice Smith",
"date": "2018-05-28",
"degree": "Maths",
"age": "24",
}
await self.admin_POST(
"/credential_exchange/send",
{
"credential_values": cred_attrs,
"connection_id": self.connection_id,
"credential_definition_id": self.credential_definition_id,
},
)
class RoutingAgent(BaseAgent):
def __init__(self, port: int, **kwargs):
super().__init__("Router", port, **kwargs)
async def main(start_port: int, show_timing: bool = False, routing: bool = False):
genesis = await default_genesis_txns()
if not genesis:
print("Error retrieving ledger genesis transactions")
sys.exit(1)
alice = None
faber = None
alice_router = None
run_timer = log_timer("Total runtime:")
run_timer.start()
try:
alice = AliceAgent(start_port, genesis_data=genesis, timing=show_timing)
await alice.listen_webhooks(start_port + 2)
faber = FaberAgent(start_port + 3, genesis_data=genesis, timing=show_timing)
await faber.listen_webhooks(start_port + 5)
await faber.register_did()
if routing:
alice_router = RoutingAgent(
start_port + 6, genesis_data=genesis, timing=show_timing
)
await alice_router.listen_webhooks(start_port + 8)
await alice_router.register_did()
with log_timer("Startup duration:"):
if alice_router:
await alice_router.start_process()
await alice.start_process()
await faber.start_process()
with log_timer("Publish duration:"):
await faber.publish_defs()
with log_timer("Connect duration:"):
if routing:
router_invite = await alice_router.get_invite()
alice_router_conn_id = await alice.receive_invite(router_invite)
await asyncio.wait_for(alice.detect_connection(), 30)
invite = await faber.get_invite()
if routing:
conn_id = await alice.receive_invite(invite, accept="manual")
await alice.establish_inbound(conn_id, alice_router_conn_id)
await alice.accept_invite(conn_id)
await asyncio.wait_for(alice.detect_connection(), 30)
else:
await alice.receive_invite(invite)
await asyncio.wait_for(faber.detect_connection(), 30)
if show_timing:
await alice.reset_timing()
await faber.reset_timing()
if routing:
await alice_router.reset_timing()
issue_count = 300
batch_size = 100
semaphore = asyncio.Semaphore(10)
async def send():
await semaphore.acquire()
asyncio.ensure_future(faber.send_credential()).add_done_callback(
lambda fut: semaphore.release()
)
recv_timer = alice.log_timer(f"Received {issue_count} credentials in ")
recv_timer.start()
batch_timer = faber.log_timer(f"Started {batch_size} credential exchanges in ")
batch_timer.start()
async def check_received(agent, issue_count, pb):
reported = 0
iter_pb = iter(pb) if pb else None
while True:
pending, total = agent.check_received_creds()
if iter_pb and total > reported:
try:
while next(iter_pb) < total:
pass
except StopIteration:
iter_pb = None
reported = total
if total == issue_count and not pending:
break
await asyncio.wait_for(agent.update_creds(), 30)
with progress() as pb:
receive_task = None
try:
issue_pg = pb(range(issue_count), label="Issuing credentials")
receive_pg = pb(range(issue_count), label="Receiving credentials")
issue_task = asyncio.ensure_future(
check_received(faber, issue_count, issue_pg)
)
receive_task = asyncio.ensure_future(
check_received(alice, issue_count, receive_pg)
)
with faber.log_timer(
f"Done starting {issue_count} credential exchanges in "
):
for idx in range(0, issue_count):
await send()
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("Canceled")
recv_timer.stop()
avg = recv_timer.duration / issue_count
alice.log(f"Average time per credential: {avg:.2f}s ({1/avg:.2f}/s)")
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 routing:
timing = await alice_router.fetch_timing()
if timing:
for line in alice_router.format_timing(timing):
alice_router.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_router:
await alice_router.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(
"-p",
"--port",
type=int,
default=8030,
metavar=("<port>"),
help="Choose the starting port number to listen on",
)
parser.add_argument(
"--routing", action="store_true", help="Enable inbound routing demonstration"
)
args = parser.parse_args()
require_indy()
try:
asyncio.get_event_loop().run_until_complete(main(args.port, True, args.routing))
except KeyboardInterrupt:
os._exit(1)