From c07e9eec39d1b9530a7de2b0c972c57b7536f4bc Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Sun, 17 Sep 2017 10:58:38 +0100 Subject: [PATCH 1/2] Use syds.getsizeof() to get expected memory consumption in tests. This takes care of size differences in cpython on 32-bit versus 64-bit machines. --- .../widgets/tests/test_memoryprofiler.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py b/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py index 91acda7..0a68e2a 100644 --- a/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py +++ b/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py @@ -9,6 +9,7 @@ # Standard library imports import os +import sys # Third party imports from pytestqt import qtbot @@ -36,7 +37,12 @@ def foo(): foo()""" def test_profile_and_display_results(qtbot, tmpdir, monkeypatch): - """Run profiler on simple script and check that results are okay.""" + """ + Run profiler on simple script and check that results are okay. + + This is a fairly simple integration test which checks that the plugin works + on a basic level. + """ os.chdir(tmpdir.strpath) testfilename = tmpdir.join('test_foo.py').strpath @@ -62,12 +68,20 @@ def test_profile_and_display_results(qtbot, tmpdir, monkeypatch): for i in range(6): assert top.child(i).data(0, Qt.DisplayRole) == i + 1 # line no - # column 2 has increment (in Mib); displayed as 'xxx MiB' so need to strip - # last 4 characters - assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) >= 7 # increment (MiB) - assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) <= 8 - assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) >= 150 - assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) <= 160 - assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) >= -160 - assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) <= -150 + # Column 2 has increment (in MiB); displayed as 'xxx MiB' so need to strip + # last 4 characters. Allow for 2% margin in measured memory consumption. + measured = float(top.child(2).data(2, Qt.DisplayRole)[:-4]) + list_size_in_mib = sys.getsizeof([1] * 10 ** 6) / 2 ** 20 + assert measured >= 0.98 * list_size_in_mib + assert measured <= 1.02 * list_size_in_mib + + measured = float(top.child(3).data(2, Qt.DisplayRole)[:-4]) + list_size_in_mib = sys.getsizeof([2] * 2 * 10 ** 7) / 2 ** 20 + assert measured >= 0.98 * list_size_in_mib + assert measured <= 1.02 * list_size_in_mib + + measured = float(top.child(4).data(2, Qt.DisplayRole)[:-4]) + assert measured >= -1.02 * list_size_in_mib + assert measured <= -0.98 * list_size_in_mib + assert float(top.child(5).data(2, Qt.DisplayRole)[:-4]) == 0 From 4da36a276bb1b07e919bf661b23da53431d2de0c Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Mon, 18 Sep 2017 21:02:06 +0100 Subject: [PATCH 2/2] Test: fix for Python 2 and allow for wider margin --- .../widgets/tests/test_memoryprofiler.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py b/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py index 0a68e2a..e687314 100644 --- a/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py +++ b/spyder_memory_profiler/widgets/tests/test_memoryprofiler.py @@ -7,6 +7,8 @@ # ----------------------------------------------------------------------------- """Tests for memoryprofiler.py.""" +from __future__ import division + # Standard library imports import os import sys @@ -69,19 +71,19 @@ def test_profile_and_display_results(qtbot, tmpdir, monkeypatch): assert top.child(i).data(0, Qt.DisplayRole) == i + 1 # line no # Column 2 has increment (in MiB); displayed as 'xxx MiB' so need to strip - # last 4 characters. Allow for 2% margin in measured memory consumption. + # last 4 characters. Allow for 5% margin in measured memory consumption. measured = float(top.child(2).data(2, Qt.DisplayRole)[:-4]) list_size_in_mib = sys.getsizeof([1] * 10 ** 6) / 2 ** 20 - assert measured >= 0.98 * list_size_in_mib - assert measured <= 1.02 * list_size_in_mib + assert measured >= 0.95 * list_size_in_mib + assert measured <= 1.05 * list_size_in_mib measured = float(top.child(3).data(2, Qt.DisplayRole)[:-4]) list_size_in_mib = sys.getsizeof([2] * 2 * 10 ** 7) / 2 ** 20 - assert measured >= 0.98 * list_size_in_mib - assert measured <= 1.02 * list_size_in_mib + assert measured >= 0.95 * list_size_in_mib + assert measured <= 1.05 * list_size_in_mib measured = float(top.child(4).data(2, Qt.DisplayRole)[:-4]) - assert measured >= -1.02 * list_size_in_mib - assert measured <= -0.98 * list_size_in_mib + assert measured >= -1.05 * list_size_in_mib + assert measured <= -0.95 * list_size_in_mib assert float(top.child(5).data(2, Qt.DisplayRole)[:-4]) == 0