Skip to content

Commit b785ea7

Browse files
committed
add comment and test
1 parent 1ecaf4e commit b785ea7

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

memory_profiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
340340
if retval:
341341
ret = ret, returned
342342
if max_usage:
343+
# Convert the one element list produced by MemTimer to a singular value
343344
ret = ret[0]
344345
except Exception:
345346
parent = psutil.Process(os.getpid())

test/test_memory_usage.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,20 @@ def test_memory_usage():
1111
assert ret[0] == (1, 2)
1212
assert ret[1] == dict(a=1)
1313

14+
15+
def test_return_value_consistency():
16+
# Test return values when watching process by PID
17+
pid_mem_list = memory_usage(timeout=1)
18+
assert type(pid_mem_list) == list, "Memory usage of process should be a list"
19+
pid_mem_max = memory_usage(timeout=1, max_usage=True)
20+
assert type(pid_mem_max) == float, "Max memory usage of process should be a number"
21+
# Test return values when watching callable
22+
func_mem_list = memory_usage((some_func, (42,), dict(a=42)))
23+
assert type(func_mem_list) == list, "Memory usage of callable should be a list"
24+
func_mem_max = memory_usage((some_func, (42,), dict(a=42)), max_usage=True)
25+
assert type(func_mem_max) == float, "Max memory usage of callable should be a number"
26+
27+
1428
if __name__ == "__main__":
1529
test_memory_usage()
30+
test_return_value_consistency()

0 commit comments

Comments
 (0)