From 074d3a10e038cd62691c71722b7e9a269d33591c Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Thu, 9 May 2024 14:12:15 +0100 Subject: [PATCH 1/2] Use tracemalloc for memory measurements. --- benchmarks/benchmarks/__init__.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index 14b28b3070..0f3bc38fa9 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 ARTIFICIAL_DIM_SIZE = int(10e3) # For all artificial cubes, coords etc. @@ -63,27 +65,32 @@ 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 = tracemalloc.get_traced_memory() + tracemalloc.stop() + self._peak_mb = peak_mem * 1.0 / 1024**2 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) + result = np.round(result, self.RESULT_ROUND_DP) return result @staticmethod From d9232df1aa0b0f8c9b5f834a41406da51d9799af Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Wed, 15 May 2024 15:27:13 +0100 Subject: [PATCH 2/2] Review changes. --- benchmarks/benchmarks/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index 0f3bc38fa9..51f1cfa517 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -81,15 +81,17 @@ def __enter__(self): return self def __exit__(self, *_): - _, peak_mem = tracemalloc.get_traced_memory() + _, peak_mem_bytes = tracemalloc.get_traced_memory() tracemalloc.stop() - self._peak_mb = peak_mem * 1.0 / 1024**2 + # 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._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