[本文出自天外歸雲的博客園]html
1. 須要安裝adb;android
2. 須要本機設置PowerShell腳本運行策略。shell
PowerShell例子以下(不斷更新):windows
#獲取當前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 } } }
能夠根據實際測試過程當中反覆手點的過程進行組裝調配。好比在反覆測試登陸的狀況下,就要反覆輸入密碼,若是來回用手點就比較麻煩,用這個小工具的話就很是輕鬆了,按一下上再敲一下回車就搞定了。app
如下是進行統計指定時間內android應用流量的消耗:tcp
計算當前app的內存佔用狀況:工具
退出:ctrl+c性能