Skip to content

Commit

Permalink
Merge 2a69918 into c956403
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenworsley committed May 20, 2024
2 parents c956403 + 2a69918 commit 3bf0041
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
27 changes: 27 additions & 0 deletions benchmarks/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ def _wrapper(*args, **kwargs):
decorated_func.unit = "Mb"
return _wrapper

@staticmethod
def decorator_repeating(repeats=3):
"""Benchmark to track growth in resident memory during execution.
Tracks memory for repeated calls of decorated function.
Intended for use on ASV ``track_`` benchmarks. Applies the
:class:`TrackAddedMemoryAllocation` context manager to the benchmark
code, sets the benchmark ``unit`` attribute to ``Mb``.
"""

def decorator(decorated_func):
def _wrapper(*args, **kwargs):
assert decorated_func.__name__[:6] == "track_"
# Run the decorated benchmark within the added memory context
# manager.
with TrackAddedMemoryAllocation() as mb:
for _ in range(repeats):
decorated_func(*args, **kwargs)
return mb.addedmem_mb()

decorated_func.unit = "Mb"
return _wrapper

return decorator


def on_demand_benchmark(benchmark_object):
"""Disable these benchmark(s) unless ON_DEMAND_BENCHARKS env var is set.
Expand Down
9 changes: 9 additions & 0 deletions benchmarks/benchmarks/merge_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from iris.cube import CubeList

from . import TrackAddedMemoryAllocation
from .generate_data.stock import realistic_4d_w_everything


Expand All @@ -33,6 +34,10 @@ def setup(self):
def time_merge(self):
_ = self.cube_list.merge_cube()

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_merge(self):
_ = self.cube_list.merge_cube()


class Concatenate:
# TODO: Improve coverage.
Expand All @@ -50,3 +55,7 @@ def setup(self):

def time_concatenate(self):
_ = self.cube_list.concatenate_cube()

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_merge(self):
_ = self.cube_list.concatenate_cube()
23 changes: 23 additions & 0 deletions benchmarks/benchmarks/regridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from iris.analysis import AreaWeighted, PointInCell
from iris.coords import AuxCoord

from . import TrackAddedMemoryAllocation


class HorizontalChunkedRegridding:
def setup(self) -> None:
Expand Down Expand Up @@ -51,6 +53,20 @@ def time_regrid_area_w_new_grid(self) -> None:
# Realise data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_area_w(self) -> None:
# Regrid the chunked cube
out = self.cube.regrid(self.template_cube, self.scheme_area_w)
# Realise data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_area_w_new_grid(self) -> None:
# Regrid the chunked cube
out = self.chunked_cube.regrid(self.template_cube, self.scheme_area_w)
# Realise data
out.data


class CurvilinearRegridding:
def setup(self) -> None:
Expand Down Expand Up @@ -93,3 +109,10 @@ def time_regrid_pic(self) -> None:
out = self.cube.regrid(self.template_cube, self.scheme_pic)
# Realise the data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_pic(self) -> None:
# Regrid the cube onto the template.
out = self.cube.regrid(self.template_cube, self.scheme_pic)
# Realise the data
out.data
14 changes: 14 additions & 0 deletions benchmarks/benchmarks/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from iris.analysis.stats import pearsonr
import iris.tests

from . import TrackAddedMemoryAllocation


class PearsonR:
def setup(self):
Expand All @@ -30,9 +32,21 @@ def setup(self):
def time_real(self):
pearsonr(self.cube_a, self.cube_b, weights=self.weights)

@TrackAddedMemoryAllocation.decorator_repeating()
def track_real(self):
pearsonr(self.cube_a, self.cube_b, weights=self.weights)

def time_lazy(self):
for cube in self.cube_a, self.cube_b:
cube.data = cube.lazy_data()

result = pearsonr(self.cube_a, self.cube_b, weights=self.weights)
result.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_lazy(self):
for cube in self.cube_a, self.cube_b:
cube.data = cube.lazy_data()

result = pearsonr(self.cube_a, self.cube_b, weights=self.weights)
result.data
16 changes: 16 additions & 0 deletions benchmarks/benchmarks/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import iris
from iris.analysis.trajectory import interpolate

from . import TrackAddedMemoryAllocation


class TrajectoryInterpolation:
def setup(self) -> None:
Expand All @@ -33,8 +35,22 @@ def time_trajectory_linear(self) -> None:
# Realise the data
out_cube.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_trajectory_linear(self) -> None:
# Regrid the cube onto the template.
out_cube = interpolate(self.cube, self.sample_points, method="linear")
# Realise the data
out_cube.data

def time_trajectory_nearest(self) -> None:
# Regrid the cube onto the template.
out_cube = interpolate(self.cube, self.sample_points, method="nearest")
# Realise the data
out_cube.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_trajectory_nearest(self) -> None:
# Regrid the cube onto the template.
out_cube = interpolate(self.cube, self.sample_points, method="nearest")
# Realise the data
out_cube.data

0 comments on commit 3bf0041

Please sign in to comment.