基于机器学习的启动耗时自动化测试方案(二)

简介: 基于机器学习的启动耗时自动化测试方案(二)

阶段二


人工标注训练集数据


由于我们是通过图片分类算法来对启动各个阶段进行识别的,所以首先要定义启动的阶段都有哪些,这里我分为5个阶段:


  • 0_desk:桌面阶段
  • 1_start:点击icon图标的阶段
  • 2_splash:闪屏页出现的阶段
  • 3_loading:首页加载的阶段
  • 4_stable:首页渲染稳定的阶段


这五个阶段的图片如下:


image.png


由于应用还会有广告页、业务弹框、首页动态变化等,这些暂时先忽略,不影响整体的测试思路。


特征提取与描述子生成

这里选择SIFT特征,SIFT特征具有缩、旋转、光照不变性,同时对图像几何变形有一定程度的鲁棒性,使用Python OpenCV扩展模块中的SIFT特征提取接口,就可以提取图像的SIFT特征点与描述子。


词袋生成

词袋生成,是基于描述子数据的基础上,生成一系列的向量数据,最常见就是首先通过K-Means实现对描述子数据的聚类分析,一般会分成100个聚类、得到每个聚类的中心数据,就生成了100个词袋,根据每个描述子到这些聚类中心的距离,决定了它属于哪个聚类,这样就生成了它的直方图表示数据。


SVM分类训练与模型生成

使用SVM进行数据的分类训练,得到输出模型,这里通过sklearn的线性SVM实现了分类模型的训练与导出。

import cv2
import imutils
import numpy as np
import os
from sklearn.svm import LinearSVC
from sklearn.externals import joblib
from scipy.cluster.vq import *
from sklearn.preprocessing import StandardScaler
# Get the training classes names and store them in a list
train_path = "dataset/train/"
training_names = os.listdir(train_path)
# Get all the path to the images and save them in a list
# image_paths and the corresponding label in image_paths
image_paths = []
image_classes = []
class_id = 0
for training_name in training_names:
    dir = os.path.join(train_path, training_name)
    class_path = imutils.imlist(dir)
    image_paths += class_path
    image_classes += [class_id] * len(class_path)
    class_id += 1
# 创建SIFT特征提取器
sift = cv2.xfeatures2d.SIFT_create()
# 特征提取与描述子生成
des_list = []
for image_path in image_paths:
    im = cv2.imread(image_path)
    im = cv2.resize(im, (300, 300))
    kpts = sift.detect(im)
    kpts, des = sift.compute(im, kpts)
    des_list.append((image_path, des))
    print("image file path : ", image_path)
# 描述子向量
descriptors = des_list[0][1]
for image_path, descriptor in des_list[1:]:
    descriptors = np.vstack((descriptors, descriptor))
# 100 聚类 K-Means
k = 100
voc, variance = kmeans(descriptors, k, 1)
# 生成特征直方图
im_features = np.zeros((len(image_paths), k), "float32")
for i in range(len(image_paths)):
    words, distance = vq(des_list[i][1], voc)
    for w in words:
        im_features[i][w] += 1
# 实现动词词频与出现频率统计
nbr_occurences = np.sum((im_features > 0) * 1, axis=0)
idf = np.array(np.log((1.0 * len(image_paths) + 1) / (1.0 * nbr_occurences + 1)), 'float32')
# 尺度化
stdSlr = StandardScaler().fit(im_features)
im_features = stdSlr.transform(im_features)
# Train the Linear SVM
clf = LinearSVC()
clf.fit(im_features, np.array(image_classes))
# Save the SVM
print("training and save model...")
joblib.dump((clf, training_names, stdSlr, k, voc), "startup.pkl", compress=3)


预测验证

加载预先训练好的模型,使用模型在测试集上进行数据预测,测试结果表明,对于启动阶段的图像分类可以获得比较好的效果。


下面是预测方法的代码实现:

import cv2 as cvimport numpy as npfrom imutils import pathsfrom scipy.cluster.vq import *from sklearn.externals import joblib
def predict_image(image_path, pkl):    # Load the classifier, class names, scaler, number of clusters and vocabulary    clf, classes_names, stdSlr, k, voc = joblib.load("eleme.pkl")    # Create feature extraction and keypoint detector objects    sift = cv.xfeatures2d.SIFT_create()    # List where all the descriptors are stored    des_list = []    im = cv.imread(image_path, cv.IMREAD_GRAYSCALE)    im = cv.resize(im, (300, 300))    kpts = sift.detect(im)    kpts, des = sift.compute(im, kpts)    des_list.append((image_path, des))
    descriptors = des_list[0][1]    for image_path, descriptor in des_list[0:]:        descriptors = np.vstack((descriptors, descriptor))
    test_features = np.zeros((1, k), "float32")    words, distance = vq(des_list[0][1], voc)    for w in words:        test_features[0][w] += 1
    # Perform Tf-Idf vectorization    nbr_occurences = np.sum((test_features > 0) * 1, axis=0)    idf = np.array(np.log((1.0 + 1) / (1.0 * nbr_occurences + 1)), 'float32')
    # Scale the features    test_features = stdSlr.transform(test_features)
    # Perform the predictions    predictions = [classes_names[i] for i in clf.predict(test_features)]    return predictions


阶段三


采集新的启动视频

和阶段一采用的方式一样。


用模型进行预测

和阶段二预测验证的做法一样。


计算启动时间

根据预测结果,确定点击应用icon阶段的图片和首页渲染稳定之后的图片,获取两个图片直接的帧数差值,如果前面以60帧抽取图片,那么总耗时 = 帧数差值 * 1/60,具体计算这部分的代码实现如下:

from airtest.core.api import *from dingtalkchatbot.chatbot import DingtalkChatbotfrom poco.drivers.android.uiautomation import AndroidUiautomationPoco
webhook = 'https://oapi.dingtalk.com/robot/send?access_token='robot = DingtalkChatbot(webhook)
def calculate(package_name, apk_path, pkl, device_name, app_name, app_version):    sample = 'sample/screen.mp4'    test_path = "dataset/test/"    if not os.path.isdir('sample/'):        os.makedirs('sample/')    if not os.path.isdir(test_path):        os.makedirs(test_path)    try:        os.system("adb uninstall {}".format(package_name))
        os.system("adb install -r {}".format(apk_path))
        poco = AndroidUiautomationPoco()        poco.device.wake()
        time.sleep(2)
        poco(text='应用名').click()        poco(text='下一步').click()        poco(text='允许').click()        poco(text='允许').click()        poco(text='允许').click()
        os.system("adb shell am force-stop {}".format(package_name))
        subprocess.Popen("adb shell screenrecord  --time-limit 20 /sdcard/sample.mp4", shell=True,                         stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        poco(text="应用名").click()
        time.sleep(20)
        os.system("adb pull /sdcard/sample.mp4 {}".format(sample))        os.system("adb uninstall {}".format(package_name))
        os.system("ffmpeg -i {} -r 60 {}%d.jpeg".format(sample, test_path))        image_paths = []        class_path = list(paths.list_images(test_path))        image_paths += class_path        start = []        stable = []        for image_path in image_paths:            predictions = predict_image(image_path, pkl)            if predictions[0] == '1_start':                start += [str(image_path.split('/')[2]).split('.')[0]]            elif predictions[0] == '4_stable':                stable += [str(image_path.split('/')[2]).split('.')[0]]
        start_time = int(sorted(start)[0])        stable_time = int(sorted(stable)[0])        print("耗时:%.2f 秒" % ((stable_time - start_time) / 60))        robot.send_text(            msg="启动耗时自动化测试结果:\n被测设备:{}\n被测应用:{}\n被测版本:{}\n".format(device_name, app_name,                                                                   app_version) + "启动耗时:%.2f 秒" % (                        (stable_time - start_time) / 60),            is_at_all=True)    except:        shutil.rmtree(test_path)        if os.path.exists(sample):            os.remove(sample)
if __name__ == "__main__":    calculate("package_name", "app/app-release.apk", "startup.pkl", "小米MIX3", "应用名", "10.1.1")


持续集成

根据上面测试方法提供的参数,通过Jenkins配置任务,训练好模型,将以上三个阶段通过Python脚本的形式封装好,另外再配置好WebHook跟打包平台关联好,即可实现自动验证分析计算最新包的首屏加载耗时。


效果

通过人工录屏,然后用QuickTime分帧查看时间轴,计算出的首屏加载耗时跟这套方案得到的结果误差基本在100毫秒以内,但这个过程一次取数需要15分钟左右,而现在这套方案一次取数只需要3分钟左右,效率明显提升,还避免了不同人操作采集标准不一致的问题。


相关文章
|
11月前
|
人工智能 自然语言处理 算法
AI智能混剪视频大模型开发方案:从文字到视频的自动化生成·优雅草卓伊凡
AI智能混剪视频大模型开发方案:从文字到视频的自动化生成·优雅草卓伊凡
1472 0
AI智能混剪视频大模型开发方案:从文字到视频的自动化生成·优雅草卓伊凡
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。
|
10月前
|
机器学习/深度学习 Kubernetes 监控
Kubernetes 节点故障自愈方案:结合 Node Problem Detector 与自动化脚本
本文深入探讨了Kubernetes节点故障自愈方案,结合Node Problem Detector(NPD)与自动化脚本,提供技术细节、完整代码示例及实战验证。文章分析了硬件、系统和内核层面的典型故障场景,指出现有监控体系的局限性,并提出基于NPD的实时事件捕获与自动化诊断树的改进方案。通过深度集成NPD、设计自动化修复引擎以及展示内核死锁恢复的实战案例,文章详细说明了自愈流程的实现步骤与性能优势。此外,还提供了生产环境部署指南、高可用架构设计及安全防护措施,并展望了机器学习增强故障预测和混沌工程验证的进阶优化方向。全文约1.2万字,适合希望提升Kubernetes集群稳定性的技术人员阅读。
707 1
|
10月前
|
自然语言处理 算法 数据可视化
文本聚类效果差?5种主流算法性能测试帮你找到最佳方案
本文探讨了自然语言处理中句子嵌入的聚类技术,使用Billingsmoore数据集(925个英语句子)进行实验。通过生成句子嵌入向量并可视化分析,对比了K-Means、DBSCAN、HDBSCAN、凝聚型层次聚类和谱聚类等算法的表现。结果表明,K-Means适合已知聚类数量的场景,DBSCAN和HDBSCAN适用于未知聚类数量且存在异常值的情况,而谱聚类在句子嵌入领域表现不佳。最终建议根据数据特征和计算资源选择合适的算法以实现高质量聚类。
758 0
文本聚类效果差?5种主流算法性能测试帮你找到最佳方案
|
机器学习/深度学习 人工智能 运维
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
544 14
|
关系型数据库 Shell 网络安全
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
|
Kubernetes 持续交付 开发工具
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
875 2
|
Kubernetes 持续交付 开发工具
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
|
弹性计算 运维 安全
自动化AutoTalk第十五期:自动化场景-多账号自动化场景下的AK管理方案
自动化AutoTalk第十五期探讨了多账号自动化场景下的AK管理方案。主要介绍了通过阿里云的实例角色和STS Token减少AK暴露风险,避免硬编码AK带来的安全隐患。最佳实践包括定期轮转AK、使用临时Token、分环境管理凭据,以及利用ECS实例角色实现安全的跨账号资源操作,确保在多账号架构中提升自动化程序的安全性和管理效率。
382 7
|
监控 测试技术 定位技术
HTTP代理IP响应速度测试方案设计与指标体系
随着数字化发展,网络安全、隐私保护及内容访问自由成为核心需求。HTTP代理因其技术优势成为热门选择。本文介绍HTTP代理IP响应速度测试方案,包括基础性能、稳定性、地理位置、实际应用、安全性测试及监控指标,推荐测试工具,并提供测试结果评估标准。
378 2