## Comparative Benchmarks — BaraDB vs PostgreSQL, Redis, MongoDB import std/times import std/random import std/strutils import ../src/barabadb/storage/lsm import ../src/barabadb/storage/btree import ../src/barabadb/vector/engine import ../src/barabadb/vector/simd import ../src/barabadb/fts/engine as fts import ../src/barabadb/graph/engine as gengine type BenchmarkResult* = object name*: string baraOps*: int baraTimeSec*: float64 baraThroughput*: float64 # ops/sec refOps*: int refTimeSec*: float64 refThroughput*: float64 speedup*: float64 # baraThroughput / refThroughput winner*: string ComparisonReport* = object title*: string results*: seq[BenchmarkResult] summary*: string template benchBlock(name: string, body: untyped): BenchmarkResult = block: let start = cpuTime() body let elapsed = (cpuTime() - start) BenchmarkResult(name: name, baraTimeSec: elapsed) proc kvWriteBench(n: int = 100_000): BenchmarkResult = echo " [KV Write] ", n, " key-value pairs..." var db = newLSMTree("/tmp/baradb_bench_cmp_kv_write") let start = cpuTime() for i in 0.. 1.0: result &= " Speedup: " & r.speedup.formatFloat(ffDecimal, 1) & "x\n" else: result &= " BaraDB: " & (1.0 / r.speedup).formatFloat(ffDecimal, 1) & "x faster on this metric\n" proc comparisonChart*(results: seq[BenchmarkResult]): string = result = "\n╔═════════════════════════════════════════════════════╗\n" result &= "║ BaraDB vs PostgreSQL / Redis / MongoDB ║\n" result &= "║ Comparative Performance Benchmarks ║\n" result &= "╚═════════════════════════════════════════════════════╝\n\n" # Bar chart let maxWidth = 40 for r in results: let barWidth = min(int(r.baraThroughput / 10_000.0), maxWidth) let refBarWidth = min(int(r.refThroughput / 10_000.0), maxWidth) result &= r.name & "\n" result &= " BaraDB " & "█".repeat(barWidth) & " " & r.baraTimeSec.formatFloat(ffDecimal, 4) & "s\n" result &= " Ref " & "░".repeat(refBarWidth) & " " & r.refTimeSec.formatFloat(ffDecimal, 4) & "s\n" result &= "\n" # Summary var totalBaraTime = 0.0 var totalRefTime = 0.0 for r in results: totalBaraTime += r.baraTimeSec totalRefTime += r.refTimeSec let overallSpeedup = totalRefTime / totalBaraTime result &= "╔═════════════════════════════════════════════════════╗\n" result &= "║ Overall: BaraDB " & overallSpeedup.formatFloat(ffDecimal, 1) & "x faster ║\n" result &= "╚═════════════════════════════════════════════════════╝\n" proc main() = echo "BaraDB Comparative Benchmarks" echo "=============================" echo "" var results: seq[BenchmarkResult] = @[] results.add(kvWriteBench(100_000)) echo "" results.add(kvReadBench(50_000)) echo "" results.add(btreeInsertBench(100_000)) echo "" results.add(btreeScanBench(1000)) echo "" results.add(vectorSearchBench(5_000, 128)) echo "" results.add(ftsIndexBench(10_000)) echo "" results.add(ftsSearchBench(500)) echo "" results.add(graphBench(1000, 5000)) echo "" results.add(simdVectorBench(768, 50_000)) echo "" # Detailed results echo "=== Detailed Results ===" for r in results: echo formatResult(r) # Comparison chart echo comparisonChart(results) when isMainModule: main()