|
9 | 9 | import errno |
10 | 10 | from enum import Enum |
11 | 11 | import http.client |
| 12 | +import datetime |
12 | 13 | import json |
13 | 14 | import logging |
14 | 15 | import os |
@@ -382,6 +383,45 @@ def assert_debug_log(self, expected_msgs, unexpected_msgs=None, timeout=2): |
382 | 383 | time.sleep(0.05) |
383 | 384 | self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(str(expected_msgs), print_log)) |
384 | 385 |
|
| 386 | + @contextlib.contextmanager |
| 387 | + def debug_log_delta(self, first_msg, second_msg, timeout=30): |
| 388 | + time_end = time.time() + timeout |
| 389 | + if self.chain == 'mainnet': |
| 390 | + chain_dir = '' |
| 391 | + else: |
| 392 | + chain_dir = self.chain |
| 393 | + |
| 394 | + debug_log = os.path.join(self.datadir, chain_dir, 'debug.log') |
| 395 | + with open(debug_log, encoding='utf-8') as dl: |
| 396 | + dl.seek(0, 2) |
| 397 | + prev_size = dl.tell() |
| 398 | + |
| 399 | + yield |
| 400 | + |
| 401 | + DEBUG_LOG_TIMESTAMP = "^\d{4}-\d{2}-\d{2}T(\d{2}:\d{2}:\d{2}.\d{6})Z" |
| 402 | + found_first = False |
| 403 | + |
| 404 | + while True: |
| 405 | + with open(debug_log, encoding='utf-8') as dl: |
| 406 | + dl.seek(prev_size) |
| 407 | + log = dl.read() |
| 408 | + print_log = " - " + "\n - ".join(log.splitlines()) |
| 409 | + if not found_first: |
| 410 | + m = re.search(DEBUG_LOG_TIMESTAMP + ".*" + first_msg + ".*$", log, re.MULTILINE) |
| 411 | + if m: |
| 412 | + time_first = datetime.datetime.strptime(m.groups()[0], "%H:%M:%S.%f") |
| 413 | + found_first = True |
| 414 | + if found_first: |
| 415 | + m2 = re.search(DEBUG_LOG_TIMESTAMP + ".*" + second_msg + ".*$", log, re.MULTILINE) |
| 416 | + if m2: |
| 417 | + time_second = datetime.datetime.strptime(m2.groups()[0], "%H:%M:%S.%f") |
| 418 | + print("{} to {}: {}ms".format(first_msg, second_msg, (time_second - time_first).microseconds / 1000)) |
| 419 | + return |
| 420 | + if time.time() >= time_end: |
| 421 | + break |
| 422 | + time.sleep(0.05) |
| 423 | + self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(first_msg, print_log)) |
| 424 | + |
385 | 425 | @contextlib.contextmanager |
386 | 426 | def profile_with_perf(self, profile_name): |
387 | 427 | """ |
|
0 commit comments