def run(self, str_spawn_id=None): self.spawn_id = str_spawn_id # Cycle through transactions for obj_transaction_item in self.test_transactions: # Send transaction start message self.sender.send('transactionStart', { 'hostName': utils.get_ip_address(), 'spawnID': self.spawn_id, 'testName': self.name, 'iteration': self.iteration, 'transactionName': obj_transaction_item['name'], 'startTimestamp': utils.get_timestamp() }) # Run the transaction self.settings,\ self.thread_variables = \ obj_transaction_item['class'].run(self.spawn_id, self.iteration, self.settings, self.thread_variables) # Send transaction end message self.sender.send('transactionEnd', { 'hostName': utils.get_ip_address(), 'spawnID': self.spawn_id, 'testName': self.name, 'iteration': self.iteration, 'transactionName': obj_transaction_item['name'], 'endTimestamp': utils.get_timestamp() }) self.iteration += 1 # Print line if in Debug mode if self.debug: print('ID: ' + str(self.spawn_id) + ' Timestamp: ' + utils.get_timestamp())
def __run(self): click.secho(utils.logify('Starting Sysmonitor... ', 'Spawner'), nl=False) self.sysmonitor.start() click.secho('DONE', fg='green', bold=True) self.scenario.configure() self.scenario.navigations_definition() self.scenario.scenario_start = utils.get_timestamp(False) self.scenario.scenario_duration =\ self.scenario.get_scenario_duration() list_navigations = self.scenario.get_navigation_names() # Update sender spawn message elapsed time self.sender_spawn_elapsed_time += self.elapsed_time # Write beginning message click.secho(utils.logify('Running scenario...', 'Spawner'), fg='green') while self.elapsed_time <= self.scenario.scenario_duration and self.armed: # Get elapsed time time_elapsed = utils.get_timestamp(False) - self.scenario.scenario_start self.elapsed_time = time_elapsed.total_seconds() # Cycle through navigations for str_navigation_name in list_navigations: # Get planned and current spawn number self.scenario.navigations[str_navigation_name]['planned_spawns'] =\ self.scenario.get_planned_spawns(str_navigation_name, self.elapsed_time) self.scenario.navigations[str_navigation_name]['current_spawns'] =\ len(self.scenario.navigations[str_navigation_name]['threads']) # Send spawn message, but only if is passed enough time from last sending if self.sender_spawn_elapsed_time >= self.sender_spawn_polling_interval: self.sender.send('spawns', { 'hostName': utils.get_ip_address(), 'timestamp': utils.get_timestamp(), 'navigationName': str_navigation_name, 'plannedSpawns': self.scenario.navigations[str_navigation_name]['planned_spawns'], 'runningSpawns': self.scenario.navigations[str_navigation_name]['current_spawns'] }) self.sender_spawn_elapsed_time = 0.0 int_spawns_difference = self.scenario.navigations[str_navigation_name][ 'current_spawns'] - self.scenario.navigations[ str_navigation_name]['planned_spawns'] # If there are less spawns than planned, add some spawns if int_spawns_difference < 0: for int_counter in range(0, -int_spawns_difference): str_navigation_path = self.scenario.get_navigation_path(str_navigation_name) str_id = utils.random_id(16) print(str_id) obj_spawn = Spawn(str_id, str_navigation_name, str_navigation_path, self.scenario_folder, self.server_address, self.port, self.scenario.settings) obj_spawn.start() self.scenario.navigations[str_navigation_name]['threads'].append(obj_spawn) # If there are more spawns than planned, start killing older spawns elif int_spawns_difference > 0: for int_counter in range(0, int_spawns_difference): obj_spawn = self.scenario.navigations[str_navigation_name]['threads'].pop(0) obj_spawn.terminate() obj_spawn.join() print('Killed one') self.__unarm() # Clean all the remaining threads click.secho(utils.logify('Cleaning up... ', 'Spawner'), nl=False) for str_navigation_name in list_navigations: for int_counter in range(0, len(self.scenario.navigations[str_navigation_name]['threads'])): obj_spawn = self.scenario.navigations[str_navigation_name]['threads'].pop(0) obj_spawn.terminate() obj_spawn.join() print('Killed one - Cleanup') click.secho('DONE', fg='green', bold=True) # Close Sysmonitor thread click.secho(utils.logify('Closing Sysmonitor... ', 'Spawner'), nl=False) self.sysmonitor.terminate() self.sysmonitor.join() click.secho('DONE', fg='green', bold=True) click.secho(utils.logify('Scenario gracefully completed!', 'Spawner'), fg='green', bold=True)