4 from indico
.util
.console
import colored
7 class Benchmark(object):
8 """Simple benchmark class
10 Can be used manually or as a contextmanager:
12 with Benchmark() as b:
16 def __init__(self
, start
=False):
17 self
._start
_time
= None
23 self
._start
_time
= time
.time()
27 self
._end
_time
= time
.time()
32 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
36 if self
._start
_time
is None:
37 return float('-inf') # not started
38 elif self
._end
_time
is None:
39 return float('inf') # not finished
40 return self
._end
_time
- self
._start
_time
43 duration
= float(self
)
46 return '{:.05f}'.format(duration
)
50 def print_result(self
, slow
=float('inf'), veryslow
=float('inf')):
51 duration
= float(self
)
52 if duration
== float('-inf'):
53 print colored('skipped', 'blue', attrs
=['bold'])
54 elif duration
== float('inf'):
55 print colored('running', 'red')
56 elif duration
>= veryslow
:
57 print colored(str(self
), 'red', attrs
=['bold'])
58 elif duration
>= slow
:
59 print colored(str(self
), 'yellow', attrs
=['bold'])
61 print colored(str(self
), 'green', attrs
=['bold'])