mirror of
https://github.com/valitydev/yandex-tank.git
synced 2024-11-08 19:23:54 +00:00
57 lines
1.2 KiB
Python
57 lines
1.2 KiB
Python
'''
|
|
Utilities: parsers, converters, etc.
|
|
'''
|
|
import re
|
|
from itertools import islice
|
|
from module_exceptions import StepperConfigurationError
|
|
|
|
|
|
def take(number, iter):
|
|
return list(islice(iter, 0, number))
|
|
|
|
|
|
def parse_duration(duration):
|
|
'''
|
|
Parse duration string, such as '3h2m3s'
|
|
'''
|
|
_re_token = re.compile("(\d+)([dhms]?)")
|
|
|
|
def parse_token(time, multiplier):
|
|
multipliers = {
|
|
'h': 3600,
|
|
'm': 60,
|
|
's': 1,
|
|
}
|
|
if multiplier:
|
|
if multiplier in multipliers:
|
|
return int(time) * multipliers[multiplier]
|
|
else:
|
|
raise StepperConfigurationError(
|
|
'Failed to parse duration: %s' % duration)
|
|
else:
|
|
return int(time)
|
|
|
|
return sum(parse_token(*token) for token in _re_token.findall(duration))
|
|
|
|
|
|
class Limiter(object):
|
|
|
|
def __init__(self, gen, limit):
|
|
self.gen = islice(gen, limit)
|
|
|
|
def __len__(self):
|
|
return self.limit
|
|
|
|
def __iter__(self):
|
|
return (item for item in self.gen)
|
|
|
|
def loop_count(self):
|
|
return 0
|
|
|
|
|
|
def limiter(gen, limit):
|
|
if limit == 0:
|
|
return gen
|
|
else:
|
|
return Limiter(gen, limit)
|