Skip to content

Commit fdf4488

Browse files
committed
added max_iterations parameter to memory_usage function; added corresponding test case
1 parent ad64e49 commit fdf4488

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

memory_profiler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def run(self):
238238

239239
def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
240240
include_children=False, multiprocess=False, max_usage=False,
241-
retval=False, stream=None, backend=None):
241+
retval=False, stream=None, backend=None, max_iterations=None):
242242
"""
243243
Return the memory usage of a process or piece of code
244244
@@ -307,6 +307,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
307307
else:
308308
# for a Python function wait until it finishes
309309
max_iter = float('inf')
310+
if max_iterations is not None:
311+
max_iter = max_iterations
310312

311313
if callable(proc):
312314
proc = (proc, (), {})
@@ -320,7 +322,9 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
320322
else:
321323
raise ValueError
322324

325+
current_iter = 0
323326
while True:
327+
current_iter += 1
324328
child_conn, parent_conn = Pipe() # this will store MemTimer's results
325329
p = MemTimer(os.getpid(), interval, child_conn, backend,
326330
timestamps=timestamps,
@@ -349,7 +353,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
349353
raise
350354

351355
p.join(5 * interval)
352-
if n_measurements > 4 or interval < 1e-6:
356+
357+
if (n_measurements > 4) or (current_iter == max_iter) or (interval < 1e-6):
353358
break
354359
interval /= 10.
355360
elif isinstance(proc, subprocess.Popen):

test/test_memory_usage.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from memory_profiler import memory_usage
2+
import os
23

34

45
def some_func(*args, **kwargs):
@@ -10,6 +11,20 @@ def test_memory_usage():
1011
mem, ret = memory_usage((some_func, (1, 2), dict(a=1)), retval=True)
1112
assert ret[0] == (1, 2)
1213
assert ret[1] == dict(a=1)
14+
15+
16+
def write_line(filepath):
17+
with open(filepath, 'a') as the_file:
18+
the_file.write('Testing\n')
19+
20+
def test_max_iterations():
21+
# Check that memory_usage works with max_iterations set (for python functions).
22+
this_dir = os.path.dirname(os.path.realpath(__file__))
23+
file = os.path.join(this_dir, 'temp_test_max_iterations_file.txt')
24+
mem = memory_usage((write_line, (file, ), dict()), max_usage=True, max_iterations=1)
25+
n_lines = sum(1 for line in open(file))
26+
os.remove(file)
27+
assert n_lines == 1
1328

1429

1530
def test_return_value_consistency():
@@ -27,4 +42,5 @@ def test_return_value_consistency():
2742

2843
if __name__ == "__main__":
2944
test_memory_usage()
45+
test_max_iterations()
3046
test_return_value_consistency()

0 commit comments

Comments
 (0)