前言python
启动速度是一项重要的应用性能指标。以手机输入法为例,用户每次尝试键入时,均会直观感知到输入法键盘的调起速度,若速度过慢则会频繁影响用户体验。
web
为了可以更准确地获取到键盘调起速度的具体数据,小编编写了基于Python视频及图像处理模块的评测脚本,下面以其中部分函数为例,为你们介绍一下实现思路和流程。
shell
键盘调起自动化
ruby
首先须要确认的是,在手机上执行输入法切换与键盘调起等操做的自动化命令——咱们能够在找出设备屏幕上的相应坐标后,经过如下adb命令进行实现:微信
def turn_up_swtich(self): # 切换到百度输入法 inputmethod_pkgnames_list = ["com.baidu.input/.ImeService", "com.sohu.inputmethod.sogou/.SogouIME"] command_swtichInput = "adb shell ime set " + inputmethod_pkgnames_list[0] os.system(command_swtichInput) # 点击输入框,尝试调起键盘 command_turnUp = "adb shell input swipe 500 500 500 500 50" os.system(command_turnUp) # 关闭百度输入法键盘 command_turnDown = "adb shell input swipe 660 750 660 750 50" os.system(command_turnDown) # 切换到搜狗输入法 command_swtichInput = "adb shell ime set " + inputmethod_pkgnames_list[1] os.system(command_swtichInput)
其后,咱们经过一个简单的多进程应用,将键盘调起的过程以视频形式记录。为便于后续的视频处理,须要在录制前开启手机“开发者选项”中的“指针位置”与“显示触摸操做”。
markdown
# 视频录制命令def start_screenrecord(self): command = "adb shell screenrecord /sdcard/Movies/ScreenCaptures/SDvideo_data.mp4 --time-limit 5 --size 720x1280" os.system(command)
# 搜狗输入法键盘调起def click_action(self): time.sleep(0.5) command_turnUp = "adb shell input swipe 500 500 500 500 50" os.system(command_turnUp)
# 多进程使键盘调起与视频录制同时进行def make_video(self, video_path, runNum): p1 = Process(target=self.start_screenrecord) p2 = Process(target=self.click_action) p1.start() p2.start() p2.join() p1.join() time.sleep(0.5) # 导出并保存视频 dest_path = video_path + "/" + str(runNum) + ".mp4" self.get_file("adb pull /sdcard/Movies/ScreenCaptures/SDvideo_data.mp4 ", dest_path) return dest_path
这样,经过一个“切换输入法”、“键盘调起并录屏”的循环,便可获得多个视频素材,以期在后续的视频处理、结果输出时,经过多个数据的均值来消弭偏差:
app
def run(self):# 启动带有输入框的测试app command_start = "adb shell am start -n com.sogou.inputtest/.MainActivity" os.system(command_start) for runNum in range(0,50): # 每次循环先切换到其余输入法,再进行搜狗输入法键盘调起录制 self.turn_up_swtich() dest_path = self.make_video(self.video_path, runNum) # 视频处理解析 self.parse(dest_path ,runNum) # 输出耗时数据 return self.turnUp_speed
视频处理解析
编辑器
下面给出完整方法,对其中判断环节的说明可见注释及后续内容:
ide
def parse(self, video_file, runNum): output_dict = {"-r": str(60)} evaluation_video = skvideo.io.vreader(video_file, outputdict=output_dict) num = 0 has_keydown = False self.is_turn_up_img = None for frame in evaluation_video: self.img_size_width = int(frame.shape[1]) self.img_size_height = int(frame.shape[0]) target_img = frame[self.target_pos] block_img = frame[self.block_pos] if self.is_key_down(block_img, num): has_keydown = True is_key_down_num = num if has_keydown: cv2.imwrite(".\\key\\cand\\turnup_" + str(runNum) + "\\" + str(num) + ".png", target_img) if self.is_turn_up(target_img, num): has_turn_up_num = num - 1 turnup_time = int(has_turn_up_num - is_key_down_num)*16.6 self.turnUp_speed.append(turnup_time) return num += 1
def is_key_down(self, block_img): # 识别色块处颜色变化 r, g, b = cv2.split(block_img) red_num = 0 for x in range(0, r.shape[0], 5): for y in range(0, r.shape[1], 5): if r[x, y] > 200 and g[x, y] < 100 and b[x, y] < 100: red_num += 1 total_num = r.shape[0] / 5 * r.shape[1] down_filter_num = total_num / 20 up_filter_num = total_num / 100 # 根据色块颜色(变红)判断发生按下 if self.key_state['is_key_down']: if red_num < up_filter_num: self.key_state['is_key_down'] = False else: if red_num > down_filter_num: self.key_state['is_key_down'] = True return True return False
同理,以“搜狗输入法标识彻底上升到相应位置”为键盘调起完成的标志,在键盘调起、标识逐渐上升的过程当中(以下图),对每帧画面中相应坐标处的颜色变化进行识别,以期获得此刻帧数:函数
# 搜狗标识颜色变化识别def is_turn_up(self, target_img): b, g, r = cv2.split(target_img) speed_num = 0 for x in range(0, r.shape[0], 5): for y in range(0, r.shape[1], 5): if b[x, y] < 200 and g[x, y] < 200 and r[x, y] < 200: speed_num += 1 if self.is_turn_up_img: if speed_num == self.is_turn_up_img: return True # 以标识图片颜色变化达到必定程度为限 if speed_num >= 2: self.is_turn_up_img = speed_num return False
最终,将本轮脚本执行得出的两个帧数间差值乘以每帧耗时(16.67ms)后写入结果列表,并在预设的循环次数完成后,取得列表中的数据均值,即是当前输入法键盘调起速度的评测结果了。
结语
搜狗测试微信号:Qa_xiaoming
搜狗测试QQ粉丝群:459645679
本文分享自微信公众号 - 搜狗测试(SogouQA)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。