🐛 Fix time_this decorator
Wasn't actually working as a decorator with parameters, and monotonic_time also turned out to be too unprecise a time source.
This commit is contained in:
1 parent
687e6ad900
commit
098e5f2516
2 files changed
+23
-24
No files matched your search
@@ -1373,10 +1373,9 @@ class PluginManager(object):
|
||||
# wrap method with time_this
|
||||
setattr(plugin.implementation,
|
||||
method,
|
||||
time_this(getattr(plugin.implementation, method),
|
||||
logtarget=self.plugin_timings_logtarget,
|
||||
time_this(logtarget=self.plugin_timings_logtarget,
|
||||
expand_logtarget=True,
|
||||
message=self.plugin_timings_message))
|
||||
message=self.plugin_timings_message)(getattr(plugin.implementation, method)))
|
||||
|
||||
self.plugin_implementations[name] = plugin.implementation
|
||||
setattr(plugin.implementation, "__timing_wrapped", True)
|
||||
@@ -1710,10 +1709,9 @@ class PluginManager(object):
|
||||
|
||||
result = OrderedDict()
|
||||
for h in self.plugin_hooks[hook]:
|
||||
result[h[0]] = time_this(h[1],
|
||||
logtarget=self.plugin_timings_logtarget,
|
||||
result[h[0]] = time_this(logtarget=self.plugin_timings_logtarget,
|
||||
expand_logtarget=True,
|
||||
message=self.plugin_timings_message)
|
||||
message=self.plugin_timings_message)(h[1])
|
||||
return result
|
||||
|
||||
def get_implementations(self, *types, **kwargs):
|
||||
|
||||
@@ -1734,26 +1734,27 @@ def fqfn(f):
|
||||
f.__name__)
|
||||
|
||||
|
||||
def time_this(f, logtarget="octoprint.util.timings", expand_logtarget=False, message="{func} took {timing:.2f}ms"):
|
||||
@wraps(f)
|
||||
def decorator(*args, **kwargs):
|
||||
start = monotonic_time()
|
||||
try:
|
||||
return f(*args, **kwargs)
|
||||
finally:
|
||||
timing = (monotonic_time() - start) * 1000.0
|
||||
func = fqfn(f)
|
||||
def time_this(logtarget="octoprint.util.timings", expand_logtarget=False, message="{func} took {timing:.2f}ms"):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
start = time.time()
|
||||
try:
|
||||
return f(*args, **kwargs)
|
||||
finally:
|
||||
timing = (time.time() - start) * 1000.0
|
||||
func = fqfn(f)
|
||||
|
||||
l = logtarget
|
||||
if expand_logtarget:
|
||||
l += "." + func
|
||||
|
||||
logger = logging.getLogger(l)
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
data = {"func": func, "timing": timing}
|
||||
logger.debug(message.format(**data),
|
||||
extra=data)
|
||||
l = logtarget
|
||||
if expand_logtarget:
|
||||
l += "." + func
|
||||
|
||||
logger = logging.getLogger(l)
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
data = {"func": func, "timing": timing}
|
||||
logger.debug(message.format(**data),
|
||||
extra=data)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user