Best Python code snippet using Airtest
profile_recorder.py
Source:profile_recorder.py
...49 import traceback50 traceback.print_exc()51 return [], [], [], None52 return method_object.kp_sch, method_object.kp_src, method_object.good, result53 def get_and_plot_keypoints(self, method_name, plot=False):54 """è·å并ä¸ç»å¶åºç¹å¾ç¹å¹é
ç»æ."""55 if method_name not in self.method_object_dict.keys():56 print("'%s' is not in MATCHING_METHODS" % method_name)57 return None58 kp_sch, kp_src, good, result = self._get_result(method_name)59 if not plot or result is None:60 return kp_sch, kp_src, good, result61 else:62 im_search, im_source = deepcopy(self.im_search), deepcopy(self.im_source)63 # ç»å¶ç¹å¾ç¹è¯å«æ
åµãåºäºç¹å¾çå¾åå¹é
ç»æ:64 h_sch, w_sch = im_search.shape[:2]65 h_src, w_src = im_source.shape[:2]66 # init the plot image:67 plot_img = np.zeros([max(h_sch, h_src), w_sch + w_src, 3], np.uint8)68 plot_img[:h_sch, :w_sch, :] = im_search69 plot_img[:h_src, w_sch:, :] = im_source70 # plot good matche points:71 for m in good:72 color = tuple([int(random() * 255) for _ in range(3)]) # éæºé¢è²ç»çº¿73 cv2.line(plot_img, (int(kp_sch[m.queryIdx].pt[0]), int(kp_sch[m.queryIdx].pt[1])), (int(kp_src[m.trainIdx].pt[0] + w_sch), int(kp_src[m.trainIdx].pt[1])), color)74 # plot search_image75 for kp in kp_sch:76 color = tuple([int(random() * 255) for _ in range(3)]) # éæºé¢è²ç»ç¹77 pos = (int(kp.pt[0]), int(kp.pt[1]))78 mark_point(im_search, pos, circle=False, color=color, radius=5)79 # plot source_image80 for kp in kp_src:81 color = tuple([int(random() * 255) for _ in range(3)]) # éæºé¢è²ç»ç¹82 pos = (int(kp.pt[0]), int(kp.pt[1]))83 mark_point(im_source, pos, circle=False, color=color, radius=10)84 from airtest.aircv import show85 show(plot_img)86 show(im_search)87 show(im_source)88class RecordThread(threading.Thread):89 """è®°å½CPUåå
åæ°æ®çthread."""90 def __init__(self, interval=0.1):91 super(RecordThread, self).__init__()92 self.pid = os.getpid()93 self.interval = interval94 self.cpu_num = psutil.cpu_count()95 self.process = psutil.Process(self.pid)96 self.profile_data = []97 self.stop_flag = False98 def set_interval(self, interval):99 """设置æ°æ®ééé´é."""100 self.interval = interval101 def run(self):102 """å¼å§çº¿ç¨."""103 while not self.stop_flag:104 timestamp = time.time()105 cpu_percent = self.process.cpu_percent() / self.cpu_num106 # mem_percent = mem = self.process.memory_percent()107 mem_info = dict(self.process.memory_info()._asdict())108 mem_gb_num = mem_info.get('rss', 0) / 1024 / 1024109 # è®°å½ç±»åé110 self.profile_data.append({"mem_gb_num": mem_gb_num, "cpu_percent": cpu_percent, "timestamp": timestamp})111 # è®°å½cpuåmem_gb_num112 time.sleep(self.interval)113 # print("--> mem_gb_num:", mem_gb_num)114class ProfileRecorder(object):115 """帮å©ç¨æ·è®°å½æ§è½æ°æ®."""116 def __init__(self, profile_interval=0.1):117 super(ProfileRecorder, self).__init__()118 self.record_thread = RecordThread()119 self.record_thread.set_interval(profile_interval)120 def load_images(self, search_file, source_file):121 """å è½½å¾
å¹é
å¾ç."""122 self.search_file, self.source_file = search_file, source_file123 self.im_search, self.im_source = imread(self.search_file), imread(self.source_file)124 # åå§å对象125 self.check_macthing_object = CheckKeypointResult(self.im_search, self.im_source)126 def profile_methods(self, method_list):127 """帮å©å½æ°æ§è¡æ¶è®°å½æ°æ®."""128 self.method_exec_info = []129 # å¼å§æ°æ®è®°å½è¿ç¨130 self.record_thread.stop_flag = False131 self.record_thread.start()132 for name in method_list:133 if name not in self.check_macthing_object.MATCHING_METHODS.keys():134 continue135 time.sleep(3) # çåºç»å¾ç©ºç½åº136 start_time = time.time() # è®°å½å¼å§æ¶é´137 print("--->>> start '%s' matching:\n" % name)138 kp_sch, kp_src, good, result = self.check_macthing_object.get_and_plot_keypoints(name) # æ ¹æ®æ¹æ³åç»å¶å¯¹åºçè¯å«ç»æ139 print("\n\n\n")140 end_time = time.time() # è®°å½ç»ææ¶é´141 time.sleep(3) # çåºç»å¾ç©ºç½åº142 # è®°å½æ¬æ¬¡å¹é
çç¸å
³æ°æ®143 ret_info = {144 "name": name,145 "start_time": start_time,146 "end_time": end_time,147 "result": result,148 "kp_sch": len(kp_sch),149 "kp_src": len(kp_src),150 "good": len(good)}151 self.method_exec_info.append(ret_info)152 self.record_thread.stop_flag = True...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!