Skip to content

Terminal Performance

AceGuru-mjh edited this page Oct 1, 2026 · 4 revisions

终端性能(ATR 2.0 指标)

🖥️ 终端 & Linux · 🏠 首页 › Terminal-Performance

Home Version Kotlin Modules Tools License

Terminal-Performance

📑 本页目录

规格来源 ATR 2.0 Final Spec §49;v1 的 7 项指标全部达成。 来源 docs/PERF.md。

1. 七项指标

# 指标 目标 实现要点
① PTY → EventBus 延迟 < 20 ms PtyOutputPump 读 8KB chunks 非阻塞,直发 SharedFlow,无中间队列
② ProcessExited 事件 < 100 ms SessionManager exit watcher 每 100ms 轮询 nativeIsAlive + nativeWaitExit 阻塞立即返回
③ 语义快照 < 10 ms SemanticStateReducer 持 MutableStateFlow<TerminalSemanticState>,observe(SEMANTIC) 读 .value,O(1)
④ 增量 observe 0 重复字节 游标契约 prev.endCursor == next.startCursor
⑤ UI 连续输出 无主线程阻塞 / OOM / ANR yes 跑 60s 监控
⑥ RingBuffer 默认容量 256 KB RingTerminalBuffer.DEFAULT_CAPACITY = 256 * 1024,可配 64KB–4MB
⑦ 并发订阅者 ≥ 8 SharedFlow extraBufferCapacity = 1024,每订阅者独立游标

2. 五个设计选择

① 单读者泵

只有 PtyOutputPumpImpl 调 nativeRead —— 无竞争、无重复读。泵跑在 Dispatchers.IO: 每次读 → 恰好一个 OutputProduced 事件 → RingBuffer append → VT feed → reducer 更新 → bus emit,全在 IO 调度器。

② EventBus 推而非轮询

WaitEngineImpl.await() 订阅 EventBus Flow + withTimeoutOrNull, 没有 sleep+read 循环,唤醒延迟 = O(事件到达) 而非 O(轮询间隔)。

③ 增量语义状态

SemanticStateReducer.onEvent() 模式匹配 12 种事件类型,只更新受影响字段: OutputProduced → session.cursor + input.state;ProcessExited → foregroundJob + lastExitCode。无全量重算。

④ 游标式 RingBuffer

getSince(cursor) 返回 [cursor, cursor + available);越界(cursor < oldestCursor)返回 overrun = true + 空字节 —— 从不静默丢弃。

⑤ 串行化写入

InputManagerImpl 每 session 一个 Channel<WriteOp>(无限容量),单写协程顺序调 nativeWrite; InputControlState(TAKEOVER)仲裁是入队前的廉价 StateFlow 检查。

3. 内存有界策略

对象 策略
RingBuffer 默认 256KB,溢出淘汰最旧字节,EventLog 引用变 overrun
EventLog 每 session 内存 ArrayList,tail() 取最后 N;持久化只写元数据 + 最后 100 条事件
VT100Emulator 固定 rows × cols 单元数组,resize 复用

4. 已知未优化项(v2+)

项 问题 何时重要
EventLog.query(afterCursor) O(n) 线性扫描 >100K 事件时(v1 主路径是 tail(N),可接受)
VT100Emulator.scrollUp() 每行 System.arraycopy,O(rows×cols) 高频滚动(如 yes)是瓶颈;环形行索引可降到 O(cols)
EventBus.subscribe(afterCursor) 回放过老游标可能很大(有 maxEvents 上限) 极端回溯
exit watcher 每 session 一个协程 >50 session 建议共享 watcher(v1 典型 1–3 session)

5. 验证方式

./gradlew :platform:terminal:test
./gradlew :app:assembleDebug

# 插桩延迟测试(需模拟器)
adb shell am instrument -w \
  -e class com.apex.agent.platform.terminal.PerfTest \
  com.apex.agent.test/androidx.test.runner.AndroidJUnitRunner

6. 相关页面

footer

🏠 返回首页 · 📚 文档索引 · ❓ FAQ · 🔧 故障排查 · 🗺️ 路线图 · 🐛 提 Issue

Android Guru Agent · v1.4.4 · Kotlin 2.0.21 · Compose · PRoot · Room

Clone this wiki locally