| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from pathlib import Path |
| 18 |
from unittest.mock import Mock |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
|
| 23 |
try: |
| 24 |
from cylc.flow.main_loop.log_memory import ( |
| 25 |
_compute_sizes, |
| 26 |
_transpose, |
| 27 |
_dump, |
| 28 |
_plot |
| 29 |
) |
| 30 |
except ModuleNotFoundError as exc: |
| 31 |
if exc.name == 'pympler': |
| 32 |
pytest.skip( |
| 33 |
'pympler required for these tests', |
| 34 |
allow_module_level=True |
| 35 |
) |
| 36 |
else: |
| 37 |
raise |
| 38 |
|
| 39 |
|
| 40 |
def test_compute_sizes(): |
| 41 |
"""Test the interface for the calculation of instance attribute sizes.""" |
| 42 |
keys = { |
| 43 |
'a': [], |
| 44 |
'b': 42, |
| 45 |
'c': 'beef wellington' |
| 46 |
} |
| 47 |
test_object = Mock(**keys) |
| 48 |
# no fields should be larger than 10kb |
| 49 |
sizes = _compute_sizes(test_object, 10000) |
| 50 |
sizes.pop('total') |
| 51 |
assert sizes == {} |
| 52 |
# all fields should be larger than 0kb |
| 53 |
ret = _compute_sizes(test_object, 0) |
| 54 |
assert { |
| 55 |
key |
| 56 |
for key, value in ret.items() |
| 57 |
# filter out mock fields |
| 58 |
if not key.startswith('_') |
| 59 |
and key not in ('method_calls', 'total') |
| 60 |
} == set(keys) |
| 61 |
|
| 62 |
|
| 63 |
@pytest.fixture() |
| 64 |
def test_data(): |
| 65 |
return [ |
| 66 |
(5, {'a': 1, 'b': 2, 'c': 3}), |
| 67 |
(6, {'a': 2, 'c': 4}), |
| 68 |
(7, {'a': 5, 'c': 2}) |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
def test_transpose(test_data): |
| 73 |
"""Test transposing the data from bin to series orientated.""" |
| 74 |
assert _transpose(test_data) == ( |
| 75 |
{ |
| 76 |
# the keys are sorted by their last entry |
| 77 |
'a': [1, 2, 5], |
| 78 |
'c': [3, 4, 2], |
| 79 |
'b': [2, -1, -1] # missing values become -1 |
| 80 |
}, |
| 81 |
[0, 1, 2] |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
def test_dump(test_data, tmp_path): |
| 86 |
"""Ensure the data is serialiseable.""" |
| 87 |
_dump(test_data, tmp_path) |
| 88 |
assert list(tmp_path.iterdir()) == [ |
| 89 |
Path(tmp_path, 'cylc.flow.main_loop.log_memory.json') |
| 90 |
] |
| 91 |
|
| 92 |
|
| 93 |
def test_plot(test_data, tmp_path): |
| 94 |
"""Ensure the plotting mechanism doesn't raise errors.""" |
| 95 |
fields, times = _transpose(test_data) |
| 96 |
_plot(fields, times, tmp_path) |
| 97 |
assert list(tmp_path.iterdir()) == [ |
| 98 |
Path(tmp_path, 'cylc.flow.main_loop.log_memory.pdf') |
| 99 |
] |