Test Failed
Push — develop ( c372d3...3b67eb )
by Nicolas
05:18 queued 02:50
created

test_memoryleak   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A test_memoryleak() 0 24 3
1
#!/usr/bin/env python
2
#
3
# Glances - An eye on your system
4
#
5
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <[email protected]>
6
#
7
# SPDX-License-Identifier: LGPL-3.0-only
8
#
9
10
"""Glances unitary tests suite for Glances memory leak."""
11
12
import time
13
import tracemalloc
14
15
16
def test_memoryleak(glances_stats_no_history, logger):
17
    """
18
    Test Glances memory leak.
19
    """
20
    tracemalloc.start()
21
    # First iterations just to init the stats and fill the memory
22
    logger.info('Please wait test is filling memory with stats')
23
    iteration = 10
24
    for _ in range(iteration):
25
        glances_stats_no_history.update()
26
        time.sleep(1)
27
28
    # Then iteration to measure memory leak
29
    logger.info('Please wait test if a memory leak is detected')
30
    iteration = 20
31
    snapshot_begin = tracemalloc.take_snapshot()
32
    for _ in range(iteration):
33
        glances_stats_no_history.update()
34
        time.sleep(1)
35
    snapshot_end = tracemalloc.take_snapshot()
36
    snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename')
37
    memory_leak = sum([s.size_diff for s in snapshot_diff]) // iteration
38
    logger.info(f'Memory consume per iteration: {memory_leak} bytes')
39
    assert memory_leak < 1000, f'Memory leak: {memory_leak} bytes'
40