yandex-tank/Tank/stepper/info.py

109 lines
2.8 KiB
Python
Raw Normal View History

2013-05-27 13:06:28 +00:00
from progressbar import ProgressBar, ETA, ReverseBar, Bar
2013-07-16 10:56:26 +00:00
from collections import namedtuple
import logging
2013-05-27 13:06:28 +00:00
class DefaultProgressBar(ProgressBar):
2013-05-27 13:06:28 +00:00
"""
progressbar with predefined parameters
"""
2013-05-27 13:06:28 +00:00
def __init__(self, maxval, caption=''):
widgets = [caption, Bar('>'), ' ', ETA(), ' ', ReverseBar('<')]
super(DefaultProgressBar, self).__init__(
widgets=widgets, maxval=maxval)
2013-05-27 13:06:28 +00:00
def progress(gen, caption='', pb_class=DefaultProgressBar):
"""
Make a generator that displays a progress bar from
generator gen, set caption and choose the class to
use (DefaultProgressBar by default)
Generator should have __len__ method returning it's
size.
Progress bar class constructor should take two
parameters:
* generator size
* progress bar caption.
It also should have start, update and finish methods.
"""
2013-07-03 11:25:00 +00:00
pbar = None
if len(gen):
pbar = pb_class(len(gen), caption).start() if pb_class else None
2013-05-27 13:06:28 +00:00
i = 0
for elem in gen:
if pbar:
pbar.update(i)
i += 1
yield(elem)
if pbar:
pbar.finish()
2013-07-16 10:56:26 +00:00
StepperInfo = namedtuple(
'StepperInfo',
'loop_count,steps,loadscheme,duration,ammo_count'
)
class StepperStatus(object):
2013-07-16 12:18:46 +00:00
'''
Raises StopIteration when limits are reached.
'''
def __init__(self):
2013-07-16 10:56:26 +00:00
self.log = logging.getLogger(__name__)
self.info = {
'loop_count': None,
'steps': None,
'loadscheme': None,
'duration': None,
'ammo_count': None,
}
2013-07-16 12:08:23 +00:00
self.loop_limit = None
self.ammo_limit = None
2013-07-16 10:56:26 +00:00
def publish(self, key, value):
if key not in self.info:
raise RuntimeError(
"Tryed to publish to a non-existent key: %s" % key)
2013-07-16 11:46:19 +00:00
self.log.debug('Published %s to %s', value, key)
2013-07-16 10:56:26 +00:00
self.info[key] = value
2013-07-16 12:02:56 +00:00
@property
def ammo_count(self):
#return self._ammo_count
return self.info['ammo_count']
@ammo_count.setter
def ammo_count(self, value):
#self._ammo_count = value
self.info['ammo_count'] = value
2013-07-16 12:08:23 +00:00
if self.ammo_limit and value > self.ammo_limit:
raise StopIteration
2013-07-16 12:02:56 +00:00
@property
def loop_count(self):
#return self._loop_count
return self.info['loop_count']
@loop_count.setter
def loop_count(self, value):
#self._loop_count = value
self.info['loop_count'] = value
2013-07-16 12:08:23 +00:00
if self.loop_limit and value > self.loop_limit:
raise StopIteration
2013-07-16 12:02:56 +00:00
2013-07-16 11:46:19 +00:00
def get_info(self):
2013-07-16 10:56:26 +00:00
for key in self.info:
if self.info[key] is None:
raise RuntimeError(
"Information for %s is not published yet." % key)
return StepperInfo(**self.info)
STATUS = StepperStatus()