Files
2026-10-23 15:12:43 +03:00

169 lines
6.7 KiB
Python

"""Track 1: deterministic stale-fill race and measured reads on real PostgreSQL/Redis.
The unsafe cache exists ONLY in this experiment; API always uses versioned keys.
"""
import json
import math
import resource
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from uuid import uuid4
from sqlalchemy import delete, event, select
from sqlalchemy.orm import sessionmaker
from src.auth import sessions
from src.cache import HistoryCache
from src.database import create_db_engine
from src.models import User, Chat, ChatMember
from src.store import PostgresStore
from .fixtures import seed_messages
class NaiveCache(HistoryCache):
def key(self, chat_id, version, limit, after):
return super().key(chat_id, 0, limit, after)
class NoCache:
def get_or_load(self, chat_id, version, limit, after, load):
return load()
class QueryCounter:
lock = threading.Lock()
counts = {'sql': 0, 'history_selects': 0}
@classmethod
def record(cls, conn, cursor, statement, parameters, context, executemany):
with cls.lock:
cls.counts['sql'] += 1
normalized = ' '.join(statement.split()).lower()
if normalized.startswith('select ') and ' from messages ' in normalized:
cls.counts['history_selects'] += 1
@classmethod
def reset(cls):
with cls.lock:
cls.counts = {'sql': 0, 'history_selects': 0}
def make_store(engine, cache):
store = PostgresStore(cache)
store.sessions = sessionmaker(engine, expire_on_commit=False)
return store
def stale_race(reader, writer, actor_id, chat_id, naive=False):
"""Pause AFTER the real history SELECT, commit a write, then finish cache SET."""
loaded, resume = threading.Event(), threading.Event()
original_load = reader._load_messages
def paused_load(*args):
page = original_load(*args)
loaded.set()
if not resume.wait(10):
raise AssertionError('Race writer did not finish')
return page
reader._load_messages = paused_load
try:
with ThreadPoolExecutor(max_workers=1) as executor:
pending = executor.submit(reader.list_messages, chat_id, actor_id, 100, None)
try:
assert loaded.wait(10), 'Reader did not reach the cache-fill barrier'
message = writer.create_message(chat_id, actor_id, 'committed during cache fill')
if naive:
reader.cache.client.delete(reader.cache.key(chat_id, 0, 100, None))
finally:
resume.set()
overlapping_page = pending.result(timeout=10)
finally:
reader._load_messages = original_load
assert message['id'] not in {item['id'] for item in overlapping_page['items']}
started = time.perf_counter()
page = reader.list_messages(chat_id, actor_id, 100, None)
return {'stale': message['id'] not in {item['id'] for item in page['items']},
'read_after_write_ms': round((time.perf_counter() - started) * 1000, 3)}
def benchmark(store, actor_id, chat_id, requests=300, concurrency=8):
if isinstance(store.cache, HistoryCache):
for key in store.cache.client.scan_iter(match=f'{store.cache.prefix}:{chat_id}:*'):
store.cache.client.delete(key)
expected = store.list_messages(chat_id, actor_id, 100, None)
QueryCounter.reset()
started = time.perf_counter()
cpu_started = time.process_time()
def read(_):
tick = time.perf_counter()
page = store.list_messages(chat_id, actor_id, 100, None)
assert page == expected
return (time.perf_counter() - tick) * 1000
with ThreadPoolExecutor(max_workers=concurrency) as executor:
latency = sorted(executor.map(read, range(requests)))
elapsed = time.perf_counter() - started
result = {
'requests': requests, 'concurrency': concurrency, 'errors': 0,
'seconds': round(elapsed, 3), 'successful_rps': round(requests / elapsed, 2),
'client_cpu_seconds': round(time.process_time() - cpu_started, 3),
'client_peak_rss_mib': round(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, 2),
**QueryCounter.counts,
}
for percentile in (50, 95, 99):
result[f'p{percentile}_ms'] = round(latency[math.ceil(len(latency) * percentile / 100) - 1], 3)
return result
def main():
prefix = 'experiment:' + uuid4().hex
engine = create_db_engine()
event.listen(engine, 'before_cursor_execute', QueryCounter.record)
writer = make_store(engine, NoCache())
user = writer.create_user('exp_' + uuid4().hex[:12], 'Experiment', 'Synthetic-password-123')
result = {'race_repeats': 5, 'race': {}, 'benchmark': {}, 'messages': 10000,
'page_size': 100, 'warmup_reads_per_run': 1,
'scope': 'store calls; real SQL and Redis; excludes HTTP, sessions and KDF; SQL excludes BEGIN/COMMIT'}
try:
for name, cache_type in [('naive', NaiveCache), ('versioned', HistoryCache)]:
results = []
for n in range(5):
chat = writer.create_chat(user['id'], 'Race', [user['id']])
reader = make_store(engine, cache_type(sessions.client, prefix=f'{prefix}:{name}:{n}'))
QueryCounter.reset()
row = stale_race(reader, writer, user['id'], chat['id'], naive=name == 'naive')
row.update(QueryCounter.counts)
assert row['stale'] == (name == 'naive')
results.append(row)
result['race'][name] = results
chat = writer.create_chat(user['id'], 'Benchmark', [user['id']])
with writer.sessions.begin() as session:
seed_messages(session, chat['id'], user['id'])
modes = {
'uncached': make_store(engine, NoCache()),
'naive': make_store(engine, NaiveCache(sessions.client, prefix=prefix + ':bench-naive')),
'versioned': make_store(engine, HistoryCache(sessions.client, prefix=prefix + ':bench-versioned')),
}
result['benchmark'] = {name: [] for name in modes}
names = list(modes)
for repeat in range(3):
for name in names[repeat:] + names[:repeat]:
result['benchmark'][name].append(benchmark(modes[name], user['id'], chat['id']))
print(json.dumps(result, ensure_ascii=False, indent=2))
finally:
for key in sessions.client.scan_iter(match=prefix + ':*'):
sessions.client.delete(key)
with writer.sessions.begin() as session:
session.execute(delete(Chat).where(Chat.id.in_(
select(ChatMember.chat_id).where(ChatMember.user_id == user['id']))))
session.execute(delete(User).where(User.id == user['id']))
engine.dispose()
if __name__ == '__main__':
main()