[本文出自天外归云的博客园]
简介
APP性能测试一般对以下几个方面进行测试:
1.启动时间(可以通过本工具测试);
2.CPU的占用(可以通过本工具测试);
3.内存的占用(可以通过本工具测试);
4.流量的耗用(可以通过本工具测试);
5.电量的耗用(用户实际使用中感知即可)。
除了可以做以上这几个专项测试外,本工具还能进行monkey测试等等。
可以结合工作需要灵活自定义脚本,封装成自己工作中常用的工具。
工具的实现是基于adb和PowerShell的,支持adb通过USB和WIFI两种方式连接手机进行操作。
前置工作
1. 需要;
2. 需要本机。
脚本示例
PowerShell例子如下(不断更新):
#获取当前app包名和活动名Function GetPkgAndActName () { #确保app处于激活状态 $a = adb shell dumpsys window windows|findstr Focu $b = $a -like "*mCurrentFocus*" $b = $b.Trim() $startIndex = $b.IndexOf("{ ") $endIndex = $b.IndexOf("}") $pkgAndActName = (($b.Substring($startIndex+1,$endIndex-$startIndex-1)).split(" "))[2] return $pkgAndActName}#获取当前流量统计信息Function GetCurrFlow () { #确保app处于激活状态 $pkgAndActName = GetPkgAndActName $pkgName = ($pkgAndActName.split("/"))[0] $activityName = ($pkgAndActName.split("/"))[1] $userId = (((((adb shell dumpsys package $pkgName | findstr userId).Trim()).split("="))[1]).split(" "))[0] $rets = adb shell cat /proc/net/xt_qtaguid/stats | findstr $userId foreach ($ret in $rets) { $spices = ($ret.Split(" ")) $flow += [int]$spices[5]+[int]$spices[7] } $flow/1000}#转换文件大小单位function Convert-Size { [cmdletbinding()] param( [validateset("Bytes","KB","MB","GB","TB")] [string]$From, [validateset("Bytes","KB","MB","GB","TB")] [string]$To, [Parameter(Mandatory=$true)] [double]$Value, [int]$Precision = 4 ) switch($From) { "Bytes" { $value = $Value } "KB" { $value = $Value * 1024 } "MB" { $value = $Value * 1024 * 1024} "GB" { $value = $Value * 1024 * 1024 * 1024} "TB" { $value = $Value * 1024 * 1024 * 1024 * 1024} } switch ($To) { "Bytes" { return $value} "KB" { $Value = $Value/1KB} "MB" { $Value = $Value/1MB} "GB" { $Value = $Value/1GB} "TB" { $Value = $Value/1TB} } return [Math]::Round($value,$Precision,[MidPointRounding]::AwayFromZero) }#获取当前安卓app的启动耗时function CalcStartUpTime () { #确保app处于激活状态 $packageInfo = adb shell dumpsys activity | findstr mFocusedActivity $regex = [regex]"\s??(\S*)/(\S*)??\s" $s = $regex.Matches($packageInfo).Value $info = $s.SubString(1,$s.Length-1) $packageName = $info.split("/")[0] $activityName = $info.split("/")[1] adb shell am force-stop $packageName $result = adb shell am start -W $info | findstr WaitTime $result.replace("WaitTime","当前app启动耗时")}#获取当前安卓app的CPU占用情况(持续20次)function GetAppCPU () { #确保app处于激活状态 $pkgAndActName = GetPkgAndActName $pkgName = ($pkgAndActName.split("/"))[0] $count = 0 while ($count -lt 20) { adb shell top -n 1 | findstr $pkgName Start-Sleep -Seconds 1 $count++ }}#获取当前安卓app的内存占用情况(持续20次)function GetAppMem () { #确保app处于激活状态 $pkgAndActName = GetPkgAndActName $pkgName = ($pkgAndActName.split("/"))[0] $count = 0 while ($count -lt 20) { $appUsageRAMInfo = adb shell dumpsys meminfo $pkgName | findstr "TOTAL:" $infoRegex = [regex]"TOTAL:\s*(\d)*" $numRegex = [regex]"(\d)+" $appUsageRAM = $numRegex.Matches($infoRegex.Matches($appUsageRAMInfo).Value).Value $totalRAMInfo = (adb shell dumpsys meminfo | findstr "RAM" | findstr "Total").replace(",","") $totalRAM = $numRegex.Matches($totalRAMInfo).Value "当前app占用内存:"+$appUsageRAM+",占用率为:"+([int]$appUsageRAM/[int]$totalRAM)*100+"%" Start-Sleep -Seconds 1 $count++ }}#开启ADB-WIFI模式function AdbWifiConnect () { #确保手机连上usb(成功开启ADB-WIFI模式后方可以拔线) $ipText = adb shell ifconfig | findstr "Bcast" $ipInfoReg = [regex]"inet addr:\s*(\d)+`.(\d)+`.(\d)+`.(\d)+" $ipInfo = $ipInfoReg.Matches($ipText).Value $ipReg = [regex]"(\d)+`.(\d)+`.(\d)+`.(\d)+" $ip = $ipReg.Matches($ipInfo).Value adb disconnect $ip adb tcpip 5555 adb connect $ip}#重连ADB-WIFI到指定ipfunction ReconnectAdbWifi () { $ip= Read-Host "请输入手机ip" adb connect $ip}#主程序入口while($true){ Write-Host "输入数字进行选择" -ForegroundColor Green Write-Host "1 唤醒屏幕" -ForegroundColor Yellow Write-Host "2 输入文字" -ForegroundColor Yellow Write-Host "3 触发事件" -ForegroundColor Yellow Write-Host "4 向上滑动" -ForegroundColor Yellow Write-Host "5 向下滑动" -ForegroundColor Yellow Write-Host "6 向左滑动" -ForegroundColor Yellow Write-Host "7 向右滑动" -ForegroundColor Yellow Write-Host "8 删除输入" -ForegroundColor Yellow Write-Host "9 屏幕截图" -ForegroundColor Yellow Write-Host "10 获取手机分辨率" -ForegroundColor Yellow Write-Host "11 获取手机系统版本" -ForegroundColor Yellow Write-Host "12 获取当前app包名和活动名" -ForegroundColor Yellow Write-Host "13 流量统计" -ForegroundColor Yellow Write-Host "14 进行简单monkey测试" -ForegroundColor Yellow Write-Host "15 计算当前app的启动时间" -ForegroundColor Yellow Write-Host "16 获取当前安卓app的CPU占用情况(持续20次)" -ForegroundColor Yellow Write-Host "17 获取当前安卓app的内存占用情况(持续20次)" -ForegroundColor Yellow Write-Host "18 开启ADB-WIFI模式" -ForegroundColor Yellow Write-Host "19 重连ADB-WIFI" -ForegroundColor Yellow $choice = Read-Host "请选择" switch($choice) { 1 { adb shell input keyevent 26 } 2 { $text = Read-Host "输入文字";adb shell input text $text } 3 { $event = Read-Host "输入事件代号";adb shell input keyevent $event } 4 { adb shell input swipe 200 800 200 100 } 5 { adb shell input swipe 200 100 200 800 } 6 { adb shell input swipe 500 100 100 100 } 7 { adb shell input swipe 100 100 500 100 } 8 { [int]$amount = Read-Host "输入要删除的字符数量" for($i=0;$i -lt $amount;$i++) { adb shell input keyevent 67 } } 9 { $result = adb devices $device_id = $result[1].Split()[0] adb -s $device_id shell /system/bin/screencap -p /sdcard/screenshot.png adb -s $device_id pull /sdcard/screenshot.png d:/screenshot.png D:\screenshot.png } 10 { adb shell wm size } 11 { adb shell getprop ro.build.version.release } 12 { $pkgAndActName = GetPkgAndActName $pkgName = ($pkgAndActName.split("/"))[0] $activityName = ($pkgAndActName.split("/"))[1] "包名:"+$pkgName "活动名:"+$activityName } 13 { Read-Host "按任意键开始统计" $startFlow = GetCurrFlow Write-Host "流量监控中……`n" -ForegroundColor DarkMagenta Read-Host "按任意键结束统计" $endFlow = GetCurrFlow $consumedFlow = [int]$endFlow-[int]$startFlow $consumedFlowKb = Convert-Size -From KB -To KB -Value $consumedFlow $consumedFlowMb = Convert-Size -From KB -To MB -Value $consumedFlow "共消耗流量:"+$consumedFlowKb+"kb("+$consumedFlowMb+"mb)" } 14 { $count = Read-Host "请指定随机事件数" $pkgAndActName = GetPkgAndActName $pkgName = ($pkgAndActName.split("/"))[0] adb shell monkey -p $pkgName -v $count } 15 { CalcStartUpTime } 16 { GetAppCPU } 17 { GetAppMem } 18 { AdbWifiConnect } 19 { ReconnectAdbWifi } }}
可以根据实际测试过程中反复手点的过程进行组装调配。比如在反复测试登录的情况下,就要反复输入密码,如果来回用手点就比较麻烦,用这个小工具的话就非常轻松了,按一下上再敲一下回车就搞定了。
以下是进行统计指定时间内android应用流量的消耗:
计算当前app的内存占用情况:
退出:ctrl+c