Align Python cache benchmark with C# shape

This commit is contained in:
Meta Vens 2026-06-14 20:08:25 +01:00
commit b57d3c7441
4 changed files with 30 additions and 5 deletions

View file

@ -233,6 +233,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
benchmarks with the same operation names and reports median Python-to-C# ratios
from three samples by default. It also emits per-metric threshold checks and an
overall parity status so performance claims have an explicit evidence gate.
Both benchmark lanes precompute record keys before measuring public cache
reads and writes, matching the C# reference benchmark shape instead of
charging one runtime for key construction inside the hot path.
The Python in-memory record owner is type-partitioned so public `get(...)`
reads avoid per-call composite-key allocation on the hot path.

View file

@ -460,7 +460,9 @@ registered cache, and public `CultCache` upsert/get throughput. The C# compare
command also runs `packages/cultcache-py/tools/GameCult.Caching.Benchmark` and
reports median Python-to-C# ratios for the shared public cache operations. It
uses three samples by default; pass `--samples` when you need a different
evidence shape. The public in-memory cache owner stores records in
evidence shape. Both runtimes precompute record keys before measured cache
upsert/get loops so the comparison is about public cache access, not string
construction noise. The public in-memory cache owner stores records in
type-partitioned dictionaries so hot `get(...)` calls avoid per-call
composite-key allocation. Treat the benchmark numbers as a local baseline, not
a claim that Python matches the C# reference in every workload.

View file

@ -46,6 +46,7 @@ def run_benchmark(records: int) -> dict[str, Any]:
cls=BenchItem,
)
values = [BenchItem(f"item-{index}", f"cat-{index % 8}", index) for index in range(records)]
keys = [f"item:{index}" for index in range(records)]
encoded_payloads, encode_metric = measure("database_entry_encode", records, lambda: [
document.encode_payload(value)
@ -58,7 +59,7 @@ def run_benchmark(records: int) -> dict[str, Any]:
wire_messages, frame_metric = measure("cultnet_frame_parse", records, lambda: [
parse_message(decode_frame(encode_frame(document_put_raw(
message_id=f"put-{index}",
key=f"item:{index}",
key=keys[index],
schema_id=document.catalog_entry().schema_id,
stored_at="2026-06-13T00:00:00Z",
payload=encoded_payloads[index],
@ -75,7 +76,7 @@ def run_benchmark(records: int) -> dict[str, Any]:
"documents": [
{
"schemaId": document.catalog_entry().schema_id,
"recordKey": f"item:{index}",
"recordKey": keys[index],
"storedAt": "2026-06-13T00:00:00Z",
"payloadEncoding": "messagepack",
"payload": encoded_payloads[index],
@ -91,11 +92,11 @@ def run_benchmark(records: int) -> dict[str, Any]:
upsert_cache = CultCache()
upsert_cache.register_document_type(document)
_, upsert_metric = measure("cache_upsert", records, lambda: [
upsert_cache.put(document, f"item:{index}", values[index])
upsert_cache.put(document, keys[index], values[index])
for index in range(records)
])
_, get_metric = measure("cache_get", records, lambda: [
upsert_cache.get(document, f"item:{index}")
upsert_cache.get(document, keys[index])
for index in range(records)
])

View file

@ -384,6 +384,25 @@ class CultCacheTests(unittest.TestCase):
self.assertEqual(_parity_status([by_name["cache_upsert"]], "ok"), "meets-threshold")
self.assertEqual(_parity_status(comparison, "failed"), "unknown")
def test_compare_csharp_marks_meets_threshold_when_all_common_metrics_pass(self) -> None:
comparison = _compare_common_metrics(
{
"metrics": [
{"name": "cache_get", "operations": 3, "elapsedMs": 1.0, "opsPerSecond": 95.0},
{"name": "cache_upsert", "operations": 3, "elapsedMs": 1.0, "opsPerSecond": 120.0},
],
},
{
"metrics": [
{"name": "cache_get", "operations": 3, "elapsedMs": 1.0, "opsPerSecond": 100.0},
{"name": "cache_upsert", "operations": 3, "elapsedMs": 1.0, "opsPerSecond": 100.0},
],
},
parity_threshold=0.90,
)
self.assertEqual(_parity_status(comparison, "ok"), "meets-threshold")
def test_cache_put_without_backing_store_keeps_in_memory_value(self) -> None:
document = define_database_entry_type(
"bench.memory_item",