|
| 1 | +""" |
| 2 | +Medium stress test - 15 workers, 30 seconds |
| 3 | +""" |
| 4 | +import asyncio |
| 5 | +import threading |
| 6 | +import time |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +sys.path.insert(0, str(Path(__file__).parent / "src")) |
| 11 | +from agents.memory.sqlite_session import SQLiteSession |
| 12 | + |
| 13 | + |
| 14 | +class Stats: |
| 15 | + def __init__(self): |
| 16 | + self.lock = threading.Lock() |
| 17 | + self.writes = 0 |
| 18 | + self.reads = 0 |
| 19 | + self.errors = [] |
| 20 | + |
| 21 | + def record_write(self): |
| 22 | + with self.lock: |
| 23 | + self.writes += 1 |
| 24 | + |
| 25 | + def record_read(self): |
| 26 | + with self.lock: |
| 27 | + self.reads += 1 |
| 28 | + |
| 29 | + def record_error(self, msg): |
| 30 | + with self.lock: |
| 31 | + self.errors.append(msg) |
| 32 | + |
| 33 | + |
| 34 | +async def writer(session_id, db_path, worker_id, duration, stats): |
| 35 | + """Write for duration seconds""" |
| 36 | + session = SQLiteSession(session_id=session_id, db_path=db_path) |
| 37 | + end_time = time.time() + duration |
| 38 | + count = 0 |
| 39 | + |
| 40 | + while time.time() < end_time: |
| 41 | + try: |
| 42 | + await session.add_items([{ |
| 43 | + "role": "user", |
| 44 | + "content": f"w{worker_id}_m{count}", |
| 45 | + "type": "message" |
| 46 | + }]) |
| 47 | + stats.record_write() |
| 48 | + count += 1 |
| 49 | + if count % 10 == 0: |
| 50 | + await asyncio.sleep(0.001) |
| 51 | + except Exception as e: |
| 52 | + stats.record_error(f"Writer {worker_id}: {e}") |
| 53 | + |
| 54 | + |
| 55 | +async def reader(session_id, db_path, worker_id, duration, stats): |
| 56 | + """Read for duration seconds""" |
| 57 | + session = SQLiteSession(session_id=session_id, db_path=db_path) |
| 58 | + end_time = time.time() + duration |
| 59 | + |
| 60 | + while time.time() < end_time: |
| 61 | + try: |
| 62 | + await session.get_items() |
| 63 | + stats.record_read() |
| 64 | + await asyncio.sleep(0.01) |
| 65 | + except Exception as e: |
| 66 | + stats.record_error(f"Reader {worker_id}: {e}") |
| 67 | + |
| 68 | + |
| 69 | +async def main(): |
| 70 | + db_path = Path("medium_test.db") |
| 71 | + if db_path.exists(): |
| 72 | + db_path.unlink() |
| 73 | + |
| 74 | + SESSION_ID = "test" |
| 75 | + DURATION = 60 |
| 76 | + stats = Stats() |
| 77 | + |
| 78 | + print(f"Comprehensive stress test: 30 writers + 10 readers, {DURATION} seconds") |
| 79 | + |
| 80 | + # Initialize DB |
| 81 | + s = SQLiteSession(session_id=SESSION_ID, db_path=db_path) |
| 82 | + await s.add_items([{"role": "system", "content": "init", "type": "message"}]) |
| 83 | + await s.clear_session() |
| 84 | + print("Database initialized\n") |
| 85 | + |
| 86 | + # Create workers |
| 87 | + tasks = [] |
| 88 | + for i in range(30): |
| 89 | + tasks.append(writer(SESSION_ID, db_path, i, DURATION, stats)) |
| 90 | + for i in range(10): |
| 91 | + tasks.append(reader(SESSION_ID, db_path, i, DURATION, stats)) |
| 92 | + |
| 93 | + print(f"Starting {len(tasks)} workers...\n") |
| 94 | + start = time.time() |
| 95 | + |
| 96 | + # Run with progress updates |
| 97 | + async def report(): |
| 98 | + for i in range(6): |
| 99 | + await asyncio.sleep(10) |
| 100 | + elapsed = time.time() - start |
| 101 | + writes_per_sec = stats.writes / elapsed if elapsed > 0 else 0 |
| 102 | + print(f"[{elapsed:.0f}s] Writes: {stats.writes:,} ({writes_per_sec:.0f}/s), Reads: {stats.reads:,}, Errors: {len(stats.errors)}") |
| 103 | + |
| 104 | + tasks.append(report()) |
| 105 | + await asyncio.gather(*tasks) |
| 106 | + |
| 107 | + duration = time.time() - start |
| 108 | + |
| 109 | + # Final results |
| 110 | + print(f"\n{'='*60}") |
| 111 | + print(f"Duration: {duration:.1f}s") |
| 112 | + print(f"Writes: {stats.writes:,}") |
| 113 | + print(f"Reads: {stats.reads:,}") |
| 114 | + print(f"Errors: {len(stats.errors)}") |
| 115 | + |
| 116 | + if stats.errors: |
| 117 | + print(f"\nFirst errors:") |
| 118 | + for err in stats.errors[:5]: |
| 119 | + print(f" {err}") |
| 120 | + |
| 121 | + # Verify |
| 122 | + final = await s.get_items() |
| 123 | + print(f"\nExpected items: {stats.writes}") |
| 124 | + print(f"Actual items: {len(final)}") |
| 125 | + print(f"Match: {'✅ PASS' if stats.writes == len(final) else '❌ FAIL'}") |
| 126 | + |
| 127 | + success = stats.writes == len(final) and len(stats.errors) == 0 |
| 128 | + return 0 if success else 1 |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + sys.exit(asyncio.run(main())) |
0 commit comments