def main(num_agents, rampup, interval, duration, tc_xml_filename, log_msgs, output_dir=None, test_name=None): runtime_stats = {} error_queue = [] interval = interval / 1000.0 # convert from millisecs to secs # create a load manager lm = LoadManager(num_agents, interval, rampup, log_msgs, runtime_stats, error_queue, output_dir, test_name) # load the test cases try: cases = xmlparse.load_xml_cases(tc_xml_filename) for req in cases: lm.add_req(req) except Exception as e: print('ERROR: can not parse testcase file: %s' % e) sys.exit(1) start_time = time.time() # start the load manager lm.setDaemon(True) lm.start() reporter = RuntimeReporter(duration, runtime_stats) while (time.time() < start_time + duration): refresh_rate = 0.5 time.sleep(refresh_rate) # when all agents are started, start displaying the progress bar and stats if lm.agents_started: elapsed_secs = time.time() - start_time reporter.refresh(elapsed_secs, refresh_rate) lm.stop() # wait until the result generator is finished if config.GENERATE_RESULTS: while lm.results_gen.is_alive(): time.sleep(0.1) print('Done.')
def main(num_agents, rampup, interval, duration, tc_xml_filename, log_msgs, output_dir=None, test_name=None): if not config.HTTP_DEBUG: # turn off stdout and stderr sys.stdout = open(os.devnull, 'w') sys.stderr = open(os.devnull, 'w') runtime_stats = {} error_queue = [] interval = interval / 1000.0 # convert from millisecs to secs if test_name: if output_dir: output_dir = output_dir + '/' + test_name # create a load manager lm = LoadManager( num_agents, interval, rampup, log_msgs, runtime_stats, error_queue, output_dir, test_name, ) # load the test cases try: cases = xmlparse.load_xml_cases(tc_xml_filename) for req in cases: lm.add_req(req) except Exception as e: print('ERROR: can not parse testcase file: %s' % e) sys.exit(1) start_time = time.time() # start the load manager lm.setDaemon(True) lm.start() # wait for the test duration to lapse time.sleep(duration) elapsed_secs = time.time() - start_time lm.stop() # wait until the result generator is finished while lm.results_gen.is_alive(): time.sleep(.10) ids = runtime_stats.keys() agg_count = sum([runtime_stats[id].count for id in ids]) # total req count agg_total_latency = sum([runtime_stats[id].total_latency for id in ids]) agg_error_count = sum([runtime_stats[id].error_count for id in ids]) total_bytes_received = sum([runtime_stats[id].total_bytes for id in ids]) avg_resp_time = agg_total_latency / agg_count # avg response time since start avg_throughput = float( agg_count) / elapsed_secs # avg throughput since start response_xml = """\ <results> <summary-results> <requests>%d</requests> <errors>%d</errors> <avg-response-time>%.3f</avg-response-time> <avg-throughput>%.2f</avg-throughput> <bytes-received>%i</bytes-received> </summary-results> """ % (agg_count, agg_error_count, avg_resp_time, avg_throughput, total_bytes_received) for id in runtime_stats: response_xml += """ <agent-results> <agent-num>%d</agent-num> <requests>%d</requests> <errors>%d</errors> <avg-response-time>%.3f<avg-response-time> <bytes-received>%i</bytes-received> </agent-results> """ % ( id + 1, runtime_stats[id].count, runtime_stats[id].error_count, runtime_stats[id].avg_latency, runtime_stats[id].total_bytes) response_xml += """ </results>""" sys.stdout = original_stdout # temporarily turn on stdout print(response_xml) sys.stdout = open(os.devnull, 'w') # turn off stdout again return response_xml
def case_start(case_id, num_agents, rampup, interval, duration, tc_xml_str, log_msgs, output_dir, test_name, f_success, f_error, job_log_file): def log_func(msg): with open(job_log_file, "a", encoding='utf-8') as f: f.write(str(msg)) # 刷新缓冲区 f.flush() runtime_stats = {} error_queue = [] interval = interval / 1000.0 # convert from millisecs to secs # create a load manager lm = LoadManager(num_agents, interval, rampup, log_msgs, runtime_stats, error_queue, output_dir, test_name) # load the test cases try: cases = xmlparse.load_xml_string_cases(tc_xml_str) for req in cases: lm.add_req(req) except Exception as e: log_func('ERROR: can not parse testcase: %s\n' % test_name) raise Exception('ERROR: can not parse testcase: %s' % test_name) start_time = time.time() # start the load manager lm.setDaemon(True) lm.start() # 维护,LoadManager,用于stop GL.gl_lm[case_id] = lm @async_my def monitor_lm(): reporter = RuntimeReporter(duration, runtime_stats, lm) while (time.time() < start_time + duration and lm.running): refresh_rate = 5 time.sleep(refresh_rate) # when all agents are started, start displaying the progress bar and stats if lm.agents_started: elapsed_secs = time.time() - start_time reporter.refresh(elapsed_secs, refresh_rate) lm.stop() GL.gl_lm.pop(case_id) # wait until the result generator is finished if config.GENERATE_RESULTS: while lm.results_gen.is_alive(): time.sleep(0.1) logger.info("%s done,report path:%s" % (case_id, lm.output_dir)) log_func("Test Case Done.\n") log_func("Report Path:%s.\n" % (lm.output_dir)) f_success(lm.output_dir) monitor_lm()