
                         @    S r SSKrSSKrSr " S S\5      r\" 5       rg)a  A useful class for digesting, on a high-level, where time in a program goes.

Usage:

sw = StopWatch()
sw.start()
sw.start('foo')
foo()
sw.stop('foo')
args = overhead_code()
sw.start('bar')
bar(args)
sw.stop('bar')
sw.dump()

If you start a new timer when one is already running, then the other one will
stop running, and restart when you stop this timer.  This behavior is very
useful for when you want to try timing for a subcall without remembering
what is already running.  For instance:

sw.start('all_this')
do_some_stuff()
sw.start('just_that')
small_but_expensive_function()
sw.stop('just_that')
cleanup_code()
sw.stop('all_this')

In this case, the output will be what you want:  the time spent in
small_but_expensive function will show up in the timer for just_that and not
all_this.
    Nz!dbentley@google.com (Dan Bentley)c                   Z    \ rS rSrSrS rSS jrSS jrSS jrSS jr	SS	 jr
SS
 jrSrg)	StopWatch8   a  Class encapsulating a timer; see above for example usage.

Instance variables:
  timers: map of stopwatch name -> time for each currently running stopwatch,
          where time is seconds from the epoch of when this stopwatch was
          started.
  accum: map of stopwatch name -> accumulated time, in seconds, it has
          already been run for.
  stopped: map of timer name -> list of timer names that are blocking it.
  counters: map of timer name -> number of times it has been started.
c                 <    0 U l         0 U l        0 U l        0 U l        g N)timersaccumstoppedcounters)selfs    4platform/bq/third_party/google/apputils/stopwatch.py__init__StopWatch.__init__E   s    DKDJDLDM    c                 Z   U(       aV  / n[        U R                  5       H-  nUS:X  a  M  U R                  U5        UR                  U5        M/     X0R                  U'   U R
                  R                  US5      S-   U R
                  U'   [        R                  " 5       U R                  U'   g)a[  Start a timer.

Args:
  timer: str; name of the timer to start, defaults to the overall timer.
  stop_others: bool; if True, stop all other running timers.  If False, then
               you can have time that is spent inside more than one timer
               and there's a good chance that the overhead measured will be
               negative.
totalr      N)listr   stopappendr
   r   gettime)r   timerstop_othersr
   others        r   startStopWatch.startK   s     g$%
))E

..
 % $ll5==,,UA6:DMM%DKKr   c                     XR                   ;  a  [        SU-  5      eU R                  U5      U R                  U'   U R                   U	 U R                  R                  U/ 5       H  nU R                  USS9  M     g)zStop a running timer.

This includes restarting anything that was stopped on behalf of this timer.

Args:
  timer: str; name of the timer to stop, defaults to the overall timer.

Raises:
  RuntimeError: if timer refers to a timer that was never started.
z.Tried to stop timer that was never started: %sr   )r   N)r   RuntimeError
timervaluer	   r
   r   r   )r   r   r
   s      r   r   StopWatch.stop_   ss     KK
:U
BD D.DJJuE<<##E2.
jjaj( /r   Nc                     U(       d  [         R                   " 5       nXR                  ;   a.  U R                  R                  US5      X R                  U   -
  -   $ XR                  ;   a  U R                  U   $ g)at  Return the value seen by this timer so far.

If the timer is stopped, this will be the accumulated time it has seen.
If the timer is running, this will be the time it has seen up to now.
If the timer has never been started, this will be zero.

Args:
  timer: str; the name of the timer to report on.
  now: long; if provided, the time to use for 'now' for running timers.
        )r   r   r	   r   )r   r   nows      r   r    StopWatch.timervaluer   s`     IIKcZZ^^E3'3U1C+CDD	**	ZZ r   c                     U R                  SU5      nUS:X  a  g[        U R                  R                  5       5      nX#U-
  -
  $ )a  Calculate the overhead.

Args:
  now: (optional) time to use as the current time.

Returns:
  The overhead, that is, time spent in total but not in any sub timer.  This
  may be negative if time was counted in two sub timers.  Avoid this by
  always using stop_others.
r   r#   )r    sumr	   
itervalues)r   r$   r   
all_timerss       r   overheadStopWatch.overhead   sC     OOGS)E|TZZ**,-J&''r   c           	         [         R                   " 5       nU R                  R                  5       n/ nSU;   a  UR                  S5        UR	                  5         U(       a  UnU Vs/ s H!  nXPR                  XRS9U R                  U   4PM#     nnU(       a!  UR                  SU R                  US9S45        SU R                  ;   d  SU R                  ;   a/  UR                  SU R                  SUS9U R                  S   45        U$ s  snf )aH  Get the results of this stopwatch.

Args:
  verbose: bool; if True, show all times; otherwise, show only the total.

Returns:
  A list of tuples showing the output of this stopwatch, of the form
  (name, value, num_starts) for each timer.  Note that if the total timer
  is not used, non-verbose results will be the empty list.
r   )r$   r*   r   )
r   r	   keysremovesortr    r   r   r*   r   )r   verboser$   	all_namesnamesnameresultss          r   r4   StopWatch.results   s     ))+C

!IE)wNNe !" 4 oodo4dmmD6IJ   "nnj$--C-"8!<=$**4;; 6nngtwC@mmG,. /N"s   &(D	c           	         [         R                   " 5       nU R                  US9n[        U Vs/ s H  n[        US   5      PM     sn5      nU H  nUR	                  SXTS   US   4-  5        M!     UR                  5       $ s  snf )zDescribes where time in this stopwatch was spent.

Args:
  verbose: bool; if True, show all timers; otherwise, show only the total.

Returns:
  A string describing the stopwatch.
)r0   r   z%*s: %6.2fs
r   )StringIOr4   maxlenwritegetvalue)r   r0   outputr4   result	maxlengths         r   dumpStopWatch.dump   s}      Fll7l+G':'S^':;Ill?iF1I%FFG ?? ;s   B)r	   r   r
   r   )r   T)r   )r   Nr   )F)__name__
__module____qualname____firstlineno____doc__r   r   r   r    r*   r4   r?   __static_attributes__ r   r   r   r   8   s*    
%()&0($>r   r   )rE   r7   r   	__owner__objectr   swrG   r   r   <module>rK      s2    B   0	Q Qh [r   