Overview
The CoW Protocol Contracts project includes comprehensive benchmarking tools for measuring gas consumption, comparing performance across code changes, and generating detailed execution traces. These tools help optimize contract efficiency and identify gas optimization opportunities.Benchmarking Commands
Running Benchmarks
Full Benchmark Suite
Run gas benchmarks across a comprehensive set of settlement scenarios:- Executes
hardhat run bench/index.ts - Tests multiple settlement scenarios
- Measures gas consumption for each scenario
- Outputs a summary of gas costs
- Single and multiple order settlements
- Various token pair combinations
- Different interaction types (AMM, liquidity)
- Edge cases and complex scenarios
Benchmarks should be run on a clean build without coverage instrumentation for accurate gas measurements.
Single Settlement Benchmark
For focused testing of a specific settlement scenario:hardhat run bench/single.ts
Protocol-Specific Benchmarks
Benchmark specific DEX integrations:Performance Tracing
Generate detailed execution traces to identify gas consumption patterns:- Runs
hardhat run bench/trace/index.ts - Generates detailed call traces
- Shows gas consumption at each step
- Identifies expensive operations
- Helps pinpoint optimization opportunities
Understanding Trace Output
The trace output includes:- Call stack: Hierarchical view of contract calls
- Gas costs: Per-operation gas consumption
- Storage operations: SLOAD and SSTORE costs
- External calls: Interaction costs with other contracts
- Event emissions: Gas costs for logging
Analyze the output
Review the trace to identify:
- Most expensive operations
- Redundant storage accesses
- Optimization opportunities
Comparing Performance
Compare gas consumption between different code versions:- Runs benchmarks on the current code
- Checks out the specified git reference (branch, commit, tag)
- Runs benchmarks on the reference code
- Generates a comparison report
- Defaults to the merge-base if no reference is provided
Usage Examples
Comparison Script Details
Executes:bash bench/compare.sh
The script workflow:
- Stash current changes (if any)
- Run benchmarks on current code
- Checkout reference commit/branch
- Run benchmarks on reference code
- Generate diff report showing gas changes
- Restore original state
Interpreting Comparison Results
The comparison output shows:- Positive values: Increased gas consumption (regression)
- Negative values: Decreased gas consumption (improvement)
- Percentage changes: Relative impact of changes
- Scenario breakdown: Per-test-case comparisons
Best Practices
Before Benchmarking
-
Clean build: Ensure contracts are built without coverage instrumentation
- Stable environment: Close unnecessary applications to reduce system variance
- Consistent state: Use the same blockchain state for comparable results
During Benchmarking
- Run benchmarks multiple times for statistical significance
- Document any non-standard configuration
- Note the commit hash for reproducibility
After Benchmarking
- Analyze results for unexpected changes
- Investigate any significant gas increases
- Document optimizations and their impact
- Consider trade-offs between gas costs and code readability
Optimization Workflow
Gas Optimization Tips
Common optimization strategies:Storage Optimization
- Pack variables into single storage slots
- Use immutable and constant where possible
- Minimize SSTORE operations
- Cache storage reads in memory
Computation Optimization
- Minimize loop iterations
- Use unchecked blocks for safe math
- Avoid redundant calculations
- Optimize conditional logic
External Call Optimization
- Batch operations when possible
- Minimize external calls
- Use static calls for read-only operations
- Consider delegate calls for shared logic
Compiler Impact
The Foundry configuration significantly impacts gas consumption:The optimizer is configured for 1,000,000 runs, prioritizing execution gas costs over deployment costs. Adjust this value if your use case differs.
Continuous Monitoring
Integrate benchmarking into your development workflow:- Pre-commit: Run quick benchmarks on critical paths
- Pull requests: Compare against base branch
- CI/CD: Automated benchmark regression testing
- Release: Document gas costs in release notes
Troubleshooting
Inconsistent Results
- Ensure consistent blockchain state
- Rebuild contracts between runs
- Check for system resource contention
Trace Generation Failures
- Verify Hardhat configuration
- Check node version compatibility
- Ensure sufficient system memory
Comparison Script Issues
- Verify git repository state
- Ensure reference exists
- Check for uncommitted changes
Next Steps
- Review testing documentation for comprehensive testing strategies
- Explore contract architecture to understand gas optimization opportunities
- Check deployment guidelines for production considerations
