|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright © 2017 Spyder Project Contributors |
|
4
|
|
|
# Licensed under the terms of the MIT License |
|
5
|
|
|
# (see LICENSE.txt for details) |
|
6
|
|
|
|
|
7
|
|
|
"""Tests for memoryprofiler.py.""" |
|
8
|
|
|
|
|
9
|
|
|
# Standard library imports |
|
10
|
|
|
import os |
|
11
|
|
|
|
|
12
|
|
|
# Third party imports |
|
13
|
|
|
from pytestqt import qtbot |
|
14
|
|
|
from qtpy.QtCore import Qt |
|
15
|
|
|
|
|
16
|
|
|
from spyder.utils.qthelpers import qapplication |
|
17
|
|
|
MAIN_APP = qapplication() |
|
18
|
|
|
|
|
19
|
|
|
# Local imports |
|
20
|
|
|
from spyder_memory_profiler.widgets.memoryprofiler import MemoryProfilerWidget |
|
21
|
|
|
|
|
22
|
|
|
try: |
|
23
|
|
|
from unittest.mock import Mock |
|
24
|
|
|
except ImportError: |
|
25
|
|
|
from mock import Mock # Python 2 |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
TEST_SCRIPT = \ |
|
29
|
|
|
"""@profile |
|
30
|
|
|
def foo(): |
|
31
|
|
|
a = [1] * (10 ** 6) |
|
32
|
|
|
b = [2] * (2 * 10 ** 7) |
|
33
|
|
|
del b |
|
34
|
|
|
return a |
|
35
|
|
|
foo()""" |
|
36
|
|
|
|
|
37
|
|
|
def test_profile_and_display_results(qtbot, tmpdir, monkeypatch): |
|
38
|
|
|
"""Run profiler on simple script and check that results are okay.""" |
|
39
|
|
|
os.chdir(tmpdir.strpath) |
|
40
|
|
|
testfilename = tmpdir.join('test_foo.py').strpath |
|
41
|
|
|
|
|
42
|
|
|
with open(testfilename, 'w') as f: |
|
43
|
|
|
f.write(TEST_SCRIPT) |
|
44
|
|
|
|
|
45
|
|
|
MockQMessageBox = Mock() |
|
46
|
|
|
monkeypatch.setattr('spyder_memory_profiler.widgets.memoryprofiler.QMessageBox', |
|
47
|
|
|
MockQMessageBox) |
|
48
|
|
|
|
|
49
|
|
|
widget = MemoryProfilerWidget(None) |
|
50
|
|
|
qtbot.addWidget(widget) |
|
51
|
|
|
with qtbot.waitSignal(widget.sig_finished, timeout=10000, raising=True): |
|
52
|
|
|
widget.analyze(testfilename) |
|
53
|
|
|
|
|
54
|
|
|
MockQMessageBox.assert_not_called() |
|
55
|
|
|
dt = widget.datatree |
|
56
|
|
|
assert dt.topLevelItemCount() == 1 # number of functions profiled |
|
57
|
|
|
|
|
58
|
|
|
top = dt.topLevelItem(0) |
|
59
|
|
|
assert top.data(0, Qt.DisplayRole).startswith('foo ') |
|
60
|
|
|
assert top.childCount() == 6 |
|
61
|
|
|
for i in range(6): |
|
62
|
|
|
assert top.child(i).data(0, Qt.DisplayRole) == i + 1 # line no |
|
63
|
|
|
|
|
64
|
|
|
# column 2 has increment (in Mib); displayed as 'xxx MiB' so need to strip |
|
65
|
|
|
# last 4 characters |
|
66
|
|
|
assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) >= 7 # increment (MiB) |
|
67
|
|
|
assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) <= 8 |
|
68
|
|
|
assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) >= 150 |
|
69
|
|
|
assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) <= 160 |
|
70
|
|
|
assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) >= -160 |
|
71
|
|
|
assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) <= -150 |
|
72
|
|
|
assert float(top.child(5).data(2, Qt.DisplayRole)[:-4]) == 0 |
|
73
|
|
|
|