Files
CBDD/BENCHMARKS.md
Joseph Doherty 3ffd468c79
All checks were successful
NuGet Publish / build-and-pack (push) Successful in 45s
NuGet Publish / publish-to-gitea (push) Successful in 52s
Fix audit findings for coverage, architecture checks, and XML docs
2026-02-20 15:43:25 -05:00

223 lines
7.9 KiB
Markdown
Executable File

# CBDD Performance Benchmarks
> **Last Updated:** February 20, 2026
> **Platform:** Windows 11, Intel Core i7-13800H (14 cores), .NET 10.0
---
## Overview
CBDD is designed for **zero-allocation, high-performance** document operations. These benchmarks compare CBDD against SQLite using BenchmarkDotNet with identical workloads.
### Key Takeaways
**8.2x faster** single inserts vs SQLite
**2.4x faster** serialization (BSON vs JSON)
**2.1x faster** deserialization
**Zero allocations** for BSON serialization
**43% faster** bulk insert (1000 docs)
---
## Insert Performance
### Single Document Insert
| Database | Mean Time | Allocated Memory |
|:---------|----------:|-----------------:|
| **CBDD** | **355.8 μs** | 128.89 KB |
| SQLite | 2,916.3 μs | 6.67 KB |
**Speedup:** 8.2x faster
### Batch Insert (1000 Documents)
| Database | Mean Time | Notes |
|:---------|----------:|:------|
| **CBDD** | ~355 ms | Single transaction |
| SQLite | ~620 ms | WAL mode + checkpoint |
**Speedup:** 1.75x faster
> **⚠️ Important:** The "Allocated" metrics for SQLite only measure **managed .NET allocations**. SQLite's native C library allocates significant **unmanaged memory** that is **not captured** by BenchmarkDotNet. In reality, SQLite's total memory footprint is much higher than reported. CBDD's allocations are fully measured since it's 100% managed code.
---
## Serialization Performance
### Single Object
| Operation | BSON (CBDD) | JSON (System.Text.Json) | Speedup |
|:----------|-------------:|------------------------:|--------:|
| **Serialize** | **1.42 μs** | 3.43 μs | **2.4x** |
| **Deserialize** | **3.34 μs** | 7.01 μs | **2.1x** |
| Metric | BSON | JSON |
|:-------|-----:|-----:|
| Serialize Allocated | **0 B** | 1,880 B |
| Deserialize Allocated | 5,704 B | 6,600 B |
### Bulk (10,000 Objects)
| Operation | BSON (CBDD) | JSON | Speedup |
|:----------|-------------:|-----:|--------:|
| **Serialize** | **14.99 ms** | 21.40 ms | **1.43x** |
| **Deserialize** | **18.92 ms** | 42.96 ms | **2.27x** |
| Metric | BSON | JSON |
|:-------|-----:|-----:|
| Serialize Allocated | **0 B** | 19.19 MB |
| Deserialize Allocated | 57.98 MB | 66.94 MB |
---
## Architecture Highlights
### Why CBDD is Faster
1. **C-BSON Format** - Field name compression (2-byte IDs vs full strings)
2. **Zero-Copy I/O** - Direct `Span<byte>` operations
3. **Memory Pooling** - `ArrayPool` for buffer reuse
4. **Stack Allocation** - `stackalloc` for temporary buffers
5. **Source Generators** - Compile-time serialization code
### Memory Efficiency
- **Zero allocations** for BSON serialization (single object)
- **~70% less memory** for bulk deserialization vs JSON
- **No GC pressure** during write-heavy workloads
---
## Benchmark Environment
```
BenchmarkDotNet v0.15.8
OS: Windows 11 (10.0.22631.6345/23H2)
CPU: 13th Gen Intel Core i7-13800H @ 2.50GHz
- 14 physical cores, 20 logical cores
Runtime: .NET 10.0.2 (X64 RyuJIT x86-64-v3)
```
---
## Test Configuration
### Workload Profile
**Person Document Structure:**
- 10 employment history entries per document
- Nested address object
- Lists of tags (5 strings per entry)
- ObjectId, DateTime, Decimal types
- Total: ~150 fields per document
### Comparison Setup
| Database | Serialization | ORM | Journal Mode |
|:---------|:--------------|:----|:-------------|
| **CBDD** | C-BSON (custom) | Generated mappers | WAL |
| **SQLite** | System.Text.Json | Dapper | WAL + checkpoint |
---
## Running Benchmarks
```bash
# Clone repository
git clone https://github.com/EntglDb/CBDD.git
cd CBDD
# Run all benchmarks
dotnet run -c Release --project tests/CBDD.Tests.Benchmark
# Results will be in:
# BenchmarkDotNet.Artifacts/results/*.md
```
---
## Interpreting Results
- **μs (microseconds)** - Execution time (lower is better)
- **Allocated** - Memory allocated per operation
- **Gen0/Gen1** - Garbage collection counts
- **Ratio** - Performance relative to baseline (SQLite)
---
## Detailed Results
### Full Serialization Benchmark Output
```
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------- |--------------:|------------:|------------:|----------:|-----------:|
| 'Serialize Single (BSON)' | 1.415 μs | 0.0080 μs | 0.0071 μs | - | - |
| 'Serialize Single (JSON)' | 3.427 μs | 0.2013 μs | 0.5937 μs | 0.1488 | 1880 B |
| 'Deserialize Single (BSON)' | 3.338 μs | 0.0637 μs | 0.0708 μs | 0.4539 | 5704 B |
| 'Deserialize Single (JSON)' | 7.005 μs | 0.1555 μs | 0.4485 μs | 0.5188 | 6600 B |
| | | | | | |
| 'Serialize List 10k (BSON loop)' | 14,988.023 μs | 274.5623 μs | 256.8258 μs | - | - |
| 'Serialize List 10k (JSON loop)' | 21,398.339 μs | 198.2820 μs | 185.4731 μs | 1500.0000 | 19190787 B |
| 'Deserialize List 10k (BSON loop)' | 18,920.076 μs | 318.3235 μs | 297.7600 μs | 4593.7500 | 57984034 B |
| 'Deserialize List 10k (JSON loop)' | 42,961.024 μs | 534.7024 μs | 446.5008 μs | 5333.3333 | 66944224 B |
```
### Full Insert Benchmark Output
```
| Method | Mean | Error | StdDev | Ratio | Allocated |
|------------------------------------ |-----------:|----------:|----------:|------:|----------:|
| 'SQLite Single Insert (AutoCommit)' | 2,916.3 μs | 130.50 μs | 382.73 μs | 1.00 | 6.67 KB |
| 'DocumentDb Single Insert' | 355.8 μs | 19.42 μs | 56.65 μs | 0.12 | 128.89 KB |
```
---
## Performance Gate Evidence (compact_plan.md §11.4)
This section captures the required evidence for plan gate `11.4`:
- documented write/read overhead
- documented compaction throughput
- no obvious pathological GC spikes for compression-enabled workloads
### Repro Commands
```bash
# Benchmark smoke (write/read + serialization)
dotnet run -c Release --project tests/CBDD.Tests.Benchmark/ZB.MOM.WW.CBDD.Tests.Benchmark.csproj
# Performance gate smoke (compaction throughput + compression GC deltas)
dotnet run -c Release --project tests/CBDD.Tests.Benchmark/ZB.MOM.WW.CBDD.Tests.Benchmark.csproj -- gate
```
### Captured Artifacts
- `BenchmarkDotNet.Artifacts/results/ZB.MOM.WW.CBDD.Tests.Benchmark.InsertBenchmarks-report.csv`
- `BenchmarkDotNet.Artifacts/results/ZB.MOM.WW.CBDD.Tests.Benchmark.ReadBenchmarks-report.csv`
- `BenchmarkDotNet.Artifacts/results/PerformanceGateSmoke-report.json`
### Captured Values (2026-02-20 UTC)
| Metric | Value | Source |
|:--|--:|:--|
| CBDD Single Insert mean | 5,169.8 μs | InsertBenchmarks CSV |
| CBDD Batch Insert (1000) mean | 25,071.8 μs | InsertBenchmarks CSV |
| CBDD FindById mean | 8.963 μs | ReadBenchmarks CSV |
| CBDD FindById allocated | 36.23 KB | ReadBenchmarks CSV |
| Offline compaction throughput | 9,073,346.22 bytes/sec | PerformanceGateSmoke JSON |
| Offline compaction throughput | 944.26 pages/sec | PerformanceGateSmoke JSON |
| Offline compaction throughput | 2,136.21 docs/sec | PerformanceGateSmoke JSON |
| Compression OFF `Gen0/1/2` deltas | `30 / 4 / 2` | PerformanceGateSmoke JSON |
| Compression ON `Gen0/1/2` deltas | `26 / 4 / 2` | PerformanceGateSmoke JSON |
| Compression OFF allocated delta | 239,765,160 bytes | PerformanceGateSmoke JSON |
| Compression ON allocated delta | 205,801,624 bytes | PerformanceGateSmoke JSON |
### Gate Outcome
- Write/read overhead is documented from fresh benchmark smoke artifacts.
- Compaction throughput is documented with reproducible command and captured stats.
- Compression-enabled workload does not show pathological GC escalation in this smoke scenario (Gen1/Gen2 unchanged, Gen0 and allocated bytes lower than compression-off).