2013-07-15 11:51:50 +00:00
|
|
|
from module_exceptions import StepperConfigurationError
|
2013-05-27 13:06:28 +00:00
|
|
|
import load_plan as lp
|
2013-06-28 13:45:26 +00:00
|
|
|
import instance_plan as ip
|
2013-05-27 13:06:28 +00:00
|
|
|
import missile
|
2013-07-15 13:38:50 +00:00
|
|
|
from mark import get_marker
|
2013-07-16 13:21:55 +00:00
|
|
|
import info
|
2013-09-05 12:52:35 +00:00
|
|
|
import logging
|
2013-05-27 13:06:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ComponentFactory():
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
rps_schedule=None,
|
|
|
|
http_ver='1.1',
|
|
|
|
ammo_file=None,
|
|
|
|
instances_schedule=None,
|
2013-09-03 14:08:48 +00:00
|
|
|
instances=1000,
|
2013-05-27 13:06:28 +00:00
|
|
|
loop_limit=0,
|
|
|
|
ammo_limit=0,
|
|
|
|
uris=None,
|
|
|
|
headers=None,
|
|
|
|
autocases=None,
|
2013-08-14 11:38:02 +00:00
|
|
|
ammo_type='phantom'
|
2013-05-27 13:06:28 +00:00
|
|
|
):
|
2013-09-05 12:52:35 +00:00
|
|
|
self.log = logging.getLogger(__name__)
|
2013-08-14 11:56:24 +00:00
|
|
|
generators = {
|
|
|
|
'phantom': missile.AmmoFileReader,
|
2013-08-14 13:38:43 +00:00
|
|
|
'slowlog': missile.SlowLogReader,
|
2013-08-26 11:57:27 +00:00
|
|
|
'line': missile.LineReader,
|
2013-09-05 12:09:18 +00:00
|
|
|
'uri': missile.UriReader,
|
2013-08-14 11:56:24 +00:00
|
|
|
}
|
|
|
|
if ammo_type in generators:
|
2013-09-05 12:52:35 +00:00
|
|
|
if ammo_type is 'phantom':
|
|
|
|
with open(ammo_file) as ammo:
|
|
|
|
if not ammo.next()[0].isdigit():
|
|
|
|
ammo_type = 'uri'
|
|
|
|
self.log.warning(
|
|
|
|
"Setting ammo_type 'uri' because ammo is not started with digit and you did non specify ammo format.")
|
|
|
|
else:
|
|
|
|
self.log.info("I believe ammo_type is 'phantom' cause you did not specify it.")
|
2013-08-14 11:56:24 +00:00
|
|
|
self.ammo_generator_class = generators[ammo_type]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'No such ammo type implemented: "%s"' % ammo_type)
|
2013-05-27 13:06:28 +00:00
|
|
|
self.rps_schedule = rps_schedule
|
|
|
|
self.http_ver = http_ver
|
|
|
|
self.ammo_file = ammo_file
|
|
|
|
self.instances_schedule = instances_schedule
|
2013-07-16 12:18:46 +00:00
|
|
|
loop_limit = int(loop_limit)
|
|
|
|
if loop_limit == -1: # -1 means infinite
|
2013-07-30 14:40:47 +00:00
|
|
|
loop_limit = None
|
2013-07-16 12:18:46 +00:00
|
|
|
ammo_limit = int(ammo_limit)
|
|
|
|
if ammo_limit == -1: # -1 means infinite
|
2013-07-30 14:40:47 +00:00
|
|
|
ammo_limit = None
|
|
|
|
if loop_limit is None and ammo_limit is None and instances_schedule:
|
2013-09-05 12:52:35 +00:00
|
|
|
# we should have only one loop if we have instance_schedule
|
|
|
|
loop_limit = 1
|
2013-07-16 13:21:55 +00:00
|
|
|
info.status.loop_limit = loop_limit
|
|
|
|
info.status.ammo_limit = ammo_limit
|
2013-09-03 14:08:48 +00:00
|
|
|
info.status.publish("instances", instances)
|
2013-05-27 13:06:28 +00:00
|
|
|
self.uris = uris
|
2013-08-30 15:52:39 +00:00
|
|
|
if self.uris and loop_limit:
|
|
|
|
info.status.ammo_limit = len(self.uris) * loop_limit
|
2013-05-27 13:06:28 +00:00
|
|
|
self.headers = headers
|
2013-07-15 13:38:50 +00:00
|
|
|
self.marker = get_marker(autocases)
|
2013-05-27 13:06:28 +00:00
|
|
|
|
|
|
|
def get_load_plan(self):
|
|
|
|
"""
|
|
|
|
return load plan (timestamps generator)
|
|
|
|
"""
|
|
|
|
if self.rps_schedule and self.instances_schedule:
|
2013-06-25 11:22:51 +00:00
|
|
|
raise StepperConfigurationError(
|
|
|
|
'Both rps and instances schedules specified. You must specify only one of them')
|
2013-05-27 13:06:28 +00:00
|
|
|
elif self.rps_schedule:
|
2013-07-16 13:21:55 +00:00
|
|
|
info.status.publish('loadscheme', self.rps_schedule)
|
2013-05-27 13:06:28 +00:00
|
|
|
return lp.create(self.rps_schedule)
|
|
|
|
elif self.instances_schedule:
|
2013-07-16 13:21:55 +00:00
|
|
|
info.status.publish('loadscheme', self.instances_schedule)
|
2013-06-28 13:45:26 +00:00
|
|
|
return ip.create(self.instances_schedule)
|
2013-05-27 13:06:28 +00:00
|
|
|
else:
|
2013-08-01 12:37:11 +00:00
|
|
|
self.instances_schedule = []
|
|
|
|
info.status.publish('loadscheme', self.instances_schedule)
|
|
|
|
return ip.create(self.instances_schedule)
|
2013-05-27 13:06:28 +00:00
|
|
|
|
|
|
|
def get_ammo_generator(self):
|
|
|
|
"""
|
|
|
|
return ammo generator
|
|
|
|
"""
|
|
|
|
if self.uris and self.ammo_file:
|
2013-06-25 11:22:51 +00:00
|
|
|
raise StepperConfigurationError(
|
|
|
|
'Both uris and ammo file specified. You must specify only one of them')
|
2013-07-15 13:38:50 +00:00
|
|
|
elif self.uris:
|
|
|
|
ammo_gen = missile.UriStyleGenerator(
|
|
|
|
self.uris,
|
|
|
|
self.headers,
|
|
|
|
http_ver=self.http_ver
|
|
|
|
)
|
|
|
|
elif self.ammo_file:
|
2013-09-05 12:52:35 +00:00
|
|
|
ammo_gen = self.ammo_generator_class(
|
|
|
|
self.ammo_file, headers=self.headers)
|
2013-05-27 13:06:28 +00:00
|
|
|
else:
|
2013-07-15 13:38:50 +00:00
|
|
|
raise StepperConfigurationError(
|
|
|
|
'Ammo not found. Specify uris or ammo file')
|
2013-07-16 12:18:46 +00:00
|
|
|
return ammo_gen
|
2013-05-27 13:06:28 +00:00
|
|
|
|
|
|
|
def get_marker(self):
|
2013-07-15 13:38:50 +00:00
|
|
|
return self.marker
|