mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Scale the tablebase cache to ~100k entries (#24)
* Slim the cached tablebase value to the fields the trainer reads (#20) * Sample for LFU eviction instead of scanning the cache (#21) * Make cache size configurable and back up daily off-peak (#22) * Document the cache design in the glossary and an ADR (#23) * Address cache-scaling code review findings
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
defmodule Chesstrainer.Cache.BackupScheduleTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias Chesstrainer.Cache.BackupSchedule
|
||||
|
||||
describe "parse_time/1" do
|
||||
test "accepts HH:MM strings and {hour, minute} tuples" do
|
||||
assert {:ok, {3, 0}} = BackupSchedule.parse_time("03:00")
|
||||
assert {:ok, {23, 59}} = BackupSchedule.parse_time("23:59")
|
||||
assert {:ok, {0, 5}} = BackupSchedule.parse_time("0:05")
|
||||
assert {:ok, {4, 30}} = BackupSchedule.parse_time({4, 30})
|
||||
end
|
||||
|
||||
test "rejects values that are not a valid UTC time" do
|
||||
assert :error = BackupSchedule.parse_time("24:00")
|
||||
assert :error = BackupSchedule.parse_time("03:60")
|
||||
assert :error = BackupSchedule.parse_time("03:00:00")
|
||||
assert :error = BackupSchedule.parse_time("three")
|
||||
assert :error = BackupSchedule.parse_time({24, 0})
|
||||
assert :error = BackupSchedule.parse_time(nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe "time!/1" do
|
||||
test "defaults to 03:00 UTC" do
|
||||
assert {3, 0} = BackupSchedule.time!(nil)
|
||||
assert {3, 0} = BackupSchedule.time!("03:00")
|
||||
end
|
||||
end
|
||||
|
||||
describe "next_delay/2" do
|
||||
test "returns the delay until today's run when it is still ahead" do
|
||||
now = ~U[2026-11-02 01:30:00Z]
|
||||
|
||||
assert BackupSchedule.next_delay({3, 0}, now) == :timer.hours(1) + :timer.minutes(30)
|
||||
end
|
||||
|
||||
test "rolls over to tomorrow once today's run has passed" do
|
||||
now = ~U[2026-11-02 04:00:00Z]
|
||||
|
||||
assert BackupSchedule.next_delay({3, 0}, now) == :timer.hours(23)
|
||||
end
|
||||
|
||||
test "waits a full day when called exactly at the scheduled time" do
|
||||
now = ~U[2026-11-02 03:00:00Z]
|
||||
|
||||
assert BackupSchedule.next_delay({3, 0}, now) == :timer.hours(24)
|
||||
end
|
||||
|
||||
test "computes the delay from the wall clock by default" do
|
||||
delay = BackupSchedule.next_delay({0, 0})
|
||||
|
||||
assert delay > 0
|
||||
assert delay <= :timer.hours(24)
|
||||
end
|
||||
end
|
||||
end
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
defmodule Chesstrainer.Cache.EvictTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias Chesstrainer.Cache.Evict
|
||||
|
||||
@sample_size 64
|
||||
|
||||
setup do
|
||||
table = :"evict_test_#{System.unique_integer([:positive])}"
|
||||
:ets.new(table, [:named_table, :public, :set])
|
||||
|
||||
{:ok, table: table}
|
||||
end
|
||||
|
||||
describe "evict/3" do
|
||||
test "shrinks a table that is far over capacity", %{table: table} do
|
||||
for i <- 1..1_000, do: :ets.insert(table, {i, i, 1, i})
|
||||
|
||||
Evict.evict(table, 900)
|
||||
|
||||
assert :ets.info(table, :size) == 900
|
||||
end
|
||||
|
||||
test "keeps a frequently used entry while cold entries are evicted", %{table: table} do
|
||||
:ets.insert(table, {:hot, :hot, 100, 0})
|
||||
for i <- 1..1_000, do: :ets.insert(table, {i, i, 1, i})
|
||||
|
||||
Evict.evict(table, 100)
|
||||
|
||||
assert :ets.info(table, :size) == 100
|
||||
assert [{:hot, :hot, 100, 0}] = :ets.lookup(table, :hot)
|
||||
end
|
||||
|
||||
test "the returned cursor resumes the sample instead of restarting it", %{table: table} do
|
||||
for i <- 1..5, do: :ets.insert(table, {i, i, 1, i})
|
||||
|
||||
assert Evict.evict(table, 10) == :start
|
||||
|
||||
cursor = Evict.evict(table, 4)
|
||||
|
||||
assert :ets.info(table, :size) == 4
|
||||
refute cursor == :start
|
||||
end
|
||||
|
||||
test "rotates across the table, eventually evicting entries outside the first sample",
|
||||
%{table: table} do
|
||||
for i <- 1..1_000, do: :ets.insert(table, {i, i, 1, i})
|
||||
|
||||
# Find the slice a fresh pass would sample and make it the hottest part of
|
||||
# the table. A pass that always restarted from `:start` would keep
|
||||
# choosing from this slice (every one of those entries is hotter than
|
||||
# everything else), so cold entries elsewhere could never be evicted.
|
||||
hot_keys = first_sample_keys(table)
|
||||
for key <- hot_keys, do: :ets.insert(table, {key, key, 1_000, key})
|
||||
|
||||
Enum.reduce(1..@sample_size, :start, fn _, cursor ->
|
||||
Evict.evict(table, :ets.info(table, :size) - 1, cursor)
|
||||
end)
|
||||
|
||||
frequencies = Map.new(:ets.tab2list(table), fn {key, _v, f, _t} -> {key, f} end)
|
||||
cold_left = Enum.count(frequencies, fn {_key, frequency} -> frequency == 1 end)
|
||||
hot_left = Enum.count(frequencies, fn {_key, frequency} -> frequency == 1_000 end)
|
||||
|
||||
assert :ets.info(table, :size) == 1_000 - @sample_size
|
||||
assert cold_left < 1_000 - @sample_size
|
||||
assert hot_left > 0
|
||||
end
|
||||
end
|
||||
|
||||
defp first_sample_keys(table) do
|
||||
match_spec = [{{:"$1", :_, :"$2", :"$3"}, [], [:"$1"]}]
|
||||
|
||||
case :ets.select(table, match_spec, @sample_size) do
|
||||
{keys, _continuation} -> keys
|
||||
:"$end_of_table" -> []
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user