vdi-cloud/tools/run-input-latency-bench.py

56 lines
2.8 KiB
Python
Raw Normal View History

"""Start/status/stop only the temporary VDI input benchmark through VM100's agent."""
import argparse
import base64
from pathlib import Path
import runpy
import sys
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("action", choices=["start", "status", "stop"])
args = parser.parse_args()
tools_dir = Path(__file__).resolve().parent
term = runpy.run_path(str(tools_dir / "pve_term_exec.py"))
vm = runpy.run_path(str(tools_dir / "pve_vm.py"))
ticket, csrf = term["login"]()
state = vm["api"]("/api2/json/nodes/starhe/qemu/100/status/current", ticket=ticket)
if state.get("status") != "running":
sys.exit("VM100 is not running; no benchmark task changed")
folder = r"C:\ProgramData\VDI\input-bench-20261103"
prefix = "$ErrorActionPreference='Stop'; $folder='" + folder + "'; "
if args.action == "start":
encoded = base64.b64encode((tools_dir / "input-latency-bench.ps1").read_bytes()).decode()
command = prefix + r"""
New-Item -ItemType Directory -Force $folder | Out-Null
if (Get-ScheduledTask -TaskName VDIInputLatencyBench -ErrorAction SilentlyContinue) { throw 'Benchmark task already exists; stop it first' }
[IO.File]::WriteAllBytes("$folder\bench.ps1",[Convert]::FromBase64String('""" + encoded + r"""'))
$action=New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -ExecutionPolicy Bypass -STA -WindowStyle Hidden -File $folder\bench.ps1"
$principal=New-ScheduledTaskPrincipal -UserId test -LogonType Interactive -RunLevel Limited
$settings=New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
Register-ScheduledTask -TaskName VDIInputLatencyBench -Action $action -Principal $principal -Settings $settings | Out-Null
Start-ScheduledTask -TaskName VDIInputLatencyBench
'Benchmark launched'
"""
elif args.action == "status":
command = prefix + r"""
Get-Content "$folder\status.txt" -ErrorAction SilentlyContinue
Get-Content "$folder\clicks.csv" -ErrorAction SilentlyContinue
Get-ScheduledTaskInfo -TaskName VDIInputLatencyBench | Select-Object LastTaskResult | ConvertTo-Json -Compress
"""
else:
command = prefix + r"""
Stop-ScheduledTask -TaskName VDIInputLatencyBench -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName VDIInputLatencyBench -Confirm:$false -ErrorAction SilentlyContinue
if (Test-Path "$folder\pid.txt") {
$benchPid=[int](Get-Content "$folder\pid.txt")
$process=Get-CimInstance Win32_Process -Filter "ProcessId=$benchPid"
if ($process.CommandLine -like '*input-bench-20261103*') { Stop-Process -Id $benchPid -ErrorAction SilentlyContinue }
}
'Benchmark stopped; other applications untouched'
"""
out, error = vm["agent"](ticket, csrf, ["powershell.exe", "-NoProfile", "-Command", command])
print(out)
if error:
print(error, file=sys.stderr)
sys.exit(1)