diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index 73f5cd6399..1eccf6478f 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -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): @@ -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