大谢小贺 發表於 2020-7-15 16:36:00

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

<p>​今天小编跟大家分享一篇来自学院内部学员的技术分享,本文主要介绍了作者在进行 iOS 自动化性能采集的一些经验,希望对大家在进行 iOS 自动化测试时有一些启发。</p>
<p>不要为小事遮住视线,我们还有更大的世界</p>
<p>&nbsp;</p>
<p><img src="https://wx4.sinaimg.cn/large/0076zEUEly4ggrpnd9f66j30zk0np761.jpg"></p>
<p>&nbsp;</p>
<h2>前言</h2>
<p>对于iOS总体生态是比较封闭的,相比Android没有像adb这种可以查看内存、cpu的命令.在日常做性能测试,需要借助xcode中instruments查看内存、cpu等数据.</p>
<p>但是借助instruments比较麻烦、又不能提供命令行.在持续集成中,很难时时的监控app的性能指标.并且现在app发版一般是2周左右,留给做专项测试的时间更少了,那么做核心场景性能测试,肯定是来不及的.</p>
<p>所以需要借助一些自动化工具来减轻手工采集性能指标的工作量.</p>
<h2>性能采集项</h2>
<p>app中基本性能采集项,内存、cpu、fps、电量等,因为自动化采集中手机设备是插着电脑充电的,所以不能采集电量数据.</p>
<h2>已有工具</h2>
<ul>
<li>
<p>instruments是官方提供的,不能做到自动化采集</p>
</li>
<li>
<p>腾讯gt,需要在app中集成sdk,有一定的接入成本</p>
</li>
<li>
<p>第三sdk,类似腾讯gt需要在app集成,可能会有数据泄漏风险</p>
</li>
</ul>
<h2>脚本开发</h2>
<p>上述的已有工具都不满足,在持续集成中做到自动化采集性能数据,期望的性能测试工具有一下几点:</p>
<ul>
<li>
<p>方便接入</p>
</li>
<li>
<p>可生成性能报告</p>
</li>
<li>
<p>可持续化</p>
</li>
<li>
<p>数据收集精准</p>
</li>
</ul>
<p>所以基于这几点,需要自己开发一套性能采集脚本.</p>
<h2>使用官方提供的api做性能采集</h2>
<h3>获取内存、cpu等</h3>
<pre><code>#import &lt;mach/mach.h&gt;<br><br>/**<br> * &nbsp;获取内存<br> */<br>- (NSString *)get_memory {<br> &nbsp; &nbsp;int64_t memoryUsageInByte = 0;<br> &nbsp; &nbsp;task_vm_info_data_t vmInfo;<br> &nbsp; &nbsp;mach_msg_type_number_t count = TASK_VM_INFO_COUNT;<br> &nbsp; &nbsp;kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &amp;vmInfo, &amp;count);<br> &nbsp; &nbsp;if(kernelReturn == KERN_SUCCESS) {<br> &nbsp; &nbsp; &nbsp; &nbsp;memoryUsageInByte = (int64_t) vmInfo.phys_footprint;<br> &nbsp; &nbsp; &nbsp; &nbsp;NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);<br> &nbsp; &nbsp;} else {<br> &nbsp; &nbsp; &nbsp; &nbsp;NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;double mem = memoryUsageInByte / (1024.0 * 1024.0);<br> &nbsp; &nbsp;NSString *memtostring ;<br> &nbsp; &nbsp;memtostring = ;<br><br> &nbsp; &nbsp;return memtostring;<br>}<br><br><br>/**<br> * 获取cpu<br> */<br>- (NSString *) get_cpu{<br> &nbsp; &nbsp;kern_return_t kr;<br> &nbsp; &nbsp;task_info_data_t tinfo;<br> &nbsp; &nbsp;mach_msg_type_number_t task_info_count;<br><br> &nbsp; &nbsp;task_info_count = TASK_INFO_MAX;<br> &nbsp; &nbsp;kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &amp;task_info_count);<br> &nbsp; &nbsp;if (kr != KERN_SUCCESS) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return [ NSString stringWithFormat: @"%f" ,-1];<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;task_basic_info_t &nbsp; &nbsp; &nbsp;basic_info;<br> &nbsp; &nbsp;thread_array_t &nbsp; &nbsp; &nbsp; &nbsp; thread_list;<br> &nbsp; &nbsp;mach_msg_type_number_t thread_count;<br><br> &nbsp; &nbsp;thread_info_data_t &nbsp; &nbsp; thinfo;<br> &nbsp; &nbsp;mach_msg_type_number_t thread_info_count;<br><br> &nbsp; &nbsp;thread_basic_info_t basic_info_th;<br> &nbsp; &nbsp;uint32_t stat_thread = 0; // Mach threads<br><br> &nbsp; &nbsp;basic_info = (task_basic_info_t)tinfo;<br><br> &nbsp; &nbsp;// get threads in the task<br> &nbsp; &nbsp;kr = task_threads(mach_task_self(), &amp;thread_list, &amp;thread_count);<br> &nbsp; &nbsp;if (kr != KERN_SUCCESS) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return [ NSString stringWithFormat: @"%f" ,-1];<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;if (thread_count &gt; 0)<br> &nbsp; &nbsp; &nbsp; &nbsp;stat_thread += thread_count;<br><br> &nbsp; &nbsp;long tot_sec = 0;<br> &nbsp; &nbsp;long tot_usec = 0;<br> &nbsp; &nbsp;float tot_cpu = 0;<br> &nbsp; &nbsp;int j;<br><br> &nbsp; &nbsp;for (j = 0; j &lt; thread_count; j++)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;thread_info_count = THREAD_INFO_MAX;<br> &nbsp; &nbsp; &nbsp; &nbsp;kr = thread_info(thread_list, THREAD_BASIC_INFO,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (thread_info_t)thinfo, &amp;thread_info_count);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (kr != KERN_SUCCESS) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tot_cpu = -1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//return -1;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp;basic_info_th = (thread_basic_info_t)thinfo;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if (!(basic_info_th-&gt;flags &amp; TH_FLAGS_IDLE)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tot_sec = tot_sec + basic_info_th-&gt;user_time.seconds + basic_info_th-&gt;system_time.seconds;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tot_usec = tot_usec + basic_info_th-&gt;user_time.microseconds + basic_info_th-&gt;system_time.microseconds;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tot_cpu = tot_cpu + basic_info_th-&gt;cpu_usage / (float)TH_USAGE_SCALE * 100.0;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;} // for each thread<br><br> &nbsp; &nbsp;kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));<br> &nbsp; &nbsp;assert(kr == KERN_SUCCESS);<br><br> &nbsp; &nbsp;NSString *tostring = nil ;<br> &nbsp; &nbsp;tostring = [ NSString stringWithFormat: @"%.1f" ,tot_cpu];<br> &nbsp; &nbsp;NSLog (@"performance &nbsp;cpu:%@",tostring);<br><br> &nbsp; &nbsp;return tostring;<br>}<br><br></code></pre>
<h3>获取页面vc</h3>
<p>上边收集了内存和cpu,还需要在收集数据的同时和页面对应上.这样就清楚了是当前页面的内存和cpu情况.</p>
<pre><code>/**<br> *获取当前vc<br> */<br>- (UIViewController *) get_vc {<br> &nbsp; &nbsp;UIWindow *keyWindow = .keyWindow;<br> &nbsp; &nbsp;__weak typeof(self) weakSelf = self;<br> &nbsp; &nbsp;dispatch_async(dispatch_get_main_queue(), ^{<br> &nbsp; &nbsp; &nbsp; &nbsp;if (]) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UITabBarController *tab = (UITabBarController *)keyWindow.rootViewController;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UINavigationController *nav = tab.childViewControllers;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DDContainerController *content = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;weakSelf.vc = ;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp;});<br> &nbsp; &nbsp;return self.vc;<br>}<br></code></pre>
<h3>获取设备信息</h3>
<pre><code>/*<br> *获取设备名称<br> */<br>- (NSString *) get_devicesName {<br> &nbsp; &nbsp;NSString *devicesName = .name; //设备名称<br> &nbsp; &nbsp;NSLog(@"performance &nbsp; devicesName:%@", devicesName);<br> &nbsp; &nbsp;return devicesName;<br><br>}<br><br>/*<br> *获取系统版本<br> */<br>- (NSString *) get_systemVersion{<br> &nbsp; &nbsp;NSString *systemVersion = .systemVersion; //系统版本<br> &nbsp; &nbsp;NSLog(@"performance &nbsp; version:%@", systemVersion);<br> &nbsp; &nbsp;return systemVersion;<br>}<br><br>/*<br> *获取设备idf<br> */<br>- (NSString *) get_idf {<br> &nbsp; &nbsp;NSString *idf = .identifierForVendor.UUIDString;<br> &nbsp; &nbsp;NSLog(@"performance &nbsp; idf:%@", idf);<br> &nbsp; &nbsp;return idf;<br><br>}<br></code></pre>
<h3>数据拼接</h3>
<p>最终要把内存、cpu等数据拼接成字典的形式,方便输出查看</p>
<pre><code>输出log日志的数据格式<br><br>{<br> &nbsp; &nbsp;"cpu": "0.4",<br> &nbsp; &nbsp;"fps": "60 FPS",<br> &nbsp; &nbsp;"version": "11.2",<br> &nbsp; &nbsp;"appname": "xxxxxx",<br> &nbsp; &nbsp;"battery": "-100.0",<br> &nbsp; &nbsp;"appversion": "5.0.4",<br> &nbsp; &nbsp;"time": "2018-09-07 11:45:24",<br> &nbsp; &nbsp;"memory": "141.9",<br> &nbsp; &nbsp;"devicesName": "xxxxxx",<br> &nbsp; &nbsp;"vcClass": "DDAlreadPaidTabListVC",<br> &nbsp; &nbsp;"idf": "8863F83E-70CB-43D5-B6C7-EAB85F3A2AAD"<br>}<br><br></code></pre>
<h3>开启子线程采集</h3>
<pre><code>开一个子线程定时采集数据<br><br>/*<br> * 性能采集子线程<br> */<br><br>- (void) performancethread {<br> &nbsp; &nbsp;NSThread *thread = [ initWithBlock:^{<br> &nbsp; &nbsp; &nbsp; &nbsp;NSLog(@"performance &nbsp; ======get performance======");<br><br> &nbsp; &nbsp; &nbsp; &nbsp;;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;while (true) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DDPerformanceModel *model = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.time=;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.appname=;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.appversion=;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.idf =;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.devicesName =;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.version = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.vcClass = NSStringFromClass(.class);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.memory = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.battery = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.cpu = ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.fps = self.percount;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NSString *json = ;<br><br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(" getperformance &nbsp; &nbsp;%s\r\n", );<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NSLog(@"getperformance model &nbsp;%@", json);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(5);<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp;}];<br> &nbsp; &nbsp;;<br><br> &nbsp; &nbsp;NSLog(@"performance &nbsp; ======continue mainblock======");<br>}<br><br><br></code></pre>
<h3>初始化性能采集</h3>
<pre><code>AppDelegate.m文件中didFinishLaunchingWithOptions方法中用户各种初始化操作,可以在第一行初始化性能采集,<br>这样app启动以后就可以定时采集数据<br><br>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {<br><br> &nbsp; &nbsp;[ performancethread];//获取性能数据<br><br> &nbsp; &nbsp;}<br></code></pre>
<h3>性能采集日志存储</h3>
<p>一般来说日志存储都是写入到本地log日志,然后读取.但是有两个问题</p>
<ul>
<li>
<p>需要读写文件代码,对于不熟悉oc的人来说比较难</p>
</li>
<li>
<p>因为是定时采集,文件IO操作频繁</p>
</li>
</ul>
<p>所以不考虑存储本地log日志的方式,可以在代码中打印出数据,通过截获当前设备运行的日志获取数据.</p>
<pre><code>模拟器可以使用xcrun simctl命令获取当前设备运行日志,<br>真机用libimobiledevice获取日志<br><br>xcrun simctl spawn booted log stream --level=debug | grep getperformance<br><br>输出log日志的数据格式,这块做了json美化,每歌几秒在控制台就打印一次<br><br>{<br> &nbsp; &nbsp;"cpu": "0.4",<br> &nbsp; &nbsp;"fps": "60 FPS",<br> &nbsp; &nbsp;"version": "11.2",<br> &nbsp; &nbsp;"appname": "xxxxxx",<br> &nbsp; &nbsp;"battery": "-100.0",<br> &nbsp; &nbsp;"appversion": "5.0.4",<br> &nbsp; &nbsp;"time": "2018-09-07 11:45:24",<br> &nbsp; &nbsp;"memory": "141.9",<br> &nbsp; &nbsp;"devicesName": "xxxxxx",<br> &nbsp; &nbsp;"vcClass": "DDAlreadPaidTabListVC",<br> &nbsp; &nbsp;"idf": "8863F83E-70CB-43D5-B6C7-EAB85F3A2AAD"<br>}<br><br>如果获取多次数据可以使用shell脚本把命令放到后台,定时写入到logpath中<br>nohup xcrun simctl spawn booted log stream --level=debug &gt;${logpath} &amp;<br></code></pre>
<h2>代码插入到工程中</h2>
<p>因为在持续集成中,每次打取的代码都是不带性能测试代码,这些代码是单独写到文件中.在编译项目前,用shell把代码插入到工程中,这样打出来的包才能有采集性能数据功能.</p>
<pre><code>scriptrootpath=${2}<br>AddFiles=${2}"/GetPerformance/performancefiles"<br>localDDPerformanceModelh=${scriptrootpath}"/GetPerformance/performancefiles/DDPerformanceModel.h"<br>localDDPerformanceModelm=${scriptrootpath}"/GetPerformance/performancefiles/DDPerformanceModel.m"<br>localgetperformanceh=${scriptrootpath}"/GetPerformance/performancefiles/getperformance.h"<br>localgetperformancem=${scriptrootpath}"/GetPerformance/performancefiles/getperformance.m"<br><br>addfiles(){<br><br> &nbsp; &nbsp;echo "删除${projectaddpath}中的原性能采集文件"<br><br> &nbsp; &nbsp;rm -rf ${DDPerformanceModelh}<br> &nbsp; &nbsp;rm -rf ${DDPerformanceModelm}<br> &nbsp; &nbsp;rm -rf ${getperformanceh}<br> &nbsp; &nbsp;rm -rf ${getperformancem}<br><br> &nbsp; &nbsp;echo "复制文件到${projectaddpath}路径"<br><br> &nbsp; &nbsp;cp &nbsp;${localDDPerformanceModelh} ${projectaddpath}<br> &nbsp; &nbsp;cp &nbsp;${localDDPerformanceModelm} ${projectaddpath}<br> &nbsp; &nbsp;cp &nbsp;${localgetperformanceh} ${projectaddpath}<br> &nbsp; &nbsp;cp &nbsp;${localgetperformancem} ${projectaddpath}<br><br>}<br></code></pre>
<h2>性能数据绘制</h2>
<p>在手工和自动化使用插入性能测试代码的app,如果截获性能数据后,可以对数据做性能数据绘制.</p>
<p>用Higcharts或者echarts绘制性能走势图</p>
<p><img src="https://wx4.sinaimg.cn/large/0076zEUEly4ggrpnd99afj31cg0u0grp.jpg"></p>
<h2>如何在持续集成中使用</h2>
<p>monkey和UI自动化中使用,最终会发送一份性能报告.</p>
<h2>Demo代码</h2>
<p>已经把性能代码脱了主项目,可在Demo代码中编译,github地址:https://github.com/xinxi1990/iOSPerformanceTest</p>
<h2>最后</h2>
<p>虽然iOS生态封闭,但是对于开发者和测试者还是有一些空间可以利用的.</p>
<p>iOS测试一直都是一个难点,难懂的oc语法和iOS整体框架.如果你开始慢慢接触iOS,会发现iOS测试也并不是那么难,需要一点耐心和一点专心而已.</p>
<p>&nbsp;</p>
<p>(文章来源于霍格沃兹测试学院)</p>
<p>更多优秀内容及资料可点击获取</p>
<p>http://qrcode.testing-studio.com/f?from=bokeyuan&amp;url=https://ceshiren.com/t/topic/3595</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/hogwarts/p/13306336.html
頁: [1]
查看完整版本: iOS 测试 | iOS 自动化性能采集