Skip to content

Commit d95b46e

Browse files
derek73claude
andauthored
test: pin suffixes_prefixes_titles cache performance (#177)
* test: pin suffixes_prefixes_titles cache performance This library is used to parse large batches of names, making the _pst cache on suffixes_prefixes_titles load-bearing for throughput. Add a timeit-based test that asserts 10 000 repeated calls complete in under 10 ms — well within what a single uncached union build would take — so accidental cache removal surfaces immediately as a test failure. * test: add identity assertion to cache performance test --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent caf42fa commit d95b46e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

tests/test_constants.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import pickle
3+
import timeit
34

45
from nameparser import HumanName
56
from nameparser.config import Constants, RegexTupleManager, SetManager, TupleManager
@@ -232,3 +233,33 @@ def test_unpickle_legacy_state_with_property_key(self) -> None:
232233

233234
# The real customization is recovered and the property key is ignored.
234235
self.assertIn('legacytitle', restored.titles)
236+
237+
238+
class SuffixesPrefixesTitlesPerformanceTests(HumanNameTestBase):
239+
"""Guard against accidental cache removal on suffixes_prefixes_titles.
240+
241+
This library is commonly used to parse large batches of names, so
242+
suffixes_prefixes_titles must remain cached. Without the cache, each call
243+
rebuilds the union from ~700 strings (~50-100 µs); with it, repeated access
244+
is ~1000x faster. This test asserts that 10 000 repeated calls complete
245+
well within the time a single uncached union build would take.
246+
"""
247+
248+
def test_repeated_access_is_cached(self) -> None:
249+
c = Constants()
250+
first = c.suffixes_prefixes_titles
251+
second = c.suffixes_prefixes_titles
252+
assert first is second, "suffixes_prefixes_titles should return the same cached object on repeated access"
253+
254+
n = 10_000
255+
elapsed = timeit.timeit(lambda: c.suffixes_prefixes_titles, number=n)
256+
257+
# One uncached union build over ~700 strings takes ~50-100 µs on any
258+
# modern machine. If caching is broken, 10 000 calls would take
259+
# seconds; with caching they finish in well under 10 ms total.
260+
limit = 0.010 # 10 ms = 1 µs/call average
261+
assert elapsed < limit, (
262+
f"suffixes_prefixes_titles appears uncached: {n} calls took "
263+
f"{elapsed * 1000:.1f} ms (limit {limit * 1000:.0f} ms). "
264+
"Was _pst caching removed?"
265+
)

0 commit comments

Comments
 (0)