- CPU: Modern x86_64 with AVX2 support
- Memory: 32GB+ RAM
- Storage: NVMe SSD
- Network: 10Gbps+ connection
# Increase max file descriptors
ulimit -n 1000000
# Optimize network
sysctl -w net.core.rmem_max=16777216
sysctl -w net.core.wmem_max=16777216
# Disable CPU frequency scaling
cpupower frequency-set --governor performance
Optimal shard count depends on your system:
// Calculate optimal shard count
const cpu_cores = try std.Thread.getCpuCount();
const l3_cache_size = try getCPUCacheSize(.L3);
const optimal_shards = calculateOptimalShards(
cpu_cores,
l3_cache_size
);
// Initialize with optimal shards
var book = try orderbook.ShardedOrderbook.init(
allocator,
optimal_shards
);
Optimize data structure alignment:
// Cache-aligned order structure
const CacheAlignedOrder = struct {
price: u64 align(64),
amount: u64,
id: u64,
flags: OrderFlags,
padding: [24]u8, // Ensure 64-byte alignment
};
// SIMD-friendly batch structure
const OrderBatch = struct {
orders: [128]CacheAlignedOrder align(32),
count: usize,
};
Enable vectorized operations:
// Configure SIMD settings
const SimdConfig = struct {
vector_width: u32,
prefetch_distance: u32,
cache_line_size: u32,
};
// Initialize with optimal SIMD config
const config = SimdConfig{
.vector_width = if (builtin.cpu.features.avx2) 256 else 128,
.prefetch_distance = 8,
.cache_line_size = 64,
};
Monitor order processing latency:
// Initialize latency monitor
var monitor = PerformanceMonitor.init(allocator);
defer monitor.deinit();
// Track operation latency
const start = std.time.nanoTimestamp();
try book.placeOrder(order);
const end = std.time.nanoTimestamp();
// Record metrics
try monitor.recordLatency(end - start);
Track order processing rates:
// Measure throughput
const ThroughputMetrics = struct {
orders_per_second: f64,
matches_per_second: f64,
bytes_processed: usize,
};
// Calculate metrics
const metrics = try monitor.calculateThroughput(
order_count,
match_count,
elapsed_time
);
Monitor system resources:
// Track resource usage
const ResourceMetrics = struct {
cpu_usage: f64,
memory_usage: usize,
cache_misses: u64,
network_throughput: u64,
};
// Monitor resources
try monitor.trackResources();
Use bulk operations for higher throughput:
// Prepare order batch
var batch = OrderBatch.init();
for (orders) |order| {
try batch.addOrder(order);
// Process batch when full
if (batch.isFull()) {
try book.processBatch(&batch);
batch.clear();
}
}
Optimize memory usage:
// Use arena allocator for batches
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
// Process with zero allocations
try book.processOrdersZeroCopy(
arena.allocator(),
orders
);
Optimize parallel processing:
// Configure thread pool
const ThreadPoolConfig = struct {
thread_count: usize,
queue_size: usize,
stack_size: usize,
};
// Initialize thread pool
var pool = try ThreadPool.init(ThreadPoolConfig{
.thread_count = cpu_cores,
.queue_size = 1024,
.stack_size = 16384,
});
// Enable CPU features
const CpuFeatures = struct {
use_avx2: bool,
use_avx512: bool,
use_fma: bool,
};
// Configure CPU features
try book.setCpuFeatures(.{
.use_avx2 = true,
.use_avx512 = cpu_info.has_avx512,
.use_fma = true,
});
// Configure memory settings
const MemoryConfig = struct {
page_size: usize,
huge_pages: bool,
prefetch: bool,
};
// Optimize memory usage
try book.setMemoryConfig(.{
.page_size = 2 * 1024 * 1024, // 2MB pages
.huge_pages = true,
.prefetch = true,
});
// Configure network settings
const NetworkConfig = struct {
tcp_nodelay: bool,
keepalive: bool,
buffer_size: usize,
};
// Optimize network
try book.setNetworkConfig(.{
.tcp_nodelay = true,
.keepalive = true,
.buffer_size = 16 * 1024 * 1024,
});
# Run latency benchmark
zig build bench-latency
# Results format:
# P50: 0.3μs
# P95: 0.5μs
# P99: 0.8μs
# P99.9: 1.2μs
# Run throughput benchmark
zig build bench-throughput
# Results format:
# Orders/sec: 1,000,000
# Matches/sec: 500,000
# Data Rate: 10GB/s
# Run stress test
zig build stress-test
# Results format:
# Max Load: 2M orders/sec
# Error Rate: <0.001%
# Recovery Time: <1ms
// Initialize metrics dashboard
var dashboard = try MetricsDashboard.init(allocator);
defer dashboard.deinit();
// Add metrics
try dashboard.addMetric("order_latency", .Histogram);
try dashboard.addMetric("match_rate", .Counter);
try dashboard.addMetric("throughput", .Gauge);
// Start monitoring
try dashboard.start();
// Configure alerts
const AlertConfig = struct {
latency_threshold: u64,
error_threshold: f64,
load_threshold: usize,
};
// Set up alerting
try monitor.setAlerts(.{
.latency_threshold = 1000, // 1μs
.error_threshold = 0.001, // 0.1%
.load_threshold = 1_000_000,// 1M orders/sec
});
- Configure optimal shard count
- Enable SIMD operations
- Set up memory alignment
- Configure thread pool
- Enable performance monitoring
- Monitor latency distribution
- Track throughput metrics
- Watch resource utilization
- Check error rates
- Monitor cache efficiency
- Regular performance testing
- System optimization
- Resource cleanup
- Metric analysis
- Capacity planning