def beforeTest(self, test): """ before test run """ thread.clean_transaction_handlers() addr = test.address( ) # file path, package.subpackage.module, class.method test_file, module_fqn, class_method = addr test_fqn = test.id() # [package].module.class.method suite_name, case_name = test_fqn.split('.')[-2:] log.debug("Addr: %r", addr) log.debug("id: %r", test_fqn) if class_method is None: class_method = case_name description = test.shortDescription() self.controller.test_info = { "test_case": case_name, "suite_name": suite_name, "test_file": test_file, "test_fqn": test_fqn, "description": description, "module_fqn": module_fqn, "class_method": class_method } self.controller.beforeTest() # create template of current_sample
def cmdline_to_params(): parser = OptionParser() parser.add_option('', '--concurrency', action='store', type="int", default=1) parser.add_option('', '--iterations', action='store', type="int", default=sys.maxsize) parser.add_option('', '--ramp-up', action='store', type="float", default=0) parser.add_option('', '--steps', action='store', type="int", default=sys.maxsize) parser.add_option('', '--hold-for', action='store', type="float", default=0) parser.add_option('', '--result-file-template', action='store', type="str", default="result-%s.csv") parser.add_option('', '--verbose', action='store_true', default=False) parser.add_option('', "--version", action='store_true', default=False) opts, args = parser.parse_args() log.debug("%s %s", opts, args) if opts.version: print(VERSION) sys.exit(0) params = Params() params.concurrency = opts.concurrency params.ramp_up = opts.ramp_up params.steps = opts.steps params.iterations = opts.iterations params.hold_for = opts.hold_for params.report = opts.result_file_template params.tests = args params.worker_count = 1 # min(params.concurrency, multiprocessing.cpu_count()) params.verbose = opts.verbose return params
def _writer(self): while self._writing: if self._samples_queue.empty(): time.sleep(0.1) while not self._samples_queue.empty(): item = self._samples_queue.get(block=True) try: sample, test_count, success_count = item self._write_sample(sample, test_count, success_count) except BaseException as exc: log.debug("Processing sample failed: %s\n%s", str(exc), traceback.format_exc()) log.warning("Couldn't process sample, skipping") continue
def import_plugins(): path = os.environ.get(PLUGINS_PATH, None) if not path: log.debug('Plugins PATH not found, continue without plugins') return # add plugins path to PYTHONPATH sys.path.append(path) package = Path(path).resolve().name log.info(f'Plugins package {package}') # modules listing in the root package for (_, module_name, _) in iter_modules([path]): log.info(f'Importing module {module_name}') import_module(module_name)
def startTest(self, event): """ before test run """ test = event.test thread.clean_transaction_handlers() test_fqn = test.id() # [package].module.class.method suite_name, case_name = test_fqn.split('.')[-2:] log.debug("id: %r", test_fqn) class_method = case_name description = test.shortDescription() self.controller.test_info = { "test_case": case_name, "suite_name": suite_name, "test_fqn": test_fqn, "description": description, "class_method": class_method } self.controller.startTest()
def _process_apiritif_samples(self, sample): samples_processed = 0 recording = apiritif.recorder.pop_events(from_ts=self.start_time, to_ts=self.end_time) if not recording: return samples_processed try: samples = self.apiritif_extractor.parse_recording( recording, sample) except BaseException as exc: log.debug("Couldn't parse recording: %s", traceback.format_exc()) log.warning("Couldn't parse recording: %s", exc) samples = [] for sample in samples: samples_processed += 1 self._process_sample(sample) return samples_processed
def _concurrency_slicer(self, ): total_concurrency = 0 inc = self.params.concurrency / float(self.params.worker_count) assert inc >= 1 for idx in range(0, self.params.worker_count): progress = (idx + 1) * inc conc = int(round(progress - total_concurrency)) assert conc > 0 log.debug("Idx: %s, concurrency: %s", idx, conc) params = copy.deepcopy(self.params) params.worker_index = idx params.thread_index = total_concurrency # for subprocess it's index of its first thread params.concurrency = conc params.report = self.params.report % idx params.worker_count = self.params.worker_count total_concurrency += conc yield params assert total_concurrency == self.params.concurrency
def beforeTest(self, test): """ before test run """ addr = test.address( ) # file path, package.subpackage.module, class.method test_file, module_fqn, class_method = addr test_fqn = test.id() # [package].module.class.method suite_name, case_name = test_fqn.split('.')[-2:] log.debug("Addr: %r", addr) log.debug("id: %r", test_fqn) if class_method is None: class_method = case_name self.current_sample = Sample(test_case=case_name, test_suite=suite_name, start_time=time.time(), status="SKIPPED") self.current_sample.extras.update({ "file": test_file, "full_name": test_fqn, "description": test.shortDescription() }) module_fqn_parts = module_fqn.split('.') for item in module_fqn_parts[:-1]: self.current_sample.path.append(PathComponent("package", item)) self.current_sample.path.append( PathComponent("module", module_fqn_parts[-1])) if "." in class_method: # TestClass.test_method class_name, method_name = class_method.split('.')[:2] self.current_sample.path.extend([ PathComponent("class", class_name), PathComponent("method", method_name) ]) else: # test_func self.current_sample.path.append(PathComponent( "func", class_method)) log.debug("Test method path: %r", self.current_sample.path) self.test_count += 1
def run_nose(self, params): """ :type params: Params """ thread.set_index(params.thread_index) log.debug("[%s] Starting nose iterations: %s", params.worker_index, params) assert isinstance(params.tests, list) # argv.extend(['--with-apiritif', '--nocapture', '--exe', '--nologcapture']) end_time = self.params.ramp_up + self.params.hold_for end_time += time.time() if end_time else 0 time.sleep(params.delay) plugin = ApiritifPlugin() store.writer.concurrency += 1 config = Config(env=os.environ, files=all_config_files(), plugins=DefaultPluginManager()) config.plugins.addPlugins(extraplugins=[plugin]) config.testNames = params.tests config.verbosity = 3 if params.verbose else 0 if params.verbose: config.stream = open( os.devnull, "w") # FIXME: use "with", allow writing to file/log iteration = 0 try: while True: log.debug("Starting iteration:: index=%d,start_time=%.3f", iteration, time.time()) thread.set_iteration(iteration) ApiritifTestProgram(config=config) log.debug("Finishing iteration:: index=%d,end_time=%.3f", iteration, time.time()) iteration += 1 # reasons to stop if plugin.stop_reason: log.debug("[%s] finished prematurely: %s", params.worker_index, plugin.stop_reason) elif 0 < params.iterations <= iteration: log.debug("[%s] iteration limit reached: %s", params.worker_index, params.iterations) elif 0 < end_time <= time.time(): log.debug("[%s] duration limit reached: %s", params.worker_index, params.hold_for) else: continue # continue if no one is faced break finally: store.writer.concurrency -= 1 if params.verbose: config.stream.close()
def run_nose(self, params): """ :type params: Params """ if not params.tests: raise RuntimeError("Nothing to test.") thread.set_index(params.thread_index) log.debug("[%s] Starting nose2 iterations: %s", params.worker_index, params) assert isinstance(params.tests, list) # argv.extend(['--with-apiritif', '--nocapture', '--exe', '--nologcapture']) end_time = self.params.ramp_up + self.params.hold_for end_time += time.time() if end_time else 0 time.sleep(params.delay) store.writer.concurrency += 1 config = {"tests": params.tests} if params.verbose: config["verbosity"] = 3 iteration = 0 handlers = ActionHandlerFactory.create_all() log.debug(f'Action handlers created {handlers}') thread.put_into_thread_store(action_handlers=handlers) for handler in handlers: handler.startup() try: while not graceful(): log.debug("Starting iteration:: index=%d,start_time=%.3f", iteration, time.time()) thread.set_iteration(iteration) session = ApiritifSession() config["session"] = session ApiritifTestProgram(config=config) log.debug("Finishing iteration:: index=%d,end_time=%.3f", iteration, time.time()) iteration += 1 # reasons to stop if session.stop_reason: if "Nothing to test." in session.stop_reason: raise RuntimeError("Nothing to test.") elif session.stop_reason.startswith( NormalShutdown.__name__): log.info(session.stop_reason) else: raise RuntimeError( f"Unknown stop_reason: {session.stop_reason}") elif 0 < params.iterations <= iteration: log.debug("[%s] iteration limit reached: %s", params.worker_index, params.iterations) elif 0 < end_time <= time.time(): log.debug("[%s] duration limit reached: %s", params.worker_index, params.hold_for) else: continue # continue if no one is faced break finally: store.writer.concurrency -= 1 for handler in handlers: handler.finalize()