o x[hg @sRddlZddlZddlZeeZGdddZddddededefd d Z dS) Nc@s>eZdZdZddddededefddZd d Zd d Zd S)Timeda A context manager which measures and optionally logs context run time. :param msg: A message that describes the thing that is being measured :param threshold: Threshold, in seconds. When the context exceeds this threshold, a log will be made. :param log_mode: Control whether to log. Defaults to "threshold". Possible values include: "always" - Always log 'msg', even when 'threshold' is not reached. "threshold" - Log when context time exceeds 'threshold'. "skip" - Do not log. Context time and message are stored in the 'output' and 'delta' attributes, respectively. Used to manually coalesce with other logs at the call site. usage: this call: ``` with Timed("Configuring the network"): run_configure() ``` might produce this log: ``` Configuring the network took 0.100 seconds ``` {Gz? thresholdrlog_modemsgrcCs(||_||_||_d|_d|_d|_dS)Ng)rrroutputstartdelta)selfrrrr 7/usr/lib/python3/dist-packages/cloudinit/performance.py__init__%s  zTimed.__init__cCst|_|S)N)time monotonicr )r r r r __enter__3s zTimed.__enter__cCst|j|_d|jdd}d|jkrtd|j|dSd|jkr&dSd|jkrF|j|jkrDtd|j||jd||_ dSdSt d |jd ) Nztook z.3fz secondsalwaysz%s %sskipr zInvalid Timed log_mode value: 'z'.) rrr r rLOGdebugrrr ValueError)r exc_typeexc_valexc_tbsuffixr r r__exit__7s     zTimed.__exit__N) __name__ __module__ __qualname____doc__strfloatrrrr r r rrs   rrrrrrcsfdd}|S)a A decorator which measures and optionally logs context run time. :param msg: A message that describes the thing that is being measured :param threshold: Threshold, in seconds. When the context exceeds this threshold, a log will be made. :param log_mode: Control whether to log. Defaults to "threshold". Possible values include: "always" - Always log 'msg', even when 'threshold' is not reached. "threshold" - Log when context time exceeds 'threshold'. usage: this call: ``` @timed("Configuring the network") def run_configure(): ... ``` might produce this log: ``` Configuring the network took 0.100 seconds ``` cs tfdd}|S)Ncs@td|i|WdS1swYdS)Nr)r)argskwargs)funcrrrr r decoratords $z)timed..wrapper..decorator) functoolswraps)r&r'rrr)r&rwrappercsztimed..wrapperr )rrrr+r r*rtimedHsr,) r(loggingr getLoggerrrrr"r#r,r r r rs  "@