Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ package-lock.json

# Project specific
bench_results/
benchmark_results/
perf_data/
*.log
*.tmp
*.temp
*.prof
*.pprof
perf.data*
*.flamegraph

# OS specific
Thumbs.db
Expand Down
250 changes: 250 additions & 0 deletions BENCHMARKING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
# Abyssbook Benchmarking Methodology

## Overview

This document describes the comprehensive benchmarking methodology for the Abyssbook orderbook implementation, ensuring reproducible, statistically valid performance measurements across different environments and configurations.

## Benchmark Architecture

### Core Metrics

1. **Latency Measurements**
- Average execution time (µs)
- Percentile latencies (P50, P95, P99, P99.9)
- Jitter and variance analysis

2. **Throughput Measurements**
- Operations per second (ops/sec)
- Orders per second for different operation types
- Sustained throughput under load

3. **Memory Performance**
- Memory footprint per operation
- Cache hit/miss ratios
- Memory bandwidth utilization

4. **Scalability Metrics**
- Performance vs. number of shards
- Performance vs. order book depth
- Performance vs. concurrent operations

### Benchmark Categories

#### 1. Core Operations
- **Place Orders**: Limit order insertion at various price levels
- **Cancel Orders**: Order cancellation and price level cleanup
- **Market Orders**: Immediate execution against existing liquidity
- **Price Level Updates**: Bulk operations at same price level

#### 2. Advanced Order Types
- **Stop Orders**: Conditional order triggering
- **Iceberg Orders**: Large order management with display amounts
- **TWAP Orders**: Time-weighted average price execution
- **Peg Orders**: Dynamic price-following orders

#### 3. Stress Testing
- **Burst Patterns**: High-frequency order submission spikes
- **Mixed Workloads**: Realistic trading pattern simulation
- **Price Level Stress**: Many price levels with few orders each
- **Large Market Orders**: Cross multiple price levels

#### 4. System Integration
- **Blockchain Integration**: Onchain data synchronization
- **Cache Performance**: Multi-level caching efficiency
- **Memory Management**: Allocation and cleanup patterns

## Statistical Methodology

### Sample Collection
```zig
// Statistical sampling approach for large iteration counts
const sample_size = @min(iterations, 10_000);
const sample_interval = @max(1, iterations / sample_size);

// Collect samples at regular intervals to reduce memory usage
if (i % sample_interval == 0) {
try latencies.append(elapsed);
}
```

### Percentile Calculation
- **P50 (Median)**: 50th percentile - typical performance
- **P95**: 95th percentile - good service level target
- **P99**: 99th percentile - tail latency analysis
- **P99.9**: 99.9th percentile - extreme outlier detection

### Environment Detection
```zig
// Adaptive configuration based on environment
fn getConfig() BenchmarkConfig {
const ci_env = std.process.getEnvVarOwned(allocator, "CI") catch null;
const github_actions = std.process.getEnvVarOwned(allocator, "GITHUB_ACTIONS") catch null;

if (ci_env != null or github_actions != null) {
return BenchmarkConfig.forCI();
}
return BenchmarkConfig{};
}
```

## Benchmark Configuration

### Default Configuration
```zig
const BenchmarkConfig = struct {
num_shards: usize = 32,
iterations: usize = 100_000,
order_count: usize = 10_000,
price_range: u64 = 1000,
amount_range: u64 = 100,
burst_size: usize = 1000,
num_price_levels: usize = 100,
};
```

### CI-Optimized Configuration
```zig
// Reduced parameters for CI environments
const ci_config = BenchmarkConfig{
.num_shards = 4,
.iterations = 1_000,
.order_count = 1_000,
.price_range = 100,
.amount_range = 50,
.burst_size = 100,
.num_price_levels = 20,
};
```

## Running Benchmarks

### Local Development
```bash
# Build and run benchmarks
zig build bench

# Expected output format:
# Operation Avg (µs) P50 (µs) P95 (µs) P99 (µs) Ops/sec Total (ms)
# Place Orders 0.85 0.75 1.20 2.10 1176471 85.0
```

### Continuous Integration
Benchmarks automatically detect CI environments and use optimized parameters to ensure stable execution within resource constraints.

### Performance Regression Detection
```bash
# Run comparative benchmarks
zig build bench > current_results.txt
git checkout baseline
zig build bench > baseline_results.txt

# Compare results (implementation needed)
./scripts/compare_benchmarks.py baseline_results.txt current_results.txt
```

## Validation Methodology

### Empirical Validation vs. AI Estimates
1. **Baseline Establishment**: Run benchmarks on known configurations
2. **Cross-Validation**: Compare results across different hardware
3. **Repeatability Testing**: Multiple runs with statistical analysis
4. **Load Testing**: Validate performance under sustained load

### Hardware Profiling
```bash
# CPU performance counters (Linux)
perf stat -e cache-misses,cache-references,instructions,cycles zig build bench

# Memory profiling
valgrind --tool=massif --time-unit=B zig build bench

# Cache analysis
perf record -e cache-misses zig build bench
perf report
```

## Optimization Tracking

### Data Structure Performance
- **HashMap vs TreeMap**: Order storage performance comparison
- **Cache Alignment**: 64-byte alignment impact measurement
- **SIMD Utilization**: Vector operation effectiveness
- **Memory Layout**: Struct-of-arrays vs array-of-structs analysis

### Caching Strategy Validation
- **Hit Ratios**: L1/L2/L3 cache effectiveness
- **Prefetching**: Hardware prefetch utilization
- **Working Set**: Memory footprint optimization
- **Eviction Policies**: Cache replacement strategy effectiveness

## Expected Performance Targets

### Latency Targets (x86_64, 3.0GHz, 32GB RAM)
- **Place Order**: P50 < 1µs, P99 < 5µs
- **Cancel Order**: P50 < 0.8µs, P99 < 4µs
- **Market Order**: P50 < 2µs, P99 < 10µs
- **Bulk Operations**: P50 < 0.5µs per order, P99 < 3µs per order

### Throughput Targets
- **Sustained Load**: 1M+ orders/second
- **Burst Capacity**: 5M+ orders/second (short duration)
- **Mixed Workload**: 800K+ operations/second
- **Memory Efficiency**: < 1KB per active order

### Scalability Targets
- **Linear Scaling**: Up to CPU core count
- **Memory Usage**: O(n) with active orders
- **Cache Efficiency**: 95%+ hit ratio for hot data

## Benchmark Data Analysis

### Statistical Significance
- Minimum 1000 samples for percentile calculations
- Confidence intervals for mean measurements
- Outlier detection and filtering (beyond 3 standard deviations)
- Warmup periods to eliminate JIT/allocation effects

### Result Interpretation
```zig
// Example benchmark result interpretation
const BenchmarkResult = struct {
operation: []const u8,
iterations: usize,
total_time_ns: u64,
avg_time_ns: u64,
throughput: f64,
latency_p50: u64,
latency_p95: u64,
latency_p99: u64,

pub fn isWithinTarget(self: *const BenchmarkResult, target: PerformanceTarget) bool {
return self.latency_p99 <= target.max_p99_latency_ns and
self.throughput >= target.min_throughput_ops_sec;
}
};
```

## Future Enhancements

### Planned Improvements
1. **Automated Regression Detection**: CI integration with performance alerts
2. **Hardware-Specific Tuning**: Auto-detection and optimization
3. **Load Pattern Analysis**: Real-world trading pattern simulation
4. **Memory Pool Optimization**: Custom allocation strategies
5. **Network Latency Simulation**: Distributed system performance testing

### Research Areas
1. **Lock-Free Data Structures**: Evaluate CAS-based implementations
2. **NUMA Optimization**: Multi-socket system performance
3. **GPU Acceleration**: Parallel matching algorithm exploration
4. **Persistent Memory**: Storage-class memory integration

## Conclusion

This benchmarking methodology ensures that Abyssbook performance measurements are:
- **Reproducible**: Consistent results across environments
- **Statistically Valid**: Proper sampling and analysis techniques
- **Comprehensive**: Coverage of all critical performance aspects
- **Actionable**: Clear targets and optimization guidance

Regular benchmark execution and analysis will drive continuous performance improvements while maintaining system reliability and correctness.
55 changes: 55 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,54 @@ pub fn build(b: *std.Build) void {
const bench_cmd = b.addRunArtifact(bench_exe);
const bench_step = b.step("bench", "Run benchmarks");
bench_step.dependOn(&bench_cmd.step);

// Add profiler executable
const profiler_exe = b.addExecutable(.{
.name = "profiler",
.root_source_file = .{ .cwd_relative = "src/profiler.zig" },
.target = target,
.optimize = .ReleaseFast,
});

const profiler_cmd = b.addRunArtifact(profiler_exe);
const profiler_step = b.step("profile", "Run performance profiler");
profiler_step.dependOn(&profiler_cmd.step);

// Add load test executable
const load_test_exe = b.addExecutable(.{
.name = "load_test",
.root_source_file = .{ .cwd_relative = "src/load_test.zig" },
.target = target,
.optimize = .ReleaseFast,
});

const load_test_cmd = b.addRunArtifact(load_test_exe);
const load_test_step = b.step("load-test", "Run load testing");
load_test_step.dependOn(&load_test_cmd.step);

// Add regression test executable
const regression_test_exe = b.addExecutable(.{
.name = "regression_test",
.root_source_file = .{ .cwd_relative = "src/regression_test.zig" },
.target = target,
.optimize = .ReleaseFast,
});

const regression_test_cmd = b.addRunArtifact(regression_test_exe);
const regression_test_step = b.step("regression-test", "Run performance regression tests");
regression_test_step.dependOn(&regression_test_cmd.step);

// Add metrics reporter test executable
const metrics_reporter_exe = b.addExecutable(.{
.name = "metrics_reporter",
.root_source_file = .{ .cwd_relative = "src/metrics_reporter.zig" },
.target = target,
.optimize = .ReleaseFast,
});

const metrics_reporter_cmd = b.addRunArtifact(metrics_reporter_exe);
const metrics_reporter_step = b.step("test-metrics", "Test metrics reporting formats");
metrics_reporter_step.dependOn(&metrics_reporter_cmd.step);

// Unit tests
const unit_tests = b.addTest(.{
Expand Down Expand Up @@ -88,4 +136,11 @@ pub fn build(b: *std.Build) void {
all_tests_step.dependOn(&run_e2e_tests.step);
all_tests_step.dependOn(&run_security_tests.step);
all_tests_step.dependOn(&run_blockchain_security_tests.step);

// Performance testing step
const perf_test_step = b.step("perf-test", "Run all performance tests (benchmarks, profiling, load test)");
perf_test_step.dependOn(&bench_cmd.step);
perf_test_step.dependOn(&profiler_cmd.step);
perf_test_step.dependOn(&load_test_cmd.step);
perf_test_step.dependOn(&regression_test_cmd.step);
}
Loading