Telemetry puts nexrad_decode_png at 1.76 s/call × 37,847 calls/h — ~18.5 h of CPU across the cluster every real hour, the single biggest CPU consumer in the system. The map-click path (fetch_rain_cells) already cached decoded frames by 5-min bucket, but the per-contact CommonVolumeRadarWorker path (fetch_decoded_frame) went straight to network + decode on every call. Backfill means many contacts share a 5-min window, so the same 66 MB frame was being decoded dozens of times. Wire fetch_decoded_frame through NexradCache keyed on the rounded timestamp. Add a 20-entry size cap in NexradCache so backfill processing contacts in random timestamp order can't grow the ETS table to hundreds of GB. Each frame is ~66 MB, so 20 = ~1.3 GB worst case, well under typical pod memory. Expected impact: cuts sustained decode load by an order of magnitude depending on backfill temporal locality; map-click path is unchanged.
71 lines
2.2 KiB
Elixir
71 lines
2.2 KiB
Elixir
defmodule Microwaveprop.Weather.NexradCacheTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
alias Microwaveprop.Weather.NexradCache
|
|
|
|
setup do
|
|
NexradCache.clear()
|
|
:ok
|
|
end
|
|
|
|
describe "fetch/1" do
|
|
test "returns :miss when nothing is cached" do
|
|
assert NexradCache.fetch(~U[2026-04-12 12:00:00Z]) == :miss
|
|
end
|
|
|
|
test "returns cached pixels + width after put" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1, 2, 3, 4>>, 2)
|
|
assert {:ok, <<1, 2, 3, 4>>, 2} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
|
|
test "isolates entries by timestamp" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
|
|
NexradCache.put(~U[2026-04-12 12:05:00Z], <<2>>, 1)
|
|
|
|
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:05:00Z])
|
|
end
|
|
end
|
|
|
|
describe "prune_older_than/1" do
|
|
test "removes entries with a timestamp strictly before the cutoff" do
|
|
NexradCache.put(~U[2026-04-12 11:50:00Z], <<1>>, 1)
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<2>>, 1)
|
|
|
|
NexradCache.prune_older_than(~U[2026-04-12 11:55:00Z])
|
|
|
|
assert NexradCache.fetch(~U[2026-04-12 11:50:00Z]) == :miss
|
|
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
|
|
test "keeps entries equal to the cutoff" do
|
|
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
|
|
NexradCache.prune_older_than(~U[2026-04-12 12:00:00Z])
|
|
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
|
|
end
|
|
end
|
|
|
|
describe "size cap" do
|
|
test "evicts the oldest-by-timestamp entries once the cap is exceeded" do
|
|
# Insert 25 entries; max is 20, so 5 oldest get dropped.
|
|
base = ~U[2026-04-12 00:00:00Z]
|
|
|
|
for i <- 1..25 do
|
|
ts = DateTime.add(base, i * 300, :second)
|
|
NexradCache.put(ts, <<i>>, 1)
|
|
end
|
|
|
|
# The 5 oldest (i = 1..5) should have been evicted.
|
|
for i <- 1..5 do
|
|
ts = DateTime.add(base, i * 300, :second)
|
|
assert NexradCache.fetch(ts) == :miss
|
|
end
|
|
|
|
# The most recent 20 should still be present.
|
|
for i <- 6..25 do
|
|
ts = DateTime.add(base, i * 300, :second)
|
|
assert {:ok, <<^i>>, 1} = NexradCache.fetch(ts)
|
|
end
|
|
end
|
|
end
|
|
end
|