Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions benchmarks/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"""Common code for benchmarks."""

from os import environ
import resource
import tracemalloc

import numpy as np


def disable_repeat_between_setup(benchmark_object):
Expand Down Expand Up @@ -61,27 +63,34 @@ class TrackAddedMemoryAllocation:
AVD's detection threshold and be treated as 'signal'. Results
smaller than this value will therefore be returned as equal to this
value, ensuring fractionally small noise / no noise at all.
Defaults to 1.0

"""
RESULT_ROUND_DP : int
Number of decimal places of rounding on result values (in Mb).
Defaults to 1

RESULT_MINIMUM_MB = 5.0
"""

@staticmethod
def process_resident_memory_mb():
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024.0
RESULT_MINIMUM_MB = 1.0
RESULT_ROUND_DP = 1

def __enter__(self):
self.mb_before = self.process_resident_memory_mb()
tracemalloc.start()
return self

def __exit__(self, *_):
self.mb_after = self.process_resident_memory_mb()
_, peak_mem_bytes = tracemalloc.get_traced_memory()
tracemalloc.stop()
# Save peak-memory allocation, scaled from bytes to Mb.
self._peak_mb = peak_mem_bytes * (2.0**-20)

def addedmem_mb(self):
"""Return measured memory growth, in Mb."""
result = self.mb_after - self.mb_before
result = self._peak_mb
# Small results are too vulnerable to noise being interpreted as signal.
result = max(self.RESULT_MINIMUM_MB, result)
# Rounding makes results easier to read.
result = np.round(result, self.RESULT_ROUND_DP)
return result

@staticmethod
Expand Down