8000 perf(reports): shorten the refresh after opening an idle app for 0.9.9 by InfinityPacer · Pull Request #37 · InfinityPacer/meterleaf · GitHub
[go: up one dir, main page]

Skip to content

perf(reports): shorten the refresh after opening an idle app for 0.9.9 - #37

Merged
InfinityPacer merged 4 commits into
mainfrom
perf/cold-start-refresh
Oct 13, 2026
Merged

InfinityPacer merged 4 commits into
mainfrom
perf/cold-start-refresh

Conversation

@InfinityPacer
Copy link
Copy Markdown
Owner

问题

冷启动打开 PWA 或页面后,旧数据很快出现,但刷新成最新数据要 5 秒左右。线上日志显示主要有三处开销。

  • 报表索引只在有人打开时才补齐采集器的推送(为减少写盘),空闲一夜后一次补 563 批,单此一步 5.5 秒。
  • 每次后台刷新约 500 ms,其中 95% 在生成快照。订阅与 API 两种口径各读一遍全部额度历史(2.5 万行且持续增长)并重复查询窗口汇总,预估计算对每行反复解析时间。
  • 报表响应约三分之二是前端从未使用的额度重置记录,这份数据还随报表缓存写盘。手机端每次格式化数字和日期都新建格式器。

改动

  • 服务端每小时在后台补一次报表索引,没有新推送时不写盘。打开页面时最多只需补一小时的量。
  • 两种口径共用同一次账户、额度和窗口汇总读取。
  • 额度只读报表用得到的部分,即每个来源账户窗口的最新读数,以及仍在进行的周期和上一周期。预估计算每行只解析一次时间。采样时间不是标准 UTC 格式的窗口保留全部历史,结果不变。
  • 去掉响应和缓存中的额度重置记录。报表缓存格式号升为 4,仍可读取 0.9.8 保存的缓存。回滚到 0.9.8 时旧版本会清空缓存重新计算,不会停用持久缓存。
  • 前端格式器改为复用。
  • 版本升到 0.9.9,更新 CHANGELOG。数据库结构版本未变,升级无需备份。

验证

  • bun test tests 441 通过,bun run typecheck、bun run build、所改文件 Prettier 检查与 git diff --check 通过。
  • 新增回归测试覆盖以下行为。
    • 后台补齐在无人打开时写入新推送,空闲时不写。
    • 窗口汇总复用前后结果一致。
    • 额度截取保留最新读数、本周期与上一周期、无采样时间的读数,以及非标准格式窗口的全部历史,且预估与读全部历史时完全一致。
    • 0.9.8 的缓存升级后仍被使用。
  • 截取测试和缓存测试都做过反向验证。把截取放宽到只留本周期,或去掉对格式 3 的兼容,对应测试即失败。
  • 用线上导出的 25160 条额度历史按时间顺序回放,在 4311 个时刻(其中 4237 个紧接周期重置)对比旧算法读全部历史与新算法读截取结果,输出完全一致。读取行数降到 7654,两种口径的额度部分本机耗时从约 65 ms 降到约 30 ms。
  • 同一份数据分别启动改动前后的服务端,报表内容除去重置记录外逐字段一致,/api/view 从 328 KB 降到 82 KB。以真实额度历史启动新版服务端,/api/view 正常返回各账户额度状态,日志无报错,单次快照约 35 ms。
  • 4 倍 CPU 降速下 6000 次格式化从 395 ms 降到 8 ms。

未验证

  • 合并后会在 NAS 升级并对比刷新耗时。改动前线上单次刷新的快照步骤为 435 至 478 ms。
  • 手机真机上的冷启动时间未实测。

🤖 Generated with Claude Code

InfinityPacer and others added 4 commits October 13, 2026 03:25
- Catch the report index up with collector pushes every hour in the
  background; a no-op when nothing new arrived, so idle disks stay quiet.
- Share quota history and window sums between the two USD bases.
- Drop the unused historical reset list from report payloads and cache.
- Reuse Intl formatters in chart and table rendering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Report refreshes read every quota snapshot ever collected and parsed each
row's timestamps several times per view. The view worker now reads, per
source account window, the latest reading plus the current and previous
cycle it belongs to, and quotaView parses timestamps once per row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Report cache entries no longer carry reset history, which 0.9.8 requires
when restoring them. A new namespace format makes an older version clear
the cache on rollback instead of disabling persistence, while this version
keeps reading entries that 0.9.8 saved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@InfinityPacer
InfinityPacer merged commit a3b92db into main Oct 13, 2026
2 checks passed
@InfinityPacer
InfinityPacer deleted the perf/cold-start-refresh branch October 13, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0