From 492fd109ed92f15e421ebbdbd4fba5a9129eef17 Mon Sep 17 00:00:00 2001 From: Nate Johnston Date: Tue, 26 May 2020 12:29:18 -0400 Subject: [PATCH] Migrate timecost decorator to neutron-lib There are a few functions in neutron-lib where timecost profiling would be beneficial, so that means it is a good time to transition this library to neutron-lib. Change-Id: I7c84a83a2f836493f63b220f7ff0cbcc823374f2 --- neutron_lib/utils/helpers.py | 21 +++++++++++++++++++ .../rehome-timecost-90dcfc8c7f7280f5.yaml | 6 ++++++ 2 files changed, 27 insertions(+) create mode 100644 releasenotes/notes/rehome-timecost-90dcfc8c7f7280f5.yaml diff --git a/neutron_lib/utils/helpers.py b/neutron_lib/utils/helpers.py index cbc23bf33..ffc7a7a20 100644 --- a/neutron_lib/utils/helpers.py +++ b/neutron_lib/utils/helpers.py @@ -16,9 +16,16 @@ import decimal import random import weakref +from oslo_log import log as logging +from oslo_utils import timeutils +from oslo_utils import uuidutils + from neutron_lib._i18n import _ +LOG = logging.getLogger(__name__) + + def parse_mappings(mapping_list, unique_values=True, unique_keys=True): """Parse a list of mapping strings into a dictionary. @@ -198,3 +205,17 @@ def resolve_ref(ref): if isinstance(ref, weakref.ref): ref = ref() return ref + + +def timecost(f): + call_id = uuidutils.generate_uuid() + message_base = ("Time-cost: call %(call_id)s function %(fname)s ") % { + "call_id": call_id, "fname": f.__name__} + end_message = (message_base + "took %(seconds).3fs seconds to run") + + @timeutils.time_it(LOG, message=end_message, min_duration=None) + def wrapper(*args, **kwargs): + LOG.debug(message_base + "start") + ret = f(*args, **kwargs) + return ret + return wrapper diff --git a/releasenotes/notes/rehome-timecost-90dcfc8c7f7280f5.yaml b/releasenotes/notes/rehome-timecost-90dcfc8c7f7280f5.yaml new file mode 100644 index 000000000..8cc886f0d --- /dev/null +++ b/releasenotes/notes/rehome-timecost-90dcfc8c7f7280f5.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The ``timecost`` decorator is available in ``neutron_lib.utils.helpers`` + now. This permits functions to be decorated with functionality that will + emit a debug log with the time it took to execute the function.