-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathpiccolo_driver.py
More file actions
311 lines (259 loc) · 10.2 KB
/
Copy pathpiccolo_driver.py
File metadata and controls
311 lines (259 loc) · 10.2 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
import argparse
import os
import infra.e2e_args
import infra.remote_client
import infra.jwt_issuer
from infra.perf import PERF_COLUMNS
from random import seed
import getpass
from loguru import logger as LOG
import time
import hashlib
import json
from piccolo import generator
from piccolo import analyzer
import infra.bencher
import infra.proc
def get_command_args(args, network, get_command):
client_ident = network.users[0]
command_args = [
"--cert",
client_ident.cert_path,
"--key",
client_ident.key_path,
"--cacert",
network.cert_path,
]
return get_command(*command_args)
def minimum_number_of_local_nodes(args):
if args.send_tx_to == "backups":
return 2
return 1
def filter_nodes(primary, backups, filter_type):
if filter_type == "primary":
return [primary]
elif filter_type == "backups":
assert backups, "--send-tx-to backups but no backup was found"
return backups
else:
return [primary] + backups
def my_configure_remote_client(args, client_id, client_host, node, command_args):
client_host = infra.net.expand_localhost()
try:
remote_client = infra.remote_client.CCFRemoteClient(
f"client_{client_id}",
client_host,
args.client,
node.get_public_rpc_host(),
node.get_public_rpc_port(),
args.workspace,
args.label,
args.config,
command_args,
piccolo_run=True,
)
remote_client.setup()
return remote_client
except Exception:
LOG.exception("Failed to start client {}".format(client_host))
raise
def run(get_command, args):
if args.fixed_seed:
seed(getpass.getuser())
hosts = args.nodes
if not hosts:
hosts = infra.e2e_args.nodes(args, minimum_number_of_local_nodes(args))
args.initial_user_count = 3
args.sig_ms_interval = 100
args.ledger_chunk_bytes = "5MB" # Set to node default value
LOG.info("Starting nodes on {}".format(hosts))
with infra.network.network(
hosts, args.binary_dir, args.debug_nodes, pdb=args.pdb
) as network:
network.start_and_open(args)
primary, backups = network.find_nodes()
command_args = get_command_args(args, network, get_command)
additional_headers = {}
if args.use_jwt:
jwt_issuer = infra.jwt_issuer.JwtIssuer("https://example.issuer")
jwt_issuer.register(network)
jwt = jwt_issuer.issue_jwt()
additional_headers["Authorization"] = f"Bearer {jwt}"
LOG.info(f"Generating {args.repetitions} parquet requests")
msgs = generator.Messages()
for i in range(args.repetitions):
body = {
"id": i % 100,
"msg": f"Unique message: {hashlib.sha256(str(i).encode()).hexdigest()}",
}
msgs.append(
"/app/log/private",
"POST",
additional_headers=additional_headers,
body=json.dumps(body),
)
filename_prefix = "piccolo_driver"
path_to_requests_file = os.path.join(
network.common_dir, f"{filename_prefix}_requests.parquet"
)
LOG.info(f"Writing generated requests to {path_to_requests_file}")
msgs.to_parquet_file(path_to_requests_file)
path_to_send_file = os.path.join(
network.common_dir, f"{filename_prefix}_send.parquet"
)
path_to_response_file = os.path.join(
network.common_dir, f"{filename_prefix}_response.parquet"
)
# Add filepaths in commands
command_args += ["--send-filepath", path_to_send_file]
command_args += ["--response-filepath", path_to_response_file]
command_args += ["--generator-filepath", path_to_requests_file]
nodes_to_send_to = filter_nodes(primary, backups, args.send_tx_to)
clients = []
client_hosts = []
if args.one_client_per_backup:
assert backups, "--one-client-per-backup was set but no backup was found"
client_hosts = ["localhost"] * len(backups)
else:
if args.client_nodes:
client_hosts.extend(args.client_nodes)
if args.num_localhost_clients:
client_hosts.extend(["localhost"] * int(args.num_localhost_clients))
if not client_hosts:
client_hosts = ["localhost"]
for client_id, client_host in enumerate(client_hosts):
node = nodes_to_send_to[client_id % len(nodes_to_send_to)]
remote_client = my_configure_remote_client(
args, client_id, client_host, node, command_args
)
clients.append(remote_client)
if args.network_only:
for remote_client in clients:
LOG.info(f"Client can be run with: {remote_client.remote.get_cmd()}")
while True:
time.sleep(60)
else:
for remote_client in clients:
remote_client.start()
hard_stop_timeout = 90
format_width = len(str(hard_stop_timeout)) + 3
try:
start_time = time.time()
while True:
stop_waiting = True
for i, remote_client in enumerate(clients):
done = remote_client.check_done()
# all the clients need to be done
LOG.info(
f"Client {i} has {'completed' if done else 'not completed'} running ({time.time() - start_time:>{format_width}.2f}s / {hard_stop_timeout}s)"
)
stop_waiting = stop_waiting and done
if stop_waiting:
break
if time.time() > start_time + hard_stop_timeout:
raise TimeoutError(
f"Client still running after {hard_stop_timeout}s"
)
time.sleep(5)
perf_label = args.perf_label
for remote_client in clients:
analysis = analyzer.Analyze()
LOG.info(
f"Analyzing results from {path_to_send_file} and {path_to_response_file}"
)
df_sends = analyzer.get_df_from_parquet_file(path_to_send_file)
df_responses = analyzer.get_df_from_parquet_file(
path_to_response_file
)
time_spent = analysis.total_time_in_sec(df_sends, df_responses)
perf_result = round(len(df_sends.index) / time_spent, 1)
LOG.success(f"{args.label}/{remote_client.name}: {perf_result}")
# Throughput from only one client, preserved for legacy reason
# see basicperf.py for a better, cross-client approach.
bf = infra.bencher.Bencher()
bf.set(
perf_label,
infra.bencher.Throughput(perf_result),
)
primary, _ = network.find_primary()
mem = infra.proc.get_proc_memory_stats(primary.remote.remote.proc.pid)
if mem is not None:
bf = infra.bencher.Bencher()
bf.set_memory(perf_label, mem)
for remote_client in clients:
remote_client.stop()
except Exception as e:
LOG.error(f"Stopping clients due to exception: {e}")
for remote_client in clients:
remote_client.stop()
raise
def cli_args(add=lambda x: None, accept_unknown=False):
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--client", help="Client binary", required=True)
parser.add_argument(
"-n",
"--nodes",
help="List of hostnames[,pub_hostnames:ports]. If empty, spawn minimum working number of local nodes (minimum depends on consensus and other args)",
action="append",
)
client_args_group = parser.add_mutually_exclusive_group()
client_args_group.add_argument(
"-cn",
"--client-nodes",
help="List of hostnames for spawning client(s). If empty, one client is spawned locally",
action="append",
)
client_args_group.add_argument(
"--one-client-per-backup",
help="If set, allocates one (local) client per backup",
action="store_true",
)
parser.add_argument(
"-nlc",
"--num-localhost-clients",
help="The number of localhost clients. \
This argument is cumulative with the client-nodes and one-client-per-backup and arguments",
)
parser.add_argument(
"--send-tx-to",
choices=["primary", "backups", "all"],
default="all",
help="Send client requests only to primary, only to backups, or to all nodes",
)
parser.add_argument(
"--metrics-file",
default="metrics.json",
help="Path to json file where the transaction rate metrics will be saved to",
)
parser.add_argument(
"-f",
"--fixed-seed",
help="Set a fixed seed for port and IP generation.",
action="store_true",
)
parser.add_argument(
"--use-jwt",
help="Use JWT with a temporary issuer as authentication method.",
action="store_true",
)
parser.add_argument(
"--repetitions",
help="Number of requests to send",
type=int,
default=100,
)
parser.add_argument("--config", help="Path to config for client binary", default="")
return infra.e2e_args.cli_args(
add=add, parser=parser, accept_unknown=accept_unknown
)
def generic_run(*args, **kwargs):
infra.path.mk_new("perf_summary.csv", PERF_COLUMNS)
run(*args, **kwargs)
if __name__ == "__main__":
args, unknown_args = cli_args(accept_unknown=True)
unknown_args = [term for arg in unknown_args for term in arg.split(" ")]
def get_command(*args):
return [*args] + unknown_args
run(get_command, args)