perf(sandbox): make register_caches sleep configurable#46
Merged
Conversation
Cache.SandboxRegistry.register_caches/2 sleeps Process.sleep(50) after
each Registry.register call to give the entry time to propagate. With N
caches registered per test (common in apps with many cache modules),
this becomes ~N * 50ms of fixed overhead per test setup.
Read the value via Application.compile_env(:elixir_cache,
:sandbox_sleep_ms, 50). Default is unchanged at 50ms (backward
compatible). Test suites can opt in to a faster value (commonly 0):
# config/test.exs
config :elixir_cache, :sandbox_sleep_ms, 0
In a downstream umbrella with several cache apps, setting this to 0
saved ~1.7s across last_symbol_price_cache (10 tests x 102ms),
open_interest_cache (10 x 51ms), options_contract_expiry_cache
(3 x 51ms), and active_symbols_cache (4 x 51ms).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #46 +/- ##
==========================================
+ Coverage 83.46% 83.49% +0.02%
==========================================
Files 22 23 +1
Lines 635 636 +1
==========================================
+ Hits 530 531 +1
Misses 105 105 ☔ View full report in Codecov by Sentry. |
Per the request_cache_plug Config module pattern. Adds
lib/cache/config.ex with `Cache.Config.sandbox_sleep_ms/0` and switches
SandboxRegistry from a compile-time module attribute reading
Application.compile_env to a runtime call to the accessor.
Two reasons the runtime form is better here:
1. Process.sleep is already runtime-bound; the extra Application.get_env
call is a no-op compared to the actual sleep.
2. Future config knobs (TTL defaults, log levels, etc.) can land on the
same module, making the contract for downstream apps obvious — they
`config :elixir_cache, ...` and read via `Cache.Config`.
No behavior change vs. the previous commit on this branch — default
still 50 ms, override via the same `config :elixir_cache,
:sandbox_sleep_ms, N` key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cache.SandboxRegistry.register_caches/2doesProcess.sleep(50)after eachRegistry.registercall to give the entry time to propagate. With N caches registered per test (common in apps with many cache modules), this becomes ~N × 50 ms of fixed overhead per test setup.This change reads the value via
Application.compile_env(:elixir_cache, :sandbox_sleep_ms, 50). Default is unchanged at 50 ms (backward compatible). Test suites that don't need it can opt in to a faster value:Why this is safe
Registry.register/4returns{:ok, _}synchronously — once it returns, the entry is in the registry. The 50 ms sleep is paranoia for older Registry implementations (or possibly for letting:duplicate-keyed registrations propagate visibility). Modern Registry doesn't need it for correctness, but the default is preserved so no existing user pays a regression risk.Impact (downstream measurement)
In a Cheddar Flow umbrella with several cache apps, setting
sandbox_sleep_ms: 0saved:last_symbol_price_cache(10 tests)open_interest_cache(10 tests)options_contract_expiry_cache(3 tests)active_symbols_cache(4 tests)≈ 1.7 s saved across these apps' tests per umbrella mix test invocation.
Test plan