Files
NESLite/tools/collect-h-long.ps1

119 lines
5.7 KiB
PowerShell

[CmdletBinding()]
param(
[string]$Serial = '',
[string]$Package = 'org.neslite.app',
[ValidateRange(1, 1440)]
[int]$Minutes = 120,
[ValidateRange(10, 300)]
[int]$IntervalSeconds = 60,
[string]$OutputDirectory = ''
)
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
$sdkRoot = if ($env:ANDROID_HOME) { $env:ANDROID_HOME } else { 'C:\Users\shenw_o246oq4\AppData\Local\Android\Sdk' }
$adb = Join-Path $sdkRoot 'platform-tools\adb.exe'
if (-not (Test-Path $adb)) { throw "adb not found: $adb" }
$adbArguments = @()
if ($Serial) { $adbArguments += @('-s', $Serial) }
function Invoke-Adb([string[]]$Arguments) {
$output = & $adb @adbArguments @Arguments 2>&1
if ($LASTEXITCODE -ne 0) { throw "adb failed: $($output -join [Environment]::NewLine)" }
return @($output)
}
$state = (Invoke-Adb @('get-state') | Select-Object -First 1).Trim()
if ($state -ne 'device') { throw "Target device is not ready: $state" }
$pidValue = (Invoke-Adb @('shell', 'pidof', $Package) | Select-Object -First 1).Trim()
if (-not $pidValue) { throw "Package is not running: $Package" }
if (-not $OutputDirectory) {
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$OutputDirectory = Join-Path $root "build\device-evidence\h-long-$stamp"
}
$null = New-Item -ItemType Directory -Path $OutputDirectory -Force
$rawDirectory = Join-Path $OutputDirectory 'meminfo'
$null = New-Item -ItemType Directory -Path $rawDirectory -Force
$device = [ordered]@{
serial = (Invoke-Adb @('get-serialno') | Select-Object -First 1).Trim()
product = (Invoke-Adb @('shell', 'getprop', 'ro.product.model') | Select-Object -First 1).Trim()
release = (Invoke-Adb @('shell', 'getprop', 'ro.build.version.release') | Select-Object -First 1).Trim()
api = (Invoke-Adb @('shell', 'getprop', 'ro.build.version.sdk') | Select-Object -First 1).Trim()
abi = (Invoke-Adb @('shell', 'getprop', 'ro.product.cpu.abi') | Select-Object -First 1).Trim()
pageSizeBytes = [long]((Invoke-Adb @('shell', 'getconf', 'PAGESIZE') | Select-Object -First 1).Trim())
}
$started = Get-Date
$deadline = $started.AddMinutes($Minutes)
$samples = [System.Collections.Generic.List[object]]::new()
$index = 0
while ((Get-Date) -lt $deadline) {
$pidValue = (Invoke-Adb @('shell', 'pidof', $Package) | Select-Object -First 1).Trim()
if (-not $pidValue) { throw "Package exited during H-LONG at sample $index." }
$raw = Invoke-Adb @('shell', 'dumpsys', 'meminfo', '--local', $Package)
$rawPath = Join-Path $rawDirectory ("sample-{0:D4}.txt" -f $index)
$raw | Set-Content -LiteralPath $rawPath -Encoding utf8NoBOM
$text = $raw -join "`n"
$totalPss = if ($text -match 'TOTAL PSS:\s*([0-9,]+)') { [long]($Matches[1] -replace ',', '') } else { 0L }
$javaHeap = if ($text -match 'Java Heap:\s*([0-9,]+)') { [long]($Matches[1] -replace ',', '') } else { 0L }
$nativeHeap = if ($text -match 'Native Heap:\s*([0-9,]+)') { [long]($Matches[1] -replace ',', '') } else { 0L }
$samples.Add([pscustomobject]@{
index = $index
epochMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
elapsedMinutes = [Math]::Round(((Get-Date) - $started).TotalMinutes, 3)
pid = $pidValue
totalPssKiB = $totalPss
javaHeapKiB = $javaHeap
nativeHeapKiB = $nativeHeap
})
$samples | Export-Csv -LiteralPath (Join-Path $OutputDirectory 'memory.csv') -NoTypeInformation -Encoding utf8NoBOM
++$index
if ((Get-Date).AddSeconds($IntervalSeconds) -lt $deadline) { Start-Sleep -Seconds $IntervalSeconds }
}
$logcat = Invoke-Adb @('logcat', '-d', '--pid', $pidValue, '-v', 'epoch', '*:W')
$logcat | Set-Content -LiteralPath (Join-Path $OutputDirectory 'logcat-warnings.txt') -Encoding utf8NoBOM
$logText = $logcat -join "`n"
$trendSamples = @($samples | Where-Object elapsedMinutes -ge 10)
$trendSlopeKiBPerMinute = 0.0
if ($trendSamples.Count -ge 2) {
$meanX = ($trendSamples.elapsedMinutes | Measure-Object -Average).Average
$meanY = ($trendSamples.totalPssKiB | Measure-Object -Average).Average
$numerator = 0.0
$denominator = 0.0
foreach ($sample in $trendSamples) {
$deltaX = $sample.elapsedMinutes - $meanX
$numerator += $deltaX * ($sample.totalPssKiB - $meanY)
$denominator += $deltaX * $deltaX
}
if ($denominator -gt 0) { $trendSlopeKiBPerMinute = $numerator / $denominator }
}
$summary = [ordered]@{
formatVersion = 1
package = $Package
device = $device
requestedMinutes = $Minutes
intervalSeconds = $IntervalSeconds
startedAt = $started.ToString('o')
finishedAt = (Get-Date).ToString('o')
sampleCount = $samples.Count
firstTotalPssKiB = if ($samples.Count) { $samples[0].totalPssKiB } else { 0 }
lastTotalPssKiB = if ($samples.Count) { $samples[$samples.Count - 1].totalPssKiB } else { 0 }
peakTotalPssKiB = if ($samples.Count) { ($samples.totalPssKiB | Measure-Object -Maximum).Maximum } else { 0 }
postWarmupTrendStartMinute = 10
postWarmupTotalPssSlopeKiBPerMinute = [Math]::Round($trendSlopeKiBPerMinute, 3)
fatalExceptionCount = ([regex]::Matches($logText, 'FATAL EXCEPTION')).Count
anrCount = ([regex]::Matches($logText, 'ANR in ' + [regex]::Escape($Package))).Count
verdict = if ($Minutes -ge 120 -and $samples.Count -ge 120 -and
@($samples | Where-Object totalPssKiB -eq 0).Count -eq 0 -and
$logText -notmatch 'FATAL EXCEPTION|ANR in ') {
'REVIEW_MEMORY_TREND'
} else {
'INCOMPLETE_OR_FAILED'
}
}
$summary | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $OutputDirectory 'summary.json') -Encoding utf8NoBOM
Write-Output "H-LONG capture complete: $OutputDirectory"
Write-Output 'Review memory.csv for a sustained post-warmup trend before marking the run passed.'