qa: clean up assert_memory_usage_stable utility

This commit is contained in:
James O'Beirne 2018-11-26 15:47:30 -05:00
parent 0cf1632f03
commit 5a1f57646b
2 changed files with 11 additions and 7 deletions

View file

@ -66,7 +66,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
msg_at_size = msg_unrecognized("b" * valid_data_limit) msg_at_size = msg_unrecognized("b" * valid_data_limit)
assert len(msg_at_size.serialize()) == msg_limit assert len(msg_at_size.serialize()) == msg_limit
with node.assert_memory_usage_stable(perc_increase_allowed=0.5): with node.assert_memory_usage_stable(increase_allowed=0.5):
self.log.info( self.log.info(
"Sending a bunch of large, junk messages to test " "Sending a bunch of large, junk messages to test "
"memory exhaustion. May take a bit...") "memory exhaustion. May take a bit...")

View file

@ -115,7 +115,7 @@ class TestNode():
] ]
return PRIV_KEYS[self.index] return PRIV_KEYS[self.index]
def get_mem_rss(self): def get_mem_rss_kilobytes(self):
"""Get the memory usage (RSS) per `ps`. """Get the memory usage (RSS) per `ps`.
Returns None if `ps` is unavailable. Returns None if `ps` is unavailable.
@ -291,15 +291,19 @@ class TestNode():
self._raise_assertion_error('Expected message "{}" does not partially match log:\n\n{}\n\n'.format(expected_msg, print_log)) self._raise_assertion_error('Expected message "{}" does not partially match log:\n\n{}\n\n'.format(expected_msg, print_log))
@contextlib.contextmanager @contextlib.contextmanager
def assert_memory_usage_stable(self, perc_increase_allowed=0.03): def assert_memory_usage_stable(self, *, increase_allowed=0.03):
"""Context manager that allows the user to assert that a node's memory usage (RSS) """Context manager that allows the user to assert that a node's memory usage (RSS)
hasn't increased beyond some threshold percentage. hasn't increased beyond some threshold percentage.
Args:
increase_allowed (float): the fractional increase in memory allowed until failure;
e.g. `0.12` for up to 12% increase allowed.
""" """
before_memory_usage = self.get_mem_rss() before_memory_usage = self.get_mem_rss_kilobytes()
yield yield
after_memory_usage = self.get_mem_rss() after_memory_usage = self.get_mem_rss_kilobytes()
if not (before_memory_usage and after_memory_usage): if not (before_memory_usage and after_memory_usage):
self.log.warning("Unable to detect memory usage (RSS) - skipping memory check.") self.log.warning("Unable to detect memory usage (RSS) - skipping memory check.")
@ -307,10 +311,10 @@ class TestNode():
perc_increase_memory_usage = (after_memory_usage / before_memory_usage) - 1 perc_increase_memory_usage = (after_memory_usage / before_memory_usage) - 1
if perc_increase_memory_usage > perc_increase_allowed: if perc_increase_memory_usage > increase_allowed:
self._raise_assertion_error( self._raise_assertion_error(
"Memory usage increased over threshold of {:.3f}% from {} to {} ({:.3f}%)".format( "Memory usage increased over threshold of {:.3f}% from {} to {} ({:.3f}%)".format(
perc_increase_allowed * 100, before_memory_usage, after_memory_usage, increase_allowed * 100, before_memory_usage, after_memory_usage,
perc_increase_memory_usage * 100)) perc_increase_memory_usage * 100))
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, match=ErrorMatch.FULL_TEXT, *args, **kwargs): def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, match=ErrorMatch.FULL_TEXT, *args, **kwargs):