yandex-tank/yandextank/stepper/info.py

142 lines
4.5 KiB
Python
Raw Normal View History

2013-07-16 10:56:26 +00:00
import logging
import time
from collections import namedtuple
from sys import stdout
2016-08-11 14:26:25 +00:00
log = logging.getLogger(__name__)
2013-07-16 10:56:26 +00:00
StepperInfo = namedtuple(
2016-02-04 12:25:06 +00:00
'StepperInfo', 'loop_count,steps,loadscheme,duration,ammo_count,instances')
2013-07-16 10:56:26 +00:00
class StepperStatus(object):
2013-07-16 12:18:46 +00:00
'''
Raises StopIteration when limits are reached.
'''
def __init__(self):
2016-08-11 14:26:25 +00:00
self.core = None # dirty hack. StepperWrapper should pass core here.
2013-07-16 10:56:26 +00:00
self.info = {
'loop_count': 0,
2013-07-16 10:56:26 +00:00
'steps': None,
'loadscheme': None,
'duration': None,
'ammo_count': 0,
2013-08-29 14:46:27 +00:00
'instances': None,
2013-07-16 10:56:26 +00:00
}
self._ammo_count = 0
self._old_ammo_count = 0
self._loop_count = 0
2013-07-30 14:18:15 +00:00
self._af_position = None
self.af_size = None
2013-07-16 12:08:23 +00:00
self.loop_limit = None
self.ammo_limit = None
2013-07-25 14:30:21 +00:00
self.lp_len = None
2013-07-30 14:18:15 +00:00
self.lp_progress = 0
self.af_progress = 0
self._timer = time.time()
2013-07-30 11:47:54 +00:00
2013-07-16 10:56:26 +00:00
def publish(self, key, value):
if key not in self.info:
2016-07-03 19:42:48 +00:00
raise RuntimeError("Tried to publish to a non-existent key: %s" %
2016-02-04 12:25:06 +00:00
key)
2016-08-11 14:26:25 +00:00
log.debug('Published %s to %s', value, key)
2013-07-16 10:56:26 +00:00
self.info[key] = value
2013-07-30 14:18:15 +00:00
@property
def af_position(self):
return self._af_position
@af_position.setter
def af_position(self, value):
self._af_position = value
self.update_af_progress()
2013-07-16 12:02:56 +00:00
@property
def ammo_count(self):
return self._ammo_count
2013-07-16 12:02:56 +00:00
@ammo_count.setter
def ammo_count(self, value):
self._ammo_count = value
2013-07-30 14:18:15 +00:00
self.update_lp_progress()
2013-07-16 12:08:23 +00:00
if self.ammo_limit and value > self.ammo_limit:
2013-07-30 14:40:47 +00:00
print
2016-08-11 14:26:25 +00:00
log.info("Ammo limit reached: %s", self.ammo_limit)
2013-07-16 12:08:23 +00:00
raise StopIteration
2013-07-16 12:02:56 +00:00
def inc_ammo_count(self):
self.ammo_count += 1
2013-07-16 12:02:56 +00:00
@property
def loop_count(self):
return self._loop_count
2013-07-16 12:02:56 +00:00
@loop_count.setter
def loop_count(self, value):
self._loop_count = value
if self.loop_limit and value >= self.loop_limit:
print # do not overwrite status (go to new line)
2016-08-11 14:26:25 +00:00
log.info("Loop limit reached: %s", self.loop_limit)
2013-07-16 12:08:23 +00:00
raise StopIteration
2013-07-16 12:02:56 +00:00
def inc_loop_count(self):
self.loop_count += 1
2013-07-16 11:46:19 +00:00
def get_info(self):
self.info['ammo_count'] = self._ammo_count
self.info['loop_count'] = self._loop_count
2013-07-16 10:56:26 +00:00
for key in self.info:
if self.info[key] is None:
2016-02-04 12:25:06 +00:00
raise RuntimeError("Information for %s is not published yet." %
key)
2013-07-16 10:56:26 +00:00
return StepperInfo(**self.info)
2013-07-23 14:22:37 +00:00
def update_view(self):
ammo_generated = self._ammo_count - self._old_ammo_count
self._old_ammo_count = self._ammo_count
cur_time = time.time()
time_delta = cur_time - self._timer
self._timer = cur_time
2014-06-16 11:33:46 +00:00
if time_delta > 0:
2016-12-27 13:50:34 +00:00
stdout.write(
"AF: %3s%%, LP: %3s%%, loops: %10s, speed: %5s Krps\r" %
(self.af_progress, self.lp_progress, self.loop_count, int(
ammo_generated / time_delta / 1000.0)))
2014-06-16 11:33:46 +00:00
stdout.flush()
2016-08-11 14:26:25 +00:00
if self.core:
self.core.publish("stepper", "progress", self.lp_progress)
self.core.publish("stepper", "loop_count", self.loop_count)
2016-12-27 13:50:34 +00:00
self.core.publish("stepper", "speed", "%s Krps" %
int(ammo_generated / time_delta / 1000.0))
2013-07-30 14:18:15 +00:00
def update_af_progress(self):
if self.af_size and self.loop_limit and self.af_position is not None:
bytes_read = self.af_size * self.loop_count + self.af_position
total_bytes = self.af_size * self.loop_limit
2013-07-30 14:18:15 +00:00
progress = int(float(bytes_read) / float(total_bytes) * 100.0)
else:
progress = 100
if self.af_progress != progress:
self.af_progress = progress
self.update_view()
def update_lp_progress(self):
if self.ammo_limit or self.lp_len:
if self.ammo_limit:
if self.lp_len:
max_ammo = min(self.ammo_limit, self.lp_len)
else:
max_ammo = self.ammo_limit
2013-07-30 14:18:15 +00:00
else:
max_ammo = self.lp_len
progress = int(float(self.ammo_count) / float(max_ammo) * 100.0)
else:
progress = 100
if self.lp_progress != progress:
self.lp_progress = progress
self.update_view()
status = StepperStatus()