3
from typing import Optional
5
from golem.core.log import default_log
10
active_session_label = 'main'
15
Start memory monitoring session
23
Finish memory monitoring session
25
cls.log(additional_info='finish')
30
def get_measures(cls):
32
Estimates Python-related system memory consumption in MiB
33
:return: current and maximal consumption
35
current_memory, max_memory = tracemalloc.get_traced_memory()
36
return current_memory / 1024 / 1024, max_memory / 1024 / 1024
39
def log(cls, logger: Optional[logging.LoggerAdapter] = None,
40
additional_info: str = 'location', logging_level: int = logging.INFO) -> str:
42
Print the message about current and maximal memory consumption to the log or console.
43
:param logger: optional logger that should be used in output.
44
:param additional_info: label for current location in code.
45
:param logging_level: level of the message
46
:return: text of the message.
50
memory_consumption = cls.get_measures()
51
message = f'Memory consumption for {additional_info} in {cls.active_session_label} session: ' \
52
f'current {round(memory_consumption[0], 1)} MiB, ' \
53
f'max: {round(memory_consumption[1], 1)} MiB'
55
logger = default_log(prefix=cls.__name__)
56
logger.log(logging_level, message)