Skip to content

Commit 22c98e8

Browse files
stephenworsleypre-commit-ci[bot]pp-mo
authored
Add memory benchmarks (#5960)
* add memory benchmarks * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix memory benchmarks * generalise benchmark repeats * fix indent --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Patrick Peglar <[email protected]>
1 parent 0909918 commit 22c98e8

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

benchmarks/benchmarks/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ def _wrapper(*args, **kwargs):
114114
decorated_func.unit = "Mb"
115115
return _wrapper
116116

117+
@staticmethod
118+
def decorator_repeating(repeats=3):
119+
"""Benchmark to track growth in resident memory during execution.
120+
121+
Tracks memory for repeated calls of decorated function.
122+
123+
Intended for use on ASV ``track_`` benchmarks. Applies the
124+
:class:`TrackAddedMemoryAllocation` context manager to the benchmark
125+
code, sets the benchmark ``unit`` attribute to ``Mb``.
126+
127+
"""
128+
129+
def decorator(decorated_func):
130+
def _wrapper(*args, **kwargs):
131+
assert decorated_func.__name__[:6] == "track_"
132+
# Run the decorated benchmark within the added memory context
133+
# manager.
134+
with TrackAddedMemoryAllocation() as mb:
135+
for _ in range(repeats):
136+
decorated_func(*args, **kwargs)
137+
return mb.addedmem_mb()
138+
139+
decorated_func.unit = "Mb"
140+
return _wrapper
141+
142+
return decorator
143+
117144

118145
def on_demand_benchmark(benchmark_object):
119146
"""Disable these benchmark(s) unless ON_DEMAND_BENCHARKS env var is set.

benchmarks/benchmarks/merge_concat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from iris.cube import CubeList
1010

11+
from . import TrackAddedMemoryAllocation
1112
from .generate_data.stock import realistic_4d_w_everything
1213

1314

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

37+
@TrackAddedMemoryAllocation.decorator_repeating()
38+
def track_mem_merge(self):
39+
_ = self.cube_list.merge_cube()
40+
3641

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

5156
def time_concatenate(self):
5257
_ = self.cube_list.concatenate_cube()
58+
59+
@TrackAddedMemoryAllocation.decorator_repeating()
60+
def track_mem_merge(self):
61+
_ = self.cube_list.concatenate_cube()

benchmarks/benchmarks/regridding.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from iris.analysis import AreaWeighted, PointInCell
1515
from iris.coords import AuxCoord
1616

17+
from . import TrackAddedMemoryAllocation
18+
1719

1820
class HorizontalChunkedRegridding:
1921
def setup(self) -> None:
@@ -51,6 +53,20 @@ def time_regrid_area_w_new_grid(self) -> None:
5153
# Realise data
5254
out.data
5355

56+
@TrackAddedMemoryAllocation.decorator_repeating()
57+
def track_mem_regrid_area_w(self) -> None:
58+
# Regrid the chunked cube
59+
out = self.cube.regrid(self.template_cube, self.scheme_area_w)
60+
# Realise data
61+
out.data
62+
63+
@TrackAddedMemoryAllocation.decorator_repeating()
64+
def track_mem_regrid_area_w_new_grid(self) -> None:
65+
# Regrid the chunked cube
66+
out = self.chunked_cube.regrid(self.template_cube, self.scheme_area_w)
67+
# Realise data
68+
out.data
69+
5470

5571
class CurvilinearRegridding:
5672
def setup(self) -> None:
@@ -93,3 +109,10 @@ def time_regrid_pic(self) -> None:
93109
out = self.cube.regrid(self.template_cube, self.scheme_pic)
94110
# Realise the data
95111
out.data
112+
113+
@TrackAddedMemoryAllocation.decorator_repeating()
114+
def track_mem_regrid_pic(self) -> None:
115+
# Regrid the cube onto the template.
116+
out = self.cube.regrid(self.template_cube, self.scheme_pic)
117+
# Realise the data
118+
out.data

benchmarks/benchmarks/stats.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from iris.analysis.stats import pearsonr
99
import iris.tests
1010

11+
from . import TrackAddedMemoryAllocation
12+
1113

1214
class PearsonR:
1315
def setup(self):
@@ -30,9 +32,21 @@ def setup(self):
3032
def time_real(self):
3133
pearsonr(self.cube_a, self.cube_b, weights=self.weights)
3234

35+
@TrackAddedMemoryAllocation.decorator_repeating()
36+
def track_real(self):
37+
pearsonr(self.cube_a, self.cube_b, weights=self.weights)
38+
3339
def time_lazy(self):
3440
for cube in self.cube_a, self.cube_b:
3541
cube.data = cube.lazy_data()
3642

3743
result = pearsonr(self.cube_a, self.cube_b, weights=self.weights)
3844
result.data
45+
46+
@TrackAddedMemoryAllocation.decorator_repeating()
47+
def track_lazy(self):
48+
for cube in self.cube_a, self.cube_b:
49+
cube.data = cube.lazy_data()
50+
51+
result = pearsonr(self.cube_a, self.cube_b, weights=self.weights)
52+
result.data

benchmarks/benchmarks/trajectory.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import iris
1414
from iris.analysis.trajectory import interpolate
1515

16+
from . import TrackAddedMemoryAllocation
17+
1618

1719
class TrajectoryInterpolation:
1820
def setup(self) -> None:
@@ -33,8 +35,22 @@ def time_trajectory_linear(self) -> None:
3335
# Realise the data
3436
out_cube.data
3537

38+
@TrackAddedMemoryAllocation.decorator_repeating()
39+
def track_trajectory_linear(self) -> None:
40+
# Regrid the cube onto the template.
41+
out_cube = interpolate(self.cube, self.sample_points, method="linear")
42+
# Realise the data
43+
out_cube.data
44+
3645
def time_trajectory_nearest(self) -> None:
3746
# Regrid the cube onto the template.
3847
out_cube = interpolate(self.cube, self.sample_points, method="nearest")
3948
# Realise the data
4049
out_cube.data
50+
51+
@TrackAddedMemoryAllocation.decorator_repeating()
52+
def track_trajectory_nearest(self) -> None:
53+
# Regrid the cube onto the template.
54+
out_cube = interpolate(self.cube, self.sample_points, method="nearest")
55+
# Realise the data
56+
out_cube.data

0 commit comments

Comments
 (0)