Last active
April 22, 2020 01:59
-
-
Save workze/bc52e72943b2168d8982562ee36405d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import functools | |
def timer(timer_id): | |
def decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kw): | |
print('%s %s():' % (timer_id, func.__name__)) | |
start_time = time.time() | |
result = func(*args, **kw) | |
end_time = time.time() | |
spent_time = end_time - start_time | |
# 记录时间 | |
print(timer_id, 'spend', spent_time) | |
return result | |
return wrapper | |
return decorator | |
class A(): | |
@timer('metric.timer.aau.receive.image') | |
def do(self): | |
time.sleep(1) | |
if __name__ == '__main__': | |
a = A() | |
a.do() | |
============================= | |
def feature(*ids): | |
def feature_wrapper(f): | |
def wrapper(*args, **kwargs): | |
f(*args, **kwargs) | |
return wrapper | |
return feature_wrapper | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment