mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 08:33:07 +00:00
Add measure_time module to measure time taken by functions
This commit is contained in:
@@ -3,6 +3,7 @@ from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humani
|
|||||||
format_ms_time, shorten_date)
|
format_ms_time, shorten_date)
|
||||||
from freqtrade.util.formatters import decimals_per_coin, fmt_coin, round_value
|
from freqtrade.util.formatters import decimals_per_coin, fmt_coin, round_value
|
||||||
from freqtrade.util.ft_precise import FtPrecise
|
from freqtrade.util.ft_precise import FtPrecise
|
||||||
|
from freqtrade.util.measure_time import MeasureTime
|
||||||
from freqtrade.util.periodic_cache import PeriodicCache
|
from freqtrade.util.periodic_cache import PeriodicCache
|
||||||
from freqtrade.util.template_renderer import render_template, render_template_with_fallback # noqa
|
from freqtrade.util.template_renderer import render_template, render_template_with_fallback # noqa
|
||||||
|
|
||||||
@@ -24,4 +25,5 @@ __all__ = [
|
|||||||
'decimals_per_coin',
|
'decimals_per_coin',
|
||||||
'round_value',
|
'round_value',
|
||||||
'fmt_coin',
|
'fmt_coin',
|
||||||
|
'MeasureTime',
|
||||||
]
|
]
|
||||||
|
|||||||
42
freqtrade/util/measure_time.py
Normal file
42
freqtrade/util/measure_time.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import logging
|
||||||
|
import time
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from cachetools import TTLCache
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class MeasureTime:
|
||||||
|
"""
|
||||||
|
Measure the time of a block of code and call a callback if the time limit is exceeded.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def __init__(self, callback: Callable[[int, int], None], time_limit: int, ttl: int = 3600 * 4):
|
||||||
|
"""
|
||||||
|
:param callback: The callback to call if the time limit is exceeded.
|
||||||
|
This callback will be called once every "ttl" seconds,
|
||||||
|
with the parameters "duration" (in seconds) and
|
||||||
|
"time limit" - representing the passed in time limit.
|
||||||
|
:param time_limit: The time limit in seconds.
|
||||||
|
:param ttl: The time to live of the cache in seconds.
|
||||||
|
"""
|
||||||
|
self._callback = callback
|
||||||
|
self._time_limit = time_limit
|
||||||
|
self.__cache = TTLCache(maxsize=1, ttl=ttl)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self._start = time.time()
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
end = time.time()
|
||||||
|
if self.__cache.get('value'):
|
||||||
|
return
|
||||||
|
duration = end - self._start
|
||||||
|
|
||||||
|
if duration < self._time_limit:
|
||||||
|
return
|
||||||
|
self._callback(duration, self._time_limit)
|
||||||
|
|
||||||
|
self.__cache['value'] = True
|
||||||
Reference in New Issue
Block a user