diff options
author | nicolargo <nicolas@nicolargo.com> | 2022-02-26 15:45:39 +0100 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2022-02-26 15:45:39 +0100 |
commit | be927fda3dc4118b77ad0f88d5e6deb652a5f5b3 (patch) | |
tree | 78b92040c2e27b0fa6345a4c4b2a22b37f00404f /unitest.py | |
parent | 5098ac009bcf30ebf5e257510df3322394112d31 (diff) |
Prepare memory leak test. Not active for the moment
Diffstat (limited to 'unitest.py')
-rwxr-xr-x | unitest.py | 51 |
1 files changed, 49 insertions, 2 deletions
@@ -21,6 +21,7 @@ """Glances unitary tests suite.""" import time +from tracemalloc import Snapshot import unittest from glances.main import GlancesMain @@ -36,16 +37,19 @@ from glances.thresholds import GlancesThresholds from glances.plugins.glances_plugin import GlancesPlugin from glances.compat import subsample, range from glances.secure import secure_popen +from glances.compat import PY3 # Global variables # ================= # Init Glances core core = GlancesMain() +test_config = core.get_config() +test_args = core.get_args() # Init Glances stats -stats = GlancesStats(config=core.get_config(), - args=core.get_args()) +stats = GlancesStats(config=test_config, + args=test_args) # Unitest class # ============== @@ -383,6 +387,49 @@ class TestGlances(unittest.TestCase): self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') + def test_200_memory_leak(self): + """Memory leak check""" + # Only available in PY3 + if not PY3: + return + import tracemalloc + print('INFO: [TEST_200] Memory leak check') + tracemalloc.start() + # 3 iterations just to init the stats and fill the memory + for _ in range(3): + stats.update() + + # Start the memory leak check + snapshot_begin = tracemalloc.take_snapshot() + for _ in range(3): + stats.update() + snapshot_end = tracemalloc.take_snapshot() + snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') + memory_leak = sum([s.size_diff for s in snapshot_diff]) + print('INFO: Memory leak: {} bytes'.format(memory_leak)) + + # snapshot_begin = tracemalloc.take_snapshot() + for _ in range(30): + stats.update() + snapshot_end = tracemalloc.take_snapshot() + snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') + memory_leak = sum([s.size_diff for s in snapshot_diff]) + print('INFO: Memory leak: {} bytes'.format(memory_leak)) + + # snapshot_begin = tracemalloc.take_snapshot() + for _ in range(300): + stats.update() + snapshot_end = tracemalloc.take_snapshot() + snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') + memory_leak = sum([s.size_diff for s in snapshot_diff]) + print('INFO: Memory leak: {} bytes'.format(memory_leak)) + snapshot_top = snapshot_end.compare_to(snapshot_begin, 'traceback') + print("Memory consumption (top 5):") + for stat in snapshot_top[:5]: + print(stat) + for line in stat.traceback.format(): + print(line) + def test_999_the_end(self): """Free all the stats""" print('INFO: [TEST_999] Free the stats') |