11import sys
22import time
3+ import tracemalloc
34from typing import Any , Callable , Tuple
45
56import 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+
6182def 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
83105def 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
118141def 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
147171def 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
187212def 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