def auto_collect_data(self): try: # # 这里同时启动多个线程,会有问题,后面解决 # # 1. 开始采集kpi数据 # kpi_thread = GetKpiDataThread(self.KPI_THREAD_ID, config.test_package_name) # kpi_thread.start() # # # 2. 开始采集内存数据 # memory_thread = GetMemoryDataThread(self.MEMORY_THREAD_ID) # memory_thread.start() # # 3. 开始采集cpu数据 # cpu_thread = GetCpuDataThread(self.CPU_THREAD_ID) # cpu_thread.start() # 4. 开始采集帧率数据 fps_thread = GetFpsDataThread(self.FPS_THREAD_ID, config.test_package_name) fps_thread.start() fps_thread.join() # # # 5. 开始采集流量数据 # flow_thread = GetFlowDataThread(self.FLOW_THREAD_ID, config.test_package_name) # flow_thread.start() LogUtil.log_i('All thread worked!!') except Exception as e: LogUtil.log_e('collect data failure ' + e.message)
def auto_silent_collect_process(): try: # 创建进程池来执行进程 common_data_list.append([package_name, version_code, '']) __save_package_version() # result = None pool = multiprocessing.Pool(processes=2) # result = pool.apply_async(run_monkey, (config.test_package_name(), RUN_MONKEY_COUNT,)) result = None for i in range(len(SILENT_ARRAY)): result = pool.apply_async(get_silent_data, (SILENT_ARRAY[i], )) pool.close() pool.join() if result.successful(): LogUtil.log_i('excute silent success') LogUtil.log_i('All silent process worked!!') except Exception as e: LogUtil.log_e('collect data failure ' + e.message)
def auto_collect_data_process(): try: # 获取测试应用的包名和版本号,存到数据库中 version_code = AdbUtil.get_verson(package_name) common_data_list.append([package_name, version_code, '']) __save_package_version() # 创建进程池来执行进程 # result = None pool = multiprocessing.Pool(processes=6) # result = pool.apply_async(run_monkey, (config.test_package_name(), RUN_MONKEY_COUNT,)) result = None for i in range(len(METHOD_ARRAY)): result = pool.apply_async(get_data, (METHOD_ARRAY[i], )) pool.close() pool.join() if result.successful(): LogUtil.log_i('excute success') LogUtil.log_i('All process worked!!') except Exception as e: LogUtil.log_e('collect data failure ' + e.message)
def run(self): exec_count = 0 last_page_name = '' last_memory_data = 0 try: while True: LogUtil.log_i('Inspect memory') if exec_count > config.collect_data_count: break memory_data = int( AndroidUtil.get_memory_data(self.package_name)) #当前采集到的数据 now_page_name = AndroidUtil.get_cur_activity() # 目前暂时粗略的计算增量,当页面不一样时,计算增量 if now_page_name != last_page_name: memory_increase = memory_data - last_memory_data if memory_increase < 0: # 对于发生GC的情况,内存增量可能是负值, 暂时先不做处理 pass GetMemoryDataThread.memory_datas.append( [now_page_name, last_page_name, memory_increase]) last_page_name = now_page_name else: last_memory_data = memory_data exec_count += 1 continue # 内存增量大于某个值就认为是有问题 if memory_increase >= 10 * 1024: AdbUtil.screenshot(self.pic_name) LogUtil.log_i('Inspect memory 12') LogUtil.log_i('Inspect memory 13') # 设定多久采集一次数据 time.sleep(config.collect_data_interval) exec_count += 1 except Exception as e: LogUtil.log_e('get cpu error' + e.message)
def get_kpi_data(package_name, pic_name='kpi'): # 处理异常的kpi数据,当跳转时间大于3s(暂定) def handle_error_data(cost_time): if cost_time != '' and cost_time is not None: cost_time_value = handle_cost_time(cost_time) if cost_time_value > 3000: AdbUtil.screenshot(pic_name) # 因为从日志中得到的值都是 +345ms 或者 +1s234ms def handle_cost_time(cost_time): s_data = 0 ms_data = 0 s_result = re.findall(r'\ds', cost_time) if len(s_result) > 0: s_data = int(s_result[0].split('s')[0]) * 1000 ms_result = re.findall(r'\d\d\dms', cost_time) if len(ms_result) > 0: ms_data = int(ms_result[0].split('ms')[0]) return s_data + ms_data # 记录起始时间 global results start_time = time.mktime(time.localtime()) cmd = 'adb logcat -c && adb logcat -v time -s ActivityManager | findStr %s' % package_name try: results = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) except Exception as e: LogUtil.log_e('get kpi failure ' + e.message) # 这里的逻辑是采集一定时间的数据之后,结束进程 jump_page = '' cost_time = '' now_page_name = '' # 采集数据的次数 get_count = 0 while True: LogUtil.log_i('get kpi data') if get_count > config.collect_data_count: if results.poll() is None: print 'results.terminate()' results.stdout.close() break # 2.读取内容,并分析 data = results.stdout.readline() print data # 处理读取到的String if data is not None: if 'Displayed' in data: # 1. 获取跳转页面的名称及时间,过滤 Displayed result = data.split('Displayed') result = result[1].strip().split(':') if len(result) < 1: jump_page = 'unknow' cost_time = 0 else: jump_page = result[0].split('/')[1] cost_time = result[1] # 2. 获取从哪个页面跳转 if 'Moving to STOPPED:' in data: now_page = data.split('Moving to STOPPED:') now_page = now_page[1].strip().split(' ') if len(now_page) > 3: now_page_name = now_page[2].split('/')[1] else: now_page_name = 'unknow' # 将结果保存到数组中 if now_page_name is not None and jump_page is not None and cost_time is not None: PerformanceControl.kpi_datas.append([now_page_name, jump_page, handle_cost_time(cost_time)]) handle_error_data(cost_time) get_count += 1
def run(self): # 处理异常的kpi数据,当跳转时间大于3s(暂定) def handle_error_data(): if self.cost_time != '' and self.cost_time is not None: cost_time_value = handle_cost_time(self.cost_time) if cost_time_value > 3000: AdbUtil.screenshot(self.pic_name) GetKpiDataThread.kpi_error_datas.append([self.now_page_name, self.jump_page, self.cost_time, self.pic_name]) # 因为从日志中得到的值都是 +345ms 或者 +1s234ms def handle_cost_time(cost_time): s_data = 0 ms_data = 0 s_result = re.findall(r'\ds', cost_time) if len(s_result) > 0: s_data = int(s_result[0].split('s')[0]) * 1000 ms_result = re.findall(r'\d\d\dms', cost_time) if len(ms_result) > 0: ms_data = int(ms_result[0].split('ms')[0]) return s_data + ms_data # 记录起始时间 global results start_time = time.mktime(time.localtime()) cmd = 'adb logcat -c && adb logcat -v time -s ActivityManager | findStr %s' % self.package_name try: results = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) except Exception as e: log.log_e('get kpi failure ' + e.message) # 这里的逻辑是采集一定时间的数据之后,结束进程 while True: log.log_i('get kpi data') # 1. 根据时间判断是否结束 now_time = time.mktime(time.localtime()) if now_time - start_time > config.collect_data_time: if results.poll() is None: print 'results.terminate()' results.stdout.close() break # 2.读取内容,并分析 data = results.stdout.readline() print data # 处理读取到的String if data is not None: if 'Displayed' in data: # 1. 获取跳转页面的名称及时间,过滤 Displayed result = data.split('Displayed') result = result[1].strip().split(':') if len(result) < 1: self.jump_page = 'unknow' self.cost_time = 0 else: self.jump_page = result[0].split('/')[1] self.cost_time = result[1] # 2. 获取从哪个页面跳转 if 'Moving to STOPPED:' in data: now_page = data.split('Moving to STOPPED:') now_page = now_page[1].strip().split(' ') if len(now_page) > 3: self.now_page_name = now_page[2].split('/')[1] else: self.now_page_name = 'unknow' # 将结果保存到数组中 if self.now_page_name is not None and self.jump_page is not None and self.cost_time is not None: GetKpiDataThread.kpi_datas.append([self.now_page_name, self.jump_page, self.cost_time]) handle_error_data() GetKpiDataThread.task_finish = True