[本文出自天外歸雲的部落格園]
簡介
APP效能測試一般對以下幾個方面進行測試:
1.啟動時間(可以通過本工具測試);
2.CPU的佔用(可以通過本工具測試);
3.記憶體的佔用(可以通過本工具測試);
4.流量的耗用(可以通過本工具測試);
5.電量的耗用(使用者實際使用中感知即可)。
除了可以做以上這幾個專項測試外,本工具還能進行monkey測試等等。
可以結合工作需要靈活自定義指令碼,封裝成自己工作中常用的工具。
工具的實現是基於adb和PowerShell的,支援adb通過USB和WIFI兩種方式連線手機進行操作。
前置工作
1. 需要安裝adb;
2. 需要本機設定PowerShell指令碼執行策略。
指令碼示例
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到指定ip
function 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