#aapt.exe adb.exe 设置为环境变量
#将脚本和apk包放在同一个文件夹
#.\monkeytest.ps1 -deviceid 串号 是monkey测试当前文件夹下所有apk
#.\monkeytest.ps1 -deviceid 串号 -apk_list ("androbench-5.0.1.apk", "HD Video Player.apk") 是monkey测试当前文件夹下指定的apk
#.\monkeytest.ps1 -deviceid 串号 -optNum 1 是monkey测试循环1次
Param(
[string[]]$apk_list = $null,
[string]$deviceid = "0123456789ABCDEF",
[string]$apkdiristrue = "0",
[string]$optNum = "1000000",
[Int64]$allseconds = 50000
)
$scriptName = "monkeytest"
$tempFile = "$scriptName-$deviceid"
$PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
$CurrentyDir = $PSScriptRoot
$apkdir1 = (Get-ChildItem $CurrentyDir| ?{$_.psiscontainer -eq $true})
$apkdir = "$CurrentyDir\$apkdir1"
echo "Currentdir: "$CurrentyDir
echo ""
function deleteTmpFile
{
$FILE_PATH="$CurrentyDir\$tempFile-*.txt"
$TRUE_FALSE=(Test-Path $FILE_PATH)
if($TRUE_FALSE -eq "True")
{
remove-Item -Recurse -Force $FILE_PATH
}
}
#获取包名
function getPackName([Object[]]$pathApk)
{
foreach($i in $pathApk){aapt d badging $LOCALAPKDir\$i|Select-String '^package: name'| out-string -width 4096 >> $CurrentyDir\$tempFile-package1.txt}
gc $CurrentyDir\$tempFile-package1.txt|?{-not [String]::IsNullOrWhiteSpace($_)} | set-content $CurrentyDir\$tempFile-package_old.txt
gc $CurrentyDir\$tempFile-package_old.txt |%{$_.split('=')[1]}|%{$_.split(' ')[0]} >> $CurrentyDir\$tempFile-package.txt
}
#testflow
$start = Get-Date
$startforM = Get-Date
#删除中间文件
deleteTmpFile
if( $apk_list -eq $null ){
if( $apkdiristrue -eq 0){
$pathApk = dir $CurrentyDir\*.apk -name
$LOCALAPKDir = $CurrentyDir
getPackName $pathApk
}else
{
$pathApk = dir $apkdir\*.apk -name
$LOCALAPKDir = $apkdir
getPackName $pathApk
}
}else{
$pathApk = $apk_list
getPackName $pathApk
}
function Invoke-TimeOutCommand-monkey()
{
$doit = "adb -s $deviceid shell monkey -p $line --throttle 100 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 300"
$job = Start-Job -ScriptBlock {
param($num)
$numBlock = [Scriptblock]::Create($num)
$numBlock.Invoke()
} -ArgumentList $doit| Wait-Job -Timeout 50
if($job.State -ne 'Completed')
{
Write-Warning 'timeout'
adb -s $deviceid shell pm clear $line
adb -s $deviceid shell am force-stop $line
$job | Stop-Job | Remove-Job
return $null
}
else
{
return $job | Receive-Job
}
}
for($i=1;$i -le $optNum;$i++)
{
$lines_pack = cat $CurrentyDir\$tempFile-package.txt
foreach ($line in $lines_pack){
echo "$deviceid $line monkey test"
Invoke-TimeOutCommand-monkey
sleep 2
}
$endforM = Get-Date
$timespend = ($endforM - $startforM).TotalSeconds
echo "now timespend is : $timespend----------------------------------------------------------------------"
if( $timespend -gt $allseconds ){
echo "time END--------------------------------------------------"
break
}
sleep 3
}
#删除中间文件
deleteTmpFile
#计算耗时
$end = Get-Date
Write-Host -ForegroundColor Red ($deviceid + ' monkeytest ' + 'Total Runtime: ' + ($end - $start).TotalSeconds)