-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_bullshark.py
479 lines (401 loc) · 17 KB
/
extract_bullshark.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
# Copyright(C) Facebook, Inc. and its affiliates.
from os.path import join
from datetime import datetime
from glob import glob
import json
from multiprocessing import Pool
from os.path import join
from re import findall, search
from statistics import mean
import sys
class BenchError(Exception):
def __init__(self, message, error):
assert isinstance(error, Exception)
self.message = message
self.cause = error
super().__init__(message)
class PathMaker:
@staticmethod
def binary_path():
return join('..', 'target', 'release')
@staticmethod
def node_crate_path():
return join('..', 'node')
@staticmethod
def committee_file():
return '.committee.json'
@staticmethod
def parameters_file():
return '.parameters.json'
@staticmethod
def client_ips_file():
return '.ips.txt'
@staticmethod
def key_file(i):
assert isinstance(i, int) and i >= 0
return f'.node-{i}.json'
@staticmethod
def db_path(i, j=None):
assert isinstance(i, int) and i >= 0
assert (isinstance(j, int) and i >= 0) or j is None
worker_id = f'-{j}' if j is not None else ''
return f'.db-{i}{worker_id}'
@staticmethod
def logs_path():
return 'logs'
@staticmethod
def primary_log_file(i):
assert isinstance(i, int) and i >= 0
return join(PathMaker.logs_path(), f'primary-{i}.log')
@staticmethod
def worker_log_file(i, j):
assert isinstance(i, int) and i >= 0
assert isinstance(j, int) and i >= 0
return join(PathMaker.logs_path(), f'worker-{i}-{j}.log')
@staticmethod
def client_log_file(i, j):
assert isinstance(i, int) and i >= 0
assert isinstance(j, int) and i >= 0
return join(PathMaker.logs_path(), f'client-{i}-{j}.log')
@staticmethod
def results_path():
return 'results'
@staticmethod
def result_file(faults, nodes, workers, collocate, rate, tx_size):
return join(
PathMaker.results_path(),
f'bench-{faults}-{nodes}-{workers}-{collocate}-{rate}-{tx_size}.txt'
)
@staticmethod
def plots_path():
return 'plots'
@staticmethod
def agg_file(type, faults, nodes, workers, collocate, rate, tx_size, max_latency=None):
if max_latency is None:
name = f'{type}-bench-{faults}-{nodes}-{workers}-{collocate}-{rate}-{tx_size}.txt'
else:
name = f'{type}-{max_latency}-bench-{faults}-{nodes}-{workers}-{collocate}-{rate}-{tx_size}.txt'
return join(PathMaker.plots_path(), name)
@staticmethod
def plot_file(name, ext):
return join(PathMaker.plots_path(), f'{name}.{ext}')
class Color:
HEADER = '\033[95m'
OK_BLUE = '\033[94m'
OK_GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Print:
@staticmethod
def heading(message):
assert isinstance(message, str)
print(f'{Color.OK_GREEN}{message}{Color.END}')
@staticmethod
def info(message):
assert isinstance(message, str)
print(message)
@staticmethod
def warn(message):
assert isinstance(message, str)
print(f'{Color.BOLD}{Color.WARNING}WARN{Color.END}: {message}')
@staticmethod
def error(e):
assert isinstance(e, BenchError)
print(f'\n{Color.BOLD}{Color.FAIL}ERROR{Color.END}: {e}\n')
causes, current_cause = [], e.cause
while isinstance(current_cause, BenchError):
causes += [f' {len(causes)}: {e.cause}\n']
current_cause = current_cause.cause
causes += [f' {len(causes)}: {type(current_cause)}\n']
causes += [f' {len(causes)}: {current_cause}\n']
print(f'Caused by: \n{"".join(causes)}\n')
def progress_bar(iterable, prefix='', suffix='', decimals=1, length=30, fill='█', print_end='\r'):
total = len(iterable)
def printProgressBar(iteration):
formatter = '{0:.'+str(decimals)+'f}'
percent = formatter.format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
printProgressBar(0)
for i, item in enumerate(iterable):
yield item
printProgressBar(i + 1)
print()
class ParseError(Exception):
pass
class LogParser:
def __init__(self, clients, primaries, workers, faults=0):
inputs = [clients, primaries, workers]
assert all(isinstance(x, list) for x in inputs)
assert all(isinstance(x, str) for y in inputs for x in y)
assert all(x for x in inputs)
self.faults = faults
if isinstance(faults, int):
self.committee_size = len(primaries) + int(faults)
self.workers = len(workers) // len(primaries)
else:
self.committee_size = '?'
self.workers = '?'
# Parse the clients logs.
try:
with Pool() as p:
results = p.map(self._parse_clients, clients)
except (ValueError, IndexError, AttributeError) as e:
raise ParseError(f'Failed to parse clients\' logs: {e}')
self.size, self.rate, self.start, misses, self.sent_samples, \
self.true_commits = zip(*results)
self.misses = sum(misses)
# Parse the primaries logs.
try:
with Pool() as p:
results = p.map(self._parse_primaries, primaries)
except (ValueError, IndexError, AttributeError) as e:
raise ParseError(f'Failed to parse nodes\' logs: {e}')
proposals, commits, self.configs, primary_ips = zip(*results)
self.proposals = self._merge_results([x.items() for x in proposals])
self.commits = self._merge_results([x.items() for x in commits])
# Parse the workers logs.
try:
with Pool() as p:
results = p.map(self._parse_workers, workers)
except (ValueError, IndexError, AttributeError) as e:
raise ParseError(f'Failed to parse workers\' logs: {e}')
sizes, self.received_samples, workers_ips = zip(*results)
self.sizes = {
k: v for x in sizes for k, v in x.items() if k in self.commits
}
# Determine whether the primary and the workers are collocated.
self.collocate = set(primary_ips) == set(workers_ips)
# Check whether clients missed their target rate.
if self.misses != 0:
Print.warn(
f'Clients missed their target rate {self.misses:,} time(s)'
)
def _merge_results(self, input):
# Keep the earliest timestamp.
merged = {}
for x in input:
for k, v in x:
if not k in merged or merged[k] > v:
merged[k] = v
return merged
def _parse_clients(self, log):
if search(r'Error', log) is not None:
raise ParseError('Client(s) panicked')
size = int(search(r'Transactions size: (\d+)', log).group(1))
rate = int(search(r'Transactions rate: (\d+)', log).group(1))
tmp = search(r'\[(.*Z) .* Start ', log).group(1)
start = self._to_posix(tmp)
misses = len(findall(r'rate too high', log))
tmp = findall(r'\[(.*Z) .* sample transaction (\d+)', log)
samples = {int(s): self._to_posix(t) for t, s in tmp}
tmp = findall(r'\[(.*Z) .* Committed -> ([^ ]+=)', log)
tmp = [(d, self._to_posix(t)) for t, d in tmp]
true_commits = self._merge_results([tmp])
return size, rate, start, misses, samples, true_commits
def _parse_primaries(self, log):
if search(r'(?:panicked|Error)', log) is not None:
raise ParseError('Primary(s) panicked')
tmp = findall(r'\[(.*Z) .* Created B\d+\([^ ]+\) -> ([^ ]+=)', log)
tmp = [(d, self._to_posix(t)) for t, d in tmp]
proposals = self._merge_results([tmp])
tmp = findall(r'\[(.*Z) .* Committed B\d+\([^ ]+\) -> ([^ ]+=)', log)
tmp = [(d, self._to_posix(t)) for t, d in tmp]
commits = self._merge_results([tmp])
configs = {
'header_size': int(
search(r'Header size .* (\d+)', log).group(1)
),
'max_header_delay': int(
search(r'Max header delay .* (\d+)', log).group(1)
),
'gc_depth': int(
search(r'Garbage collection depth .* (\d+)', log).group(1)
),
'sync_retry_delay': int(
search(r'Sync retry delay .* (\d+)', log).group(1)
),
'sync_retry_nodes': int(
search(r'Sync retry nodes .* (\d+)', log).group(1)
),
'batch_size': int(
search(r'Batch size .* (\d+)', log).group(1)
),
'max_batch_delay': int(
search(r'Max batch delay .* (\d+)', log).group(1)
),
}
ip = search(r'booted on (\d+.\d+.\d+.\d+)', log).group(1)
return proposals, commits, configs, ip
def _parse_workers(self, log):
if search(r'(?:panic|Error)', log) is not None:
raise ParseError('Worker(s) panicked')
tmp = findall(r'Batch ([^ ]+) contains (\d+) B', log)
sizes = {d: int(s) for d, s in tmp}
tmp = findall(r'Batch ([^ ]+) contains sample tx (\d+)', log)
samples = {int(s): d for d, s in tmp}
ip = search(r'booted on (\d+.\d+.\d+.\d+)', log).group(1)
return sizes, samples, ip
def _to_posix(self, string):
x = datetime.fromisoformat(string.replace('Z', '+00:00'))
return datetime.timestamp(x)
def _consensus_throughput(self):
if not self.commits:
return 0, 0, 0
start, end = min(self.proposals.values()), max(self.commits.values())
duration = end - start
bytes = sum(self.sizes.values())
print(bytes)
bps = bytes / duration
tps = bps / self.size[0]
return tps, bps, duration
def _consensus_latency(self):
latency = [c - self.proposals[d] for d, c in self.commits.items() if d in self.proposals]
return mean(latency) if latency else 0
def _end_to_end_throughput(self):
if not self.commits:
return 0, 0, 0
start, end = min(self.start), max(self.commits.values())
duration = end - start
bytes = sum(self.sizes.values())
bps = bytes / duration
tps = bps / self.size[0]
return tps, bps, duration
def _end_to_end_latency(self):
latency = []
for received in self.received_samples:
for tx_id, batch_id in received.items():
if batch_id in self.commits:
for sent in self.sent_samples:
if tx_id in sent:
start = sent[tx_id]
end = self.commits[batch_id]
latency += [end-start]
return latency if latency else [-1]
def _true_end_to_end_latency(self):
latency = []
for received in self.received_samples:
for tx_id, batch_id in received.items():
for (sent, commits) in zip(self.sent_samples, self.true_commits):
if batch_id in commits and tx_id in sent:
start = sent[tx_id]
end = commits[batch_id]
latency += [end-start]
return latency if latency else [-1]
def result(self):
header_size = self.configs[0]['header_size']
max_header_delay = self.configs[0]['max_header_delay']
gc_depth = self.configs[0]['gc_depth']
sync_retry_delay = self.configs[0]['sync_retry_delay']
sync_retry_nodes = self.configs[0]['sync_retry_nodes']
batch_size = self.configs[0]['batch_size']
max_batch_delay = self.configs[0]['max_batch_delay']
consensus_latency = self._consensus_latency() * 1_000
consensus_tps, consensus_bps, _ = self._consensus_throughput()
end_to_end_tps, end_to_end_bps, duration = self._end_to_end_throughput()
end_to_end_latency_values = self._end_to_end_latency() # all values, can compute percentiles from this
true_end_to_end_latency_values = self._true_end_to_end_latency() # all values, can compute percentiles from this
end_to_end_latency = mean(end_to_end_latency_values) * 1_000
true_end_to_end_latency = mean(true_end_to_end_latency_values) * 1_000
estimated_tps = int(float(64)/float(len(self.received_samples)) * float(end_to_end_tps))
# PLR: export to json to make it easier to import and to add latency distribution
json_data = {}
json_data['throughput-avg'] = round(end_to_end_tps)
json_data['latency-avg'] = round(true_end_to_end_latency)
json_data['latency'] = true_end_to_end_latency_values
prefix = sys.argv[1].rstrip('/') # remove righ-most slashes
filename = '{}.easier-log.json'.format(prefix)
with open(filename, 'w') as f:
json.dump(json_data, f)
return (
'\n'
f'Added json file: {filename}\n'
'\n'
'-----------------------------------------\n'
' SUMMARY:\n'
'-----------------------------------------\n'
' + CONFIG:\n'
f' Faults: {self.faults} node(s)\n'
f' Committee size: {self.committee_size} node(s)\n'
f' Worker(s) per node: {self.workers} worker(s)\n'
f' Collocate primary and workers: {self.collocate}\n'
f' Input rate: {sum(self.rate):,} tx/s\n'
f' Transaction size: {self.size[0]:,} B\n'
f' Execution time: {round(duration):,} s\n'
'\n'
f' Header size: {header_size:,} B\n'
f' Max header delay: {max_header_delay:,} ms\n'
f' GC depth: {gc_depth:,} round(s)\n'
f' Sync retry delay: {sync_retry_delay:,} ms\n'
f' Sync retry nodes: {sync_retry_nodes:,} node(s)\n'
f' batch size: {batch_size:,} B\n'
f' Max batch delay: {max_batch_delay:,} ms\n'
'\n'
' + RESULTS:\n'
f' Consensus TPS: {round(consensus_tps):,} tx/s\n'
f' Consensus BPS: {round(consensus_bps):,} B/s\n'
f' Consensus latency: {round(consensus_latency):,} ms\n'
'\n'
f' End-to-end TPS: {round(end_to_end_tps):,} tx/s\n'
f' Estimated TPS: {round(estimated_tps):,} tx/s\n'
f' End-to-end BPS: {round(end_to_end_bps):,} B/s\n'
f' End-to-end latency: {round(end_to_end_latency):,} ms\n'
f' True End-to-end latency: {round(true_end_to_end_latency):,} ms\n'
'-----------------------------------------\n'
)
def print(self, filename):
assert isinstance(filename, str)
with open(filename, 'a') as f:
f.write(self.result())
@classmethod
def process(cls, directory, faults=0):
assert isinstance(directory, str)
clients = []
for filename in sorted(glob(join(directory, 'client-*.log'))):
with open(filename, 'r') as f:
clients += [f.read()]
primaries = []
for filename in sorted(glob(join(directory, 'primary-*.log'))):
with open(filename, 'r') as f:
primaries += [f.read()]
workers = []
for filename in sorted(glob(join(directory, 'worker-*.log'))):
with open(filename, 'r') as f:
workers += [f.read()]
return cls(clients, primaries, workers, faults=faults)
@classmethod
def chop_process(cls, directory, faults=0):
assert isinstance(directory, str)
clients = []
for filename in sorted(glob(join(directory, 'honest_client_*.err'), recursive=True)):
with open(filename, 'r') as f:
clients += [f.read()]
primaries = []
for filename in sorted(glob(join(directory, 'server_*.err'), recursive=True)):
with open(filename, 'r') as f:
primaries += [f.read()]
workers = []
for filename in sorted(glob(join(directory, 'server_*.err'), recursive=True)):
with open(filename, 'r') as f:
workers += [f.read()]
# PLR: add both server and worker logs to consider all throughput
for filename in sorted(glob(join(directory, 'worker_*.err'), recursive=True)):
with open(filename, 'r') as f:
workers += [f.read()]
return cls(clients, primaries, workers, faults=faults)
'''
Print a summary of the logs
Example Usage: python3 extract_bullshark.py comma-64-baselines/bullshark_64-500000_64-200000_16-1_88_1_2022-12-09-17-40-10_3_eu-west-2/
'''
try:
n = len(sys.argv)
if n < 2:
print("Must provide the logs directory path")
else:
print(LogParser.chop_process(sys.argv[1], faults='?').result())
except ParseError as e:
pass