8000 Use tracemalloc for memory measurements. by pp-mo · Pull Request #5948 · SciTools/iris · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use tracemalloc for memory measurements. #5948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2024
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
Loading
0