iOS 测试 | iOS 自动化性能采集

简介: 前言对于iOS总体生态是比较封闭的,相比Android没有像adb这种可以查看内存、cpu的命令.在日常做性能测试,需要借助xcode中instruments查看内存、cpu等数据.

image.png
前言
对于iOS总体生态是比较封闭的,相比Android没有像adb这种可以查看内存、cpu的命令.在日常做性能测试,需要借助xcode中instruments查看内存、cpu等数据.

但是借助instruments比较麻烦、又不能提供命令行.在持续集成中,很难时时的监控app的性能指标.并且现在app发版一般是2周左右,留给做专项测试的时间更少了,那么做核心场景性能测试,肯定是来不及的.

所以需要借助一些自动化工具来减轻手工采集性能指标的工作量.

性能采集项
app中基本性能采集项,内存、cpu、fps、电量等,因为自动化采集中手机设备是插着电脑充电的,所以不能采集电量数据.

已有工具
instruments是官方提供的,不能做到自动化采集

腾讯gt,需要在app中集成sdk,有一定的接入成本

第三sdk,类似腾讯gt需要在app集成,可能会有数据泄漏风险

脚本开发
上述的已有工具都不满足,在持续集成中做到自动化采集性能数据,期望的性能测试工具有一下几点:

方便接入

可生成性能报告

可持续化

数据收集精准

所以基于这几点,需要自己开发一套性能采集脚本.

使用官方提供的api做性能采集
获取内存、cpu等

import

/**

  • 获取内存
    */
  • (NSString *)get_memory {
    int64_t memoryUsageInByte = 0;
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count);
    if(kernelReturn == KERN_SUCCESS) {

       memoryUsageInByte = (int64_t) vmInfo.phys_footprint;
       NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);

    } else {

       NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));

    }

    double mem = memoryUsageInByte / (1024.0 * 1024.0);
    NSString *memtostring ;
    memtostring = [NSString stringWithFormat:@"%.1lf",mem];

    return memtostring;
    }

/**

  • 获取cpu
    */
  • (NSString *) get_cpu{
    kern_return_t kr;
    task_info_data_t tinfo;
    mach_msg_type_number_t task_info_count;

    task_info_count = TASK_INFO_MAX;
    kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) {

       return [ NSString stringWithFormat: @"%f" ,-1];

    }

    task_basic_info_t basic_info;
    thread_array_t thread_list;
    mach_msg_type_number_t thread_count;

    thread_info_data_t thinfo;
    mach_msg_type_number_t thread_info_count;

    thread_basic_info_t basic_info_th;
    uint32_t stat_thread = 0; // Mach threads

    basic_info = (task_basic_info_t)tinfo;

    // get threads in the task
    kr = task_threads(mach_task_self(), &thread_list, &thread_count);
    if (kr != KERN_SUCCESS) {

       return [ NSString stringWithFormat: @"%f" ,-1];

    }
    if (thread_count > 0)

       stat_thread += thread_count;
    

    long tot_sec = 0;
    long tot_usec = 0;
    float tot_cpu = 0;
    int j;

    for (j = 0; j < thread_count; j++)
    {

       thread_info_count = THREAD_INFO_MAX;
       kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
                        (thread_info_t)thinfo, &thread_info_count);
       if (kr != KERN_SUCCESS) {
           tot_cpu = -1;
           //return -1;
       }
    
       basic_info_th = (thread_basic_info_t)thinfo;
    
       if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
           tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
           tot_usec = tot_usec + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;
           tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
       }
    

    } // for each thread

    kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    assert(kr == KERN_SUCCESS);

    NSString *tostring = nil ;
    tostring = [ NSString stringWithFormat: @"%.1f" ,tot_cpu];
    NSLog (@"performance cpu:%@",tostring);

    return tostring;
    }

获取页面vc
上边收集了内存和cpu,还需要在收集数据的同时和页面对应上.这样就清楚了是当前页面的内存和cpu情况.

/**
*获取当前vc
*/

  • (UIViewController *) get_vc {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{

    if ([keyWindow.rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)keyWindow.rootViewController;
        UINavigationController *nav = tab.childViewControllers[tab.selectedIndex];
        DDContainerController *content = [nav topViewController];
        weakSelf.vc = [content contentViewController];
    }

    });
    return self.vc;
    }

获取设备信息
/*
*获取设备名称
*/

  • (NSString *) get_devicesName {
    NSString *devicesName = [UIDevice currentDevice].name; //设备名称
    NSLog(@"performance devicesName:%@", devicesName);
    return devicesName;

}

/*
*获取系统版本
*/

  • (NSString *) get_systemVersion{
    NSString *systemVersion = [UIDevice currentDevice].systemVersion; //系统版本
    NSLog(@"performance version:%@", systemVersion);
    return systemVersion;
    }

/*
*获取设备idf
*/

  • (NSString *) get_idf {
    NSString *idf = [UIDevice currentDevice].identifierForVendor.UUIDString;
    NSLog(@"performance idf:%@", idf);
    return idf;

}
数据拼接
最终要把内存、cpu等数据拼接成字典的形式,方便输出查看

输出log日志的数据格式

{

"cpu": "0.4",
"fps": "60 FPS",
"version": "11.2",
"appname": "xxxxxx",
"battery": "-100.0",
"appversion": "5.0.4",
"time": "2018-09-07 11:45:24",
"memory": "141.9",
"devicesName": "xxxxxx",
"vcClass": "DDAlreadPaidTabListVC",
"idf": "8863F83E-70CB-43D5-B6C7-EAB85F3A2AAD"

}

开启子线程采集
开一个子线程定时采集数据

/*

  • 性能采集子线程
    */
  • (void) performancethread {
    NSThread *thread = [[NSThread alloc] initWithBlock:^{

       NSLog(@"performance   ======get performance======");
    
       [self get_fps];
    
       while (true) {
           DDPerformanceModel *model = [DDPerformanceModel new];
           model.time=[self get_time];
           model.appname=[self get_appname];
           model.appversion=[self get_appversion];
           model.idf =[self get_idf];
           model.devicesName =[self get_devicesName];
           model.version = [self get_systemVersion ];
           model.vcClass = NSStringFromClass([self get_vc].class);
           model.memory = [self get_memory];
           model.battery = [self get_battery];
           model.cpu = [self get_cpu];
           model.fps = self.percount;
    
           NSString *json = [model modelToJSONString];
    

// printf(" getperformance %srn", [json UTF8String]);

        NSLog(@"getperformance model  %@", json);
        sleep(5);
    }
}];
[thread start];

NSLog(@"performance   ======continue mainblock======");

}

初始化性能采集
AppDelegate.m文件中didFinishLaunchingWithOptions方法中用户各种初始化操作,可以在第一行初始化性能采集,
这样app启动以后就可以定时采集数据

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {

    [[getperformance new] performancethread];//获取性能数据
    
    }

    性能采集日志存储

一般来说日志存储都是写入到本地log日志,然后读取.但是有两个问题

需要读写文件代码,对于不熟悉oc的人来说比较难

因为是定时采集,文件IO操作频繁

所以不考虑存储本地log日志的方式,可以在代码中打印出数据,通过截获当前设备运行的日志获取数据.

模拟器可以使用xcrun simctl命令获取当前设备运行日志,
真机用libimobiledevice获取日志

xcrun simctl spawn booted log stream --level=debug | grep getperformance

输出log日志的数据格式,这块做了json美化,每歌几秒在控制台就打印一次

{

"cpu": "0.4",
"fps": "60 FPS",
"version": "11.2",
"appname": "xxxxxx",
"battery": "-100.0",
"appversion": "5.0.4",
"time": "2018-09-07 11:45:24",
"memory": "141.9",
"devicesName": "xxxxxx",
"vcClass": "DDAlreadPaidTabListVC",
"idf": "8863F83E-70CB-43D5-B6C7-EAB85F3A2AAD"

}

如果获取多次数据可以使用shell脚本把命令放到后台,定时写入到logpath中
nohup xcrun simctl spawn booted log stream --level=debug >${logpath} &
代码插入到工程中
因为在持续集成中,每次打取的代码都是不带性能测试代码,这些代码是单独写到文件中.在编译项目前,用shell把代码插入到工程中,这样打出来的包才能有采集性能数据功能.

scriptrootpath=${2}
AddFiles=${2}"/GetPerformance/performancefiles"
localDDPerformanceModelh=${scriptrootpath}"/GetPerformance/performancefiles/DDPerformanceModel.h"
localDDPerformanceModelm=${scriptrootpath}"/GetPerformance/performancefiles/DDPerformanceModel.m"
localgetperformanceh=${scriptrootpath}"/GetPerformance/performancefiles/getperformance.h"
localgetperformancem=${scriptrootpath}"/GetPerformance/performancefiles/getperformance.m"

addfiles(){

echo "删除${projectaddpath}中的原性能采集文件"

rm -rf ${DDPerformanceModelh}
rm -rf ${DDPerformanceModelm}
rm -rf ${getperformanceh}
rm -rf ${getperformancem}

echo "复制文件到${projectaddpath}路径"

cp  ${localDDPerformanceModelh} ${projectaddpath}
cp  ${localDDPerformanceModelm} ${projectaddpath}
cp  ${localgetperformanceh} ${projectaddpath}
cp  ${localgetperformancem} ${projectaddpath}

}
性能数据绘制
在手工和自动化使用插入性能测试代码的app,如果截获性能数据后,可以对数据做性能数据绘制.

用Higcharts或者echarts绘制性能走势图
image.png
如何在持续集成中使用
monkey和UI自动化中使用,最终会发送一份性能报告.

Demo代码
已经把性能代码脱了主项目,可在Demo代码中编译,github地址:https://github.com/xinxi1990/iOSPerformanceTest

最后
虽然iOS生态封闭,但是对于开发者和测试者还是有一些空间可以利用的.

iOS测试一直都是一个难点,难懂的oc语法和iOS整体框架.如果你开始慢慢接触iOS,会发现iOS测试也并不是那么难,需要一点耐心和一点专心而已.

(文章来源于霍格沃兹测试学院)

更多优秀内容及资料可点击获取

相关实践学习
通过性能测试PTS对云服务器ECS进行规格选择与性能压测
本文为您介绍如何利用性能测试PTS对云服务器ECS进行规格选择与性能压测。
相关文章
|
1月前
|
移动开发 安全 数据安全/隐私保护
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
|
1月前
|
测试技术
现代软件测试中的自动化工具与挑战
传统软件测试面临着越来越复杂的系统架构和不断增长的测试需求,自动化测试工具应运而生。本文将探讨现代软件测试中自动化工具的应用和挑战,深入分析其优势与局限性,为软件测试领域的发展提供思路和启示。
|
1月前
|
敏捷开发 分布式计算 测试技术
深入理解软件测试中的自动化框架选择与优化策略
【2月更文挑战第29天】 在软件开发的生命周期中,测试环节扮演着至关重要的角色。随着敏捷开发和持续集成的普及,自动化测试成为确保软件质量和加快产品上市速度的关键手段。本文将探讨在构建自动化测试框架时面临的挑战,分析不同类型自动化框架的特点及其适用场景,并提出一系列优化策略,旨在帮助测试工程师提高测试效率,确保测试结果的准确性。
21 0
|
1月前
|
安全 测试技术
现代软件测试中的自动化技术应用及挑战
在当今数字化时代,软件测试的重要性日益凸显。本文探讨了现代软件测试中自动化技术的应用现状和挑战,分析了自动化测试在提高效率、降低成本、增强可靠性等方面的优势,同时也提出了自动化测试所面临的挑战和解决方案。
|
1月前
|
jenkins 测试技术 持续交付
现代软件测试中的自动化工具与挑战
随着软件开发领域的不断发展,自动化测试工具在测试过程中扮演着越来越重要的角色。本文将探讨现代软件测试中自动化工具的应用及面临的挑战,旨在帮助开发人员和测试人员更好地理解和应对自动化测试中的问题。
|
15天前
|
敏捷开发 监控 测试技术
深入探索软件测试中的自动化边界
【4月更文挑战第10天】 在现代软件开发过程中,自动化测试已成为提升效率、确保质量的关键手段。然而,随着技术的不断进步和项目需求的多样化,确定自动化的合理边界成为测试工程师面临的重要问题。本文将探讨如何界定自动化测试的有效范围,包括成本效益分析、风险评估与技术选型等方面,并提出一种基于风险和回报权衡的自动化测试策略。
11 0
|
17天前
|
测试技术
深入理解软件测试中的自动化边界
【4月更文挑战第7天】 在追求快速交付和质量保证的双重压力下,软件测试领域正经历着从手工到自动化的转变。本文旨在探讨自动化测试的有效边界,即哪些场景适合自动化,以及如何界定这些边界以优化测试策略。通过对自动化测试优势、挑战及适用性的分析,文章为读者提供了一个清晰的框架,用于评估和实施自动化测试。
|
19天前
|
jenkins 测试技术 持续交付
软件测试|docker搭建Jenkins+Python+allure自动化测试环境
通过以上步骤,你可以在Docker中搭建起Jenkins自动化测试环境,实现Python测试的自动化执行和Allure报告生成。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
39 6
|
23天前
|
Java 测试技术 API
软件测试中的自动化工具与策略
软件测试是确保软件质量的重要环节,而自动化测试工具和策略的应用在提高测试效率和准确性方面发挥着重要作用。本文将介绍几种常见的自动化测试工具,并探讨在软件测试中应用自动化测试的最佳实践和策略。
|
30天前
|
敏捷开发 测试技术 持续交付
深入探索软件测试自动化:框架与实践
在快速演进的软件行业中,测试自动化已成为确保产品质量和加快上市速度的关键因素。本文将深入分析测试自动化框架的构建要点,探讨其在实际应用中的效益,以及实施过程中可能面临的挑战。通过对比手动测试与自动化测试的优势与局限,本文旨在为读者提供一套系统化的测试自动化实践指南,以支持更高效、可靠的软件开发周期。
11 0