yandex-tank/Tank/stepper/missile.py

125 lines
3.9 KiB
Python
Raw Normal View History

2013-05-27 13:06:28 +00:00
'''
Missile object and generators
2013-07-16 12:02:56 +00:00
You should update STATUS.ammo_count and STATUS.loop_count in your custom generators!
2013-05-27 13:06:28 +00:00
'''
from itertools import cycle
2013-07-15 11:51:50 +00:00
from module_exceptions import AmmoFileError
2013-07-16 11:46:19 +00:00
from info import STATUS
2013-05-27 13:06:28 +00:00
class HttpAmmo(object):
'''
Represents HTTP missile
'''
2013-05-27 13:06:28 +00:00
def __init__(self, uri, headers, method='GET', http_ver='1.1'):
self.method = method
self.uri = uri
self.proto = 'HTTP/%s' % http_ver
self.headers = headers
self.body = []
def to_s(self):
2013-06-25 13:18:36 +00:00
if self.headers:
headers = '\r\n'.join(self.headers) + '\r\n'
2013-06-26 12:52:03 +00:00
else:
headers = ''
2013-06-25 13:18:36 +00:00
return "%s %s %s\r\n%s\r\n" % (self.method, self.uri, self.proto, headers)
2013-05-27 13:06:28 +00:00
class SimpleGenerator(object):
'''
Generates ammo based on a given sample.
'''
2013-05-27 13:06:28 +00:00
def __init__(self, missile_sample):
'''
Missile sample is any object that has to_s method which
returns its string representation.
'''
2013-05-27 13:06:28 +00:00
self.missiles = cycle([(missile_sample.to_s(), None)])
def __iter__(self):
for m in self.missiles:
self.loops += 1
2013-07-16 12:02:56 +00:00
STATUS.loop_count = self.loops
STATUS.ammo_count = self.loops # loops equals ammo count
2013-05-27 13:06:28 +00:00
yield m
def loop_count(self):
return self.loops
class UriStyleGenerator(SimpleGenerator):
'''
Generates GET ammo based on given URI list.
'''
2013-05-27 13:06:28 +00:00
def __init__(self, uris, headers, loop_limit=0, http_ver='1.1'):
'''
uris - a list of URIs as strings.
'''
2013-07-16 11:46:19 +00:00
self.ammo_count = 0
self.loop_count = 0
2013-05-27 13:06:28 +00:00
self.loop_limit = loop_limit
self.uri_count = len(uris)
self.missiles = cycle(
[(HttpAmmo(uri, headers, http_ver=http_ver).to_s(), None) for uri in uris])
2013-05-27 13:06:28 +00:00
def __iter__(self):
for m in self.missiles:
2013-07-16 11:46:19 +00:00
self.ammo_count += 1
2013-07-16 12:02:56 +00:00
STATUS.ammo_count = self.ammo_count
2013-07-16 11:46:19 +00:00
self.update_loop_count()
if self.loop_limit and self.loop_count > self.loop_limit:
2013-05-27 13:06:28 +00:00
raise StopIteration
else:
yield m
2013-07-16 11:46:19 +00:00
def update_loop_count(self):
loop_count = self.ammo_count / self.uri_count
if self.loop_count != loop_count:
2013-07-16 12:02:56 +00:00
STATUS.loop_count = loop_count
2013-07-16 11:46:19 +00:00
self.loop_count = loop_count
2013-05-27 13:06:28 +00:00
class AmmoFileReader(SimpleGenerator):
2013-05-27 13:06:28 +00:00
'''Read missiles from ammo file'''
2013-05-27 13:06:28 +00:00
def __init__(self, filename, loop_limit=0):
self.filename = filename
self.loops = 0
self.loop_limit = loop_limit
def __iter__(self):
with open(self.filename, 'rb') as ammo_file:
2013-07-16 11:46:19 +00:00
ammo_count = 0
2013-05-27 13:06:28 +00:00
chunk_header = ammo_file.readline()
while chunk_header:
if chunk_header.strip('\r\n') is not '':
try:
fields = chunk_header.split()
chunk_size = int(fields[0])
marker = fields[1] if len(fields) > 1 else None
missile = ammo_file.read(chunk_size)
if len(missile) < chunk_size:
raise AmmoFileError(
"Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size))
2013-07-16 11:46:19 +00:00
ammo_count += 1
2013-07-16 12:02:56 +00:00
STATUS.ammo_count = ammo_count
yield (missile, marker)
except (IndexError, ValueError):
raise AmmoFileError(
"Error while reading ammo file. Position: %s, header: '%s'" % (ammo_file.tell(), chunk_header))
2013-05-27 13:06:28 +00:00
chunk_header = ammo_file.readline()
2013-07-15 16:00:27 +00:00
if not chunk_header and (self.loops < self.loop_limit or self.loop_limit == 0):
self.loops += 1
2013-07-16 12:02:56 +00:00
STATUS.loop_count = self.loops
2013-07-15 16:00:27 +00:00
ammo_file.seek(0)
2013-07-15 16:02:54 +00:00
chunk_header = ammo_file.readline()