Skip to content

Commit aac221c

Browse files
committed
Add memory profiling test
1 parent 3246316 commit aac221c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"editor.codeActionsOnSave": {
1111
"source.fixAll": "explicit"
1212
}
13-
}
14-
}
13+
},
14+
"files.insertFinalNewline": true,
15+
}

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
markers =
3+
timing: mark test as time profiling test
4+
memory: mark test as memory profiling test

test/test_xarray_io.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import time
3+
import tracemalloc
34
from typing import Any, Callable, Tuple
45

56
import dask.array as da
@@ -58,6 +59,26 @@ def profile_function(
5859
time_file.write(f"| {name} | {print_args} | {end_time - start_time} |\n")
5960

6061

62+
def mem_check_function(
63+
func: Callable,
64+
args: Tuple[Any, ...],
65+
memory_file,
66+
print_args={},
67+
) -> None:
68+
tracemalloc.start()
69+
func(*args)
70+
snapshot = tracemalloc.take_snapshot()
71+
tracemalloc.stop()
72+
73+
stats_formatted = "".join(
74+
[f"<li>{s}</li>" for s in snapshot.statistics("lineno")[:10]]
75+
)
76+
name = getattr(func, "__name__", "unknown")
77+
memory_file.write(
78+
f"| {name} | {print_args} | <ol>{stats_formatted}</ol> |\n"
79+
)
80+
81+
6182
def create_and_write_jinja(tmp_path, data: xr.DataArray):
6283
env = jinja2.Environment(
6384
loader=jinja2.PackageLoader("flopy4.xarray_jinja"),
@@ -80,6 +101,7 @@ def create_and_write_jinja(tmp_path, data: xr.DataArray):
80101
test_combinations,
81102
)
82103
@pytest.mark.skip("Too slow for large data")
104+
@pytest.mark.timing
83105
def test_xarray_to_text_jinja(tmp_path, max_size, chunks, time_file):
84106
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
85107
data = data.chunk(chunks)
@@ -115,6 +137,7 @@ def create_and_write_pandas(tmp_path, data: xr.DataArray):
115137
"max_size,chunks",
116138
test_combinations,
117139
)
140+
@pytest.mark.timing
118141
def test_xarray_to_text_pandas(tmp_path, max_size, chunks, time_file):
119142
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
120143
data = data.chunk(chunks)
@@ -144,6 +167,7 @@ def create_and_write_np_savetxt(tmp_path, data: xr.DataArray):
144167
test_combinations,
145168
)
146169
@pytest.mark.skip("Too slow for large data")
170+
@pytest.mark.timing
147171
def test_xarray_to_text_np_savetxt(tmp_path, max_size, chunks, time_file):
148172
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
149173
data = data.chunk(chunks)
@@ -184,6 +208,7 @@ def create_and_write_extras(tmp_path, data: xr.DataArray):
184208
"max_size,chunks",
185209
test_combinations,
186210
)
211+
@pytest.mark.timing
187212
def test_xarray_to_text_extras(tmp_path, max_size, chunks, time_file):
188213
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
189214
data = data.chunk(chunks)
@@ -197,3 +222,23 @@ def test_xarray_to_text_extras(tmp_path, max_size, chunks, time_file):
197222
with open(tmp_path / "test_xarray_to_text_extras.disu", "r") as f:
198223
output = f.readlines()
199224
assert len(output) == 3
225+
226+
227+
@pytest.mark.parametrize(
228+
"max_size,chunks",
229+
test_combinations,
230+
)
231+
@pytest.mark.memory
232+
def test_xarray_to_text_extras_mem(tmp_path, max_size, chunks, memory_file):
233+
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
234+
data = data.chunk(chunks)
235+
mem_check_function(
236+
create_and_write_extras,
237+
(tmp_path, data),
238+
memory_file,
239+
print_args={"max_size": max_size, "chunks": chunks},
240+
)
241+
242+
with open(tmp_path / "test_xarray_to_text_extras.disu", "r") as f:
243+
output = f.readlines()
244+
assert len(output) == 3

0 commit comments

Comments
 (0)