repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources15d ago| #1 | """ |
| #2 | Comprehensive Mnemosyne BEAM Benchmark |
| #3 | ====================================== |
| #4 | Run: PYTHONPATH=. python tests/benchmark_beam_comprehensive.py |
| #5 | """ |
| #6 | |
| #7 | import time |
| #8 | import tempfile |
| #9 | import sqlite3 |
| #10 | from datetime import datetime, timedelta |
| #11 | from pathlib import Path |
| #12 | |
| #13 | from mnemosyne.core.beam import BeamMemory, init_beam |
| #14 | from mnemosyne.core.memory import Mnemosyne |
| #15 | |
| #16 | |
| #17 | def benchmark_writes(beam: BeamMemory, n: int = 500): |
| #18 | print(f"\n📝 Write Benchmark ({n} working memories)") |
| #19 | t0 = time.time() |
| #20 | for i in range(n): |
| #21 | beam.remember(f"Task {i}: evaluate model checkpoint {i * 13}", source="conversation", importance=0.5) |
| #22 | total_ms = (time.time() - t0) * 1000 |
| #23 | avg_ms = total_ms / n |
| #24 | print(f" Total: {total_ms:.1f} ms | Avg: {avg_ms:.3f} ms | Throughput: {n / (total_ms / 1000):.0f} ops/sec") |
| #25 | |
| #26 | |
| #27 | def benchmark_episodic_insert(beam: BeamMemory, n: int = 500): |
| #28 | print(f"\n🗄️ Episodic Insert Benchmark ({n} summaries)") |
| #29 | t0 = time.time() |
| #30 | for i in range(n): |
| #31 | beam.consolidate_to_episodic( |
| #32 | summary=f"Project Alpha milestone {i}: shipped feature {i * 7}", |
| #33 | source_wm_ids=[f"wm_{i}"], |
| #34 | importance=0.6 |
| #35 | ) |
| #36 | total_ms = (time.time() - t0) * 1000 |
| #37 | avg_ms = total_ms / n |
| #38 | print(f" Total: {total_ms:.1f} ms | Avg: {avg_ms:.3f} ms | Throughput: {n / (total_ms / 1000):.0f} ops/sec") |
| #39 | |
| #40 | |
| #41 | def benchmark_recall_scaling(beam: BeamMemory): |
| #42 | print(f"\n🔍 Hybrid Recall Scaling") |
| #43 | queries = [ |
| #44 | "shipped feature", |
| #45 | "milestone 250", |
| #46 | "project alpha", |
| #47 | "completely unrelated query", |
| #48 | ] |
| #49 | for q in queries: |
| #50 | times = [] |
| #51 | for _ in range(20): |
| #52 | t0 = time.time() |
| #53 | results = beam.recall(q, top_k=5) |
| #54 | times.append((time.time() - t0) * 1000) |
| #55 | avg = sum(times) / len(times) |
| #56 | p95 = sorted(times)[int(len(times) * 0.95)] |
| #57 | print(f" '{q[:30]:<30}' | {avg:.2f} ms avg | {p95:.2f} ms p95 | {len(results)} results") |
| #58 | |
| #59 | |
| #60 | def benchmark_sleep(beam: BeamMemory, n_old: int = 300): |
| #61 | print(f"\n😴 Sleep Benchmark ({n_old} old working memories)") |
| #62 | conn = sqlite3.connect(beam.db_path) |
| #63 | old_ts = (datetime.now() - timedelta(hours=20)).isoformat() |
| #64 | for i in range(n_old): |
| #65 | conn.execute( |
| #66 | "INSERT INTO working_memory (id, content, source, timestamp, session_id, importance) VALUES (?, ?, ?, ?, ?, ?)", |
| #67 | (f"old{i}", f"Old task content number {i}", "conversation", old_ts, beam.session_id, 0.5) |
| #68 | ) |
| #69 | conn.commit() |
| #70 | conn.close() |
| #71 | |
| #72 | t0 = time.time() |
| #73 | result = beam.sleep(dry_run=False) |
| #74 | total_ms = (time.time() - t0) * 1000 |
| #75 | print(f" Sleep took: {total_ms:.1f} ms | consolidated: {result.get('items_consolidated', 0)} items | summaries: {result.get('summaries_created', 0)}") |
| #76 | |
| #77 | |
| #78 | def benchmark_legacy_vs_beam(db_path: Path): |
| #79 | print(f"\n⚔️ Legacy Flat Scan vs BEAM Hybrid") |
| #80 | mem = Mnemosyne(session_id="legacy_test", db_path=db_path) |
| #81 | |
| #82 | # Populate legacy memories |
| #83 | n = 500 |
| #84 | print(f" Populating {n} legacy memories...") |
| #85 | for i in range(n): |
| #86 | mem.remember(f"Legacy memory number {i} about machine learning", source="conversation", importance=0.5) |
| #87 | |
| #88 | # Time legacy flat recall (the old approach: get last 1000, score in Python) |
| #89 | conn = sqlite3.connect(db_path) |
| #90 | conn.row_factory = sqlite3.Row |
| #91 | t0 = time.time() |
| #92 | rows = conn.execute( |
| #93 | "SELECT id, content, importance FROM memories WHERE session_id = ? ORDER BY timestamp DESC LIMIT 1000", |
| #94 | ("legacy_test",) |
| #95 | ).fetchall() |
| #96 | query_words = ["machine", "learning"] |
| #97 | for row in rows: |
| #98 | content = row["content"].lower() |
| #99 | exact = sum(1 for w in query_words if w in content) |
| #100 | score = exact / len(query_words) |
| #101 | legacy_ms = (time.time() - t0) * 1000 |
| #102 | |
| #103 | # Time BEAM recall |
| #104 | t0 = time.time() |
| #105 | beam_results = mem.recall("machine learning") |
| #106 | beam_ms = (time.time() - t0) * 1000 |
| #107 | |
| #108 | print(f" Legacy flat scan ({n} rows): {legacy_ms:.2f} ms") |
| #109 | print(f" BEAM hybrid recall: {beam_ms:.2f} ms") |
| #110 | print(f" Speedup: {legacy_ms / max(beam_ms, 0.1):.1f}x") |
| #111 | |
| #112 | |
| #113 | def benchmark_scratchpad(beam: BeamMemory): |
| #114 | print(f"\n📝 Scratchpad Benchmark") |
| #115 | t0 = time.time() |
| #116 | for i in range(100): |
| #117 | beam.scratchpad_write(f"Scratch note {i}: compute loss function") |
| #118 | write_ms = (time.time() - t0) * 1000 |
| #119 | avg_write = write_ms / 100 |
| #120 | |
| #121 | t0 = time.time() |
| #122 | entries = beam.scratchpad_read() |
| #123 | read_ms = (time.time() - t0) * 1000 |
| #124 | |
| #125 | print(f" Avg write: {avg_write:.3f} ms | Read {len(entries)} entries: {read_ms:.2f} ms") |
| #126 | |
| #127 | |
| #128 | def main(): |
| #129 | with tempfile.TemporaryDirectory() as tmpdir: |
| #130 | db_path = Path(tmpdir) / "bench.db" |
| #131 | print("=" * 60) |
| #132 | print("Mnemosyne BEAM Comprehensive Benchmark") |
| #133 | print("=" * 60) |
| #134 | |
| #135 | beam = BeamMemory(session_id="bench", db_path=db_path) |
| #136 | |
| #137 | benchmark_writes(beam, n=500) |
| #138 | benchmark_episodic_insert(beam, n=500) |
| #139 | benchmark_recall_scaling(beam) |
| #140 | benchmark_sleep(beam, n_old=300) |
| #141 | benchmark_scratchpad(beam) |
| #142 | benchmark_legacy_vs_beam(db_path) |
| #143 | |
| #144 | stats = beam.get_working_stats() |
| #145 | ep_stats = beam.get_episodic_stats() |
| #146 | print(f"\n📊 Final State") |
| #147 | print(f" Working memory: {stats['total']} items") |
| #148 | print(f" Episodic memory: {ep_stats['total']} items | vectors: {ep_stats['vectors']}") |
| #149 | print("=" * 60) |
| #150 | |
| #151 | |
| #152 | if __name__ == "__main__": |
| #153 | main() |
| #154 |