#!/bin/bash
# GPU render bench: drives the on-device renderer by writing phased workloads to the TTY of the
# phone's terminal session — no interaction with the phone needed beyond opening a session tab.
#
# Phases: P1 firehose scroll 30 s (full-damage frames — A/Bs against PERF.md §4's build/draw/swap),
# P2 idle 60 s (cursor-blink-only partial-present frames), P3 single-row updates 20 s (~8 Hz,
# typing-shaped partial frames). Each 60-painted-frame stats window lands in hilog.
#
# Use (see PERF.md §2 for the capture harness):
#   1. On the phone: open a terminal session to the host you'll run this on; make sure
#      Settings → Developer → "Log frame stats" is ON.
#   2. Copy this script to that host and find the session's TTY there:  who -u
#   3. Run:      ./gpubench.sh /dev/ttysNNN        (the phone session's tty)
#   4. Capture:  hdc -t <target> shell hilog | grep "connrender: gpu"
#
# Caveats: stats windows during P2/P3 run at low DVFS clocks — compare like-for-like cadence only
# (PERF.md §4 note). P1's first window includes the one-time CJK raster burst.
T="${1:-/dev/ttys000}"
corpus() {
  i=0
  while [ $i -lt 400 ]; do
    printf "\033[3%dm%04d 端末描画性能測定 中文渲染基准 가나다라마 \033[1mBOLD\033[0m ascii-abcdefghijklmnop %05d\n" $((i % 8)) $i $((i * 37))
    i=$((i + 1))
  done
}
C="$(corpus)"
printf "\n\033[36m=== GPUBENCH P1: firehose scroll 30s ===\033[0m\n" > "$T"
end=$((SECONDS + 30))
while [ $SECONDS -lt $end ]; do printf "%s\n" "$C" > "$T"; done
printf "\n\033[36m=== GPUBENCH P2: idle (blink only) 60s ===\033[0m\n" > "$T"
sleep 60
printf "\n\033[36m=== GPUBENCH P3: single-row updates 20s ===\033[0m\n" > "$T"
end=$((SECONDS + 20))
n=0
while [ $SECONDS -lt $end ]; do
  printf "\rtyping simulation %06d" $n > "$T"
  n=$((n + 1))
  sleep 0.12
done
printf "\n\033[36m=== GPUBENCH done ===\033[0m\n" > "$T"
