async def _run(self): verbose = self.args.verbose exception = self.args.exception if self.args.single_mode: single = get_scenario(self.args.single_mode) elif self.args.single_run: single = next_scenario() else: single = None self.count = 1 self.worker_start = _now() try: options = await self.setup() except FixtureError as e: self.results["SETUP_FAILED"] += 1 stop(why=e) return async with get_session(self.loop, self.console, verbose, self.statsd, **options) as session: get_context(session).args = self.args get_context(session).worker_id = self.wid try: await self.session_setup(session) except FixtureError as e: self.results["SESSION_SETUP_FAILED"] += 1 stop(why=e) return while self._may_run(): step_start = _now() get_context(session).step = self.count result = await self.step(self.count, session, scenario=single) if result == 1: self.results["OK"] += 1 self.results["MINUTE_OK"] += 1 elif result != 0: self.results["FAILED"] += 1 self.results["MINUTE_FAILED"] += 1 if exception: stop(why=result) if not is_stopped() and self._reached_tolerance(step_start): stop() cancellable_sleep.cancel_all() break self.count += 1 if self.args.delay > 0.0: await cancellable_sleep(self.args.delay) else: # forces a context switch await asyncio.sleep(0) await self.session_teardown(session)
def _process(self): set_timer() if self.args.processes > 1: signal.signal(signal.SIGINT, self._shutdown) signal.signal(signal.SIGTERM, self._shutdown) self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) if self.args.debug: self.console.print('**** RUNNING IN DEBUG MODE == SLOW ****') self.loop.set_debug(True) self._set_statsd() if self.args.original_pid == os.getpid(): self._tasks.append(self.ensure_future(self._send_workers_event(1))) if not self.args.quiet: fut = self._display_results(self.args.console_update) update = self.ensure_future(fut) display = self.ensure_future(self.console.display()) display = self.gather(update, display) self._tasks.append(display) workers = self.gather(*self._runner()) workers.add_done_callback(lambda fut: stop()) self._tasks.append(workers) try: self.loop.run_until_complete(self.gather(*self._tasks)) finally: self._kill_tasks() if self.statsd is not None: self.statsd.close() self.loop.close()
async def _run(self): verbose = self.args.verbose exception = self.args.exception if self.args.single_mode: single = get_scenario(self.args.single_mode) else: single = None self.count = 1 self.worker_start = _now() setup = get_fixture('setup') if setup is not None: try: options = await setup(self.wid, self.args) except Exception as e: self.console.print_error(e) stop() return if options is None: options = {} elif not isinstance(options, dict): self.console.print('The setup function needs to return a dict') stop() return else: options = {} ssetup = get_fixture('setup_session') steardown = get_fixture('teardown_session') async with Session(self.loop, self.console, verbose, self.statsd, **options) as session: session.args = self.args session.worker_id = self.wid if ssetup is not None: try: await ssetup(self.wid, session) except Exception as e: self.console.print_error(e) stop() return while self._may_run(): step_start = _now() session.step = self.count result = await self.step(self.count, session, scenario=single) if result == 1: self.results['OK'] += 1 self.results['MINUTE_OK'] += 1 elif result == -1: self.results['FAILED'] += 1 self.results['MINUTE_FAILED'] += 1 if exception: stop() if not is_stopped() and self._reached_tolerance(step_start): stop() cancellable_sleep.cancel_all() break self.count += 1 if self.args.delay > 0.: await cancellable_sleep(self.args.delay) else: # forces a context switch await asyncio.sleep(0) if steardown is not None: try: await steardown(self.wid, session) except Exception as e: # we can't stop the teardown process self.console.print_error(e)
def _stop(cb): if _duration_killer is not None: if not _duration_killer.done(): _duration_killer.cancel() stop()
def _shutdown(self, signal, frame): stop() self._kill_tasks() # send sigterms for proc in self._procs: proc.terminate()