Skip to content

perf(state): persist daily energy aggregates — kill the 25 s cold-start - #308

Merged
frahlg merged 1 commit into
masterfrom
perf/persist-daily-energy-aggregates
May 25, 2026
Merged

frahlg merged 1 commit into
masterfrom
perf/persist-daily-energy-aggregates

Conversation

@frahlg

@frahlg frahlg commented May 25, 2026

Copy link
Copy Markdown
Member

Measured baseline (2026-05-25, live Pi, state.db = 1 GB)

range cold-start warm
/api/energy/daily?days=30 25 s ~50 ms
/api/energy/daily?days=90 15 s ~50 ms
/api/energy/daily?days=7 0.35 s ~50 ms

Snapshot dir = 6 GB (9 snapshots × ~670 MB each), state.db = 1 GB.

Root cause

The handler already had an in-memory cache, but it dies on every restart. Closed days are immutable — recomputing the per-day SQL integration every cold start is pure waste.

What this PR does

Persistent two-tier cache:

  1. New SQLite table energy_daily(day PRIMARY KEY, ...six Wh columns, computed_at_ms).
  2. state.LoadDailyEnergy(day) / state.SaveDailyEnergy(day, de).
  3. handleEnergyDaily checks in-memory → persistent → recompute, and lazily writes a row on first computation for any closed day. Today's row is excluded (day still accumulating).

Expected impact

Cold /api/energy/daily?days=30 drops from ~25 s to the cost of 30 indexed PK lookups (<50 ms on the Pi). After the first warm load the persistent rows live forever — restart-free benefit accrues from there.

Tests

  • TestDailyEnergyPersistRoundtrip — save/load + upsert overwrite
  • TestDailyEnergyMissReturnsFalse — miss semantics
  • Full suite: go test ./... green

Out of scope (follow-up)

🤖 Generated with Claude Code

2026-05-25 measurement on the live Pi (state.db = 1 GB):

  /api/energy/daily?days=30  →  ~25 s  (cold cache)
  /api/energy/daily?days=90  →  ~15 s  (cold cache, fewer cache misses
                                        because the date range fits a
                                        previously-warm window)
  /api/energy/daily?days=7   →  ~0.35 s (mostly warm)

The handler already had an in-memory cache, but it dies on every
restart. Closed days are immutable — recomputing them every cold
start is pure waste.

Add a persistent two-tier cache:

  1. New SQLite table energy_daily(day PK, six Wh columns,
     computed_at_ms).
  2. State methods LoadDailyEnergy / SaveDailyEnergy.
  3. handleEnergyDaily checks in-memory → persistent → recompute,
     and lazily writes a row on first computation for any closed
     day. Today's row is excluded — the day is still accumulating.

Expected impact: cold /api/energy/daily?days=30 drops from ~25 s to
the cost of 30 indexed PK lookups (<50 ms on the Pi). After the
first warm load the persistent rows live forever; restart-free
benefit accrues from there.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@frahlg
frahlg merged commit bd17a33 into master May 25, 2026
1 check passed
@frahlg
frahlg deleted the perf/persist-daily-energy-aggregates branch May 25, 2026 12:24
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