Virling 儀錶板載入由 177 秒縮短至 13 ms:簡單修正的效果
My dashboard took 177 seconds to load. Here's the boring fix.
開源收入分析工具 Virling 在載入 83k 筆測試事件時,將儀錶板載入時間由 177 秒縮短至 13 ms,做法是改用記憶體索引計算訪客數,避免每次載入都掃描全部事件。作者亦在 CI 加入 benchmark,若儀錶板明顯變慢,建置流程便會提示。
My dashboard took 177 seconds to load.
Now it takes 13 ms.
Here's what happened, and why the fix was a lot less exciting than the numbers suggest.
The setup
I'm writing Virling, an open source revenue analytics tool, in Rust. On Friday I loaded 83k test events into it to see how the dashboard would hold up with something closer to real traffic.
The page just sat there. For three minutes.
What was going wrong
Every time the dashboard loaded, it counted visitors by scanning every single event. With a small test dataset that's invisible. With 83k events, it turns a page load into a coffee break.
Nothing was broken in the usual sense. The code was correct. It was just doing far more work than it needed to, on every request.
The boring fix
The fix wasn't clever. Instead of scanning all the events on each page load, the dashboard now counts visitors from an in-memory index.
That one change took the load time from 177 seconds to 13 ms.
Making sure it stays fast
A fix like this is easy to undo by accident. Someone adds a feature, a query creeps back toward a full scan, and nobody notices until a user does.
So I added a benchmark to CI. If the dashboard gets meaningfully slower, the build tells me before anyone else finds out.
The rest of the week
This all happened in the middle of a week with three releases: payment sync for Stripe, Dodo, Polar and LemonSqueezy, a read API, and an MCP server. It would have been easy to skip the slow page and keep shipping features. I'm glad I didn't.
The lesson
Test with real-sized data early.
A hundred rows would never have shown me the 177 seconds. The problem only appeared once the data looked like something a real user might have, and by then it was cheap to fix because I caught it before anyone relied on it.
If you're building anything that reads a growing pile of events, logs or records, load a realistic amount of data into it now, not later. It's a boring step, and it's usually the one that saves you.
What's the slowest thing you've found in your own code?
Virling: virling.com
來源:DEV Community · dev.to