|
| 1 | +# CHUK-Tool-Processor Benchmarks |
| 2 | + |
| 3 | +Performance testing suite for chuk-tool-processor components. |
| 4 | + |
| 5 | +## Benchmark Scripts |
| 6 | + |
| 7 | +### `json_performance.py` |
| 8 | +JSON serialization/deserialization performance testing comparing orjson vs stdlib json. |
| 9 | + |
| 10 | +**Tests:** |
| 11 | +- Serialization (dumps) of various tool call payloads |
| 12 | +- Deserialization (loads) of tool responses |
| 13 | +- Round-trip (dumps + loads) performance |
| 14 | +- Batch operations (100 tool calls) |
| 15 | + |
| 16 | +**Payload Types:** |
| 17 | +- Simple tool calls (basic arguments) |
| 18 | +- Complex tool calls (nested data structures) |
| 19 | +- OpenAI-style tool_calls format |
| 20 | +- Tool results with metadata |
| 21 | +- Large batches (multi-agent scenarios) |
| 22 | + |
| 23 | +**Run:** |
| 24 | +```bash |
| 25 | +# Without orjson (baseline) |
| 26 | +python benchmarks/json_performance.py |
| 27 | + |
| 28 | +# With orjson (optimized) |
| 29 | +uv add --optional fast-json orjson |
| 30 | +python benchmarks/json_performance.py |
| 31 | +``` |
| 32 | + |
| 33 | +**Expected Results:** |
| 34 | +- Serialization: 2-3x faster with orjson |
| 35 | +- Deserialization: 2-3x faster with orjson |
| 36 | +- Round-trip: 2-3x faster with orjson |
| 37 | + |
| 38 | +### `tool_processing_benchmark.py` |
| 39 | +End-to-end tool processor performance testing. |
| 40 | + |
| 41 | +**Tests:** |
| 42 | +- Simple tool call parsing (1000 iterations) |
| 43 | +- Batch processing (3 tools, 500 iterations) |
| 44 | +- JSON format parsing (1000 iterations) |
| 45 | +- Concurrent processing (100 parallel requests) |
| 46 | +- Memory usage analysis (1000 iterations) |
| 47 | + |
| 48 | +**Run:** |
| 49 | +```bash |
| 50 | +# Without orjson (baseline) |
| 51 | +python benchmarks/tool_processing_benchmark.py |
| 52 | + |
| 53 | +# With orjson (optimized) |
| 54 | +uv add --optional fast-json orjson |
| 55 | +python benchmarks/tool_processing_benchmark.py |
| 56 | +``` |
| 57 | + |
| 58 | +**Expected Results:** |
| 59 | +- Simple parsing: 500-1500 ops/sec |
| 60 | +- Batch processing: 1000-3000 calls/sec |
| 61 | +- Concurrent: 100+ batches/sec |
| 62 | +- Memory: <50 MB peak usage |
| 63 | + |
| 64 | +## Installation |
| 65 | + |
| 66 | +### Baseline (stdlib json) |
| 67 | +```bash |
| 68 | +# No additional dependencies needed |
| 69 | +python benchmarks/json_performance.py |
| 70 | +python benchmarks/tool_processing_benchmark.py |
| 71 | +``` |
| 72 | + |
| 73 | +### Optimized (with orjson) |
| 74 | +```bash |
| 75 | +# Install orjson |
| 76 | +uv add --optional fast-json orjson |
| 77 | + |
| 78 | +# Or using pip |
| 79 | +pip install 'chuk-tool-processor[fast-json]' |
| 80 | + |
| 81 | +# Run benchmarks |
| 82 | +python benchmarks/json_performance.py |
| 83 | +python benchmarks/tool_processing_benchmark.py |
| 84 | +``` |
| 85 | + |
| 86 | +## Comparing Performance |
| 87 | + |
| 88 | +### Run both versions |
| 89 | +```bash |
| 90 | +# Baseline |
| 91 | +echo "=== BASELINE (stdlib json) ===" > baseline_results.txt |
| 92 | +python benchmarks/json_performance.py >> baseline_results.txt |
| 93 | +python benchmarks/tool_processing_benchmark.py >> baseline_results.txt |
| 94 | + |
| 95 | +# Install orjson |
| 96 | +uv add --optional fast-json orjson |
| 97 | + |
| 98 | +# Optimized |
| 99 | +echo "=== OPTIMIZED (orjson) ===" > optimized_results.txt |
| 100 | +python benchmarks/json_performance.py >> optimized_results.txt |
| 101 | +python benchmarks/tool_processing_benchmark.py >> optimized_results.txt |
| 102 | + |
| 103 | +# Compare |
| 104 | +diff baseline_results.txt optimized_results.txt |
| 105 | +``` |
| 106 | + |
| 107 | +## Results Directory |
| 108 | + |
| 109 | +Benchmark results are automatically saved to `benchmarks/results/` with timestamps (if enabled). |
| 110 | + |
| 111 | +## Interpreting Results |
| 112 | + |
| 113 | +### JSON Performance |
| 114 | +- **Speedup < 1.5x**: Possible issue with orjson installation |
| 115 | +- **Speedup 2-3x**: Expected performance improvement |
| 116 | +- **Speedup > 3x**: Excellent, especially for complex payloads |
| 117 | + |
| 118 | +### Tool Processing |
| 119 | +- **Simple parsing**: Should be fast (>500 ops/sec) |
| 120 | +- **Batch processing**: Tests parallel execution |
| 121 | +- **Concurrent**: Tests asyncio performance |
| 122 | +- **Memory**: Should be stable (<100 MB for 1000 iterations) |
| 123 | + |
| 124 | +### Warning Signs |
| 125 | +- Memory usage growing unbounded: Possible leak |
| 126 | +- Throughput degrading over time: Performance regression |
| 127 | +- orjson slower than stdlib: Installation or compatibility issue |
| 128 | + |
| 129 | +## CI Integration |
| 130 | + |
| 131 | +Add to CI pipeline: |
| 132 | +```yaml |
| 133 | +- name: Run benchmarks (baseline) |
| 134 | + run: | |
| 135 | + python benchmarks/json_performance.py |
| 136 | + python benchmarks/tool_processing_benchmark.py |
| 137 | +
|
| 138 | +- name: Install orjson |
| 139 | + run: uv add --optional fast-json orjson |
| 140 | + |
| 141 | +- name: Run benchmarks (optimized) |
| 142 | + run: | |
| 143 | + python benchmarks/json_performance.py |
| 144 | + python benchmarks/tool_processing_benchmark.py |
| 145 | +``` |
| 146 | +
|
| 147 | +## Development |
| 148 | +
|
| 149 | +### Adding New Benchmarks |
| 150 | +
|
| 151 | +```python |
| 152 | +import time |
| 153 | +from chuk_tool_processor.utils import fast_json |
| 154 | + |
| 155 | +def benchmark_new_operation(iterations=1000): |
| 156 | + """Benchmark a new operation.""" |
| 157 | + print(f"Running {iterations} iterations...") |
| 158 | + |
| 159 | + start = time.perf_counter() |
| 160 | + for _ in range(iterations): |
| 161 | + # Your operation here |
| 162 | + pass |
| 163 | + elapsed = time.perf_counter() - start |
| 164 | + |
| 165 | + throughput = iterations / elapsed |
| 166 | + print(f"Time: {elapsed:.4f}s") |
| 167 | + print(f"Throughput: {throughput:,.0f} ops/sec") |
| 168 | + |
| 169 | + return elapsed, throughput |
| 170 | +``` |
| 171 | + |
| 172 | +## Performance Tips |
| 173 | + |
| 174 | +1. **Always use fast_json module** for JSON operations instead of direct `import json` |
| 175 | +2. **Install orjson in production** for 2-3x performance boost |
| 176 | +3. **Monitor memory usage** for long-running processes |
| 177 | +4. **Use batch processing** for multiple tool calls |
| 178 | +5. **Enable concurrent processing** for I/O-bound tools |
| 179 | + |
| 180 | +## Optimization Impact |
| 181 | + |
| 182 | +With orjson installed: |
| 183 | +- **JSON operations**: 2-3x faster |
| 184 | +- **Tool parsing**: 20-30% faster overall |
| 185 | +- **Batch processing**: 30-40% faster |
| 186 | +- **Memory**: Similar or slightly better |
| 187 | +- **Throughput**: 2-3x more tool calls/sec |
| 188 | + |
| 189 | +## Troubleshooting |
| 190 | + |
| 191 | +### orjson not detected |
| 192 | +```bash |
| 193 | +# Verify installation |
| 194 | +python -c "import orjson; print(orjson.__version__)" |
| 195 | + |
| 196 | +# Reinstall if needed |
| 197 | +uv add --optional fast-json orjson |
| 198 | +``` |
| 199 | + |
| 200 | +### Benchmark crashes |
| 201 | +```bash |
| 202 | +# Check dependencies |
| 203 | +uv sync |
| 204 | + |
| 205 | +# Run with error output |
| 206 | +python benchmarks/json_performance.py 2>&1 | tee error.log |
| 207 | +``` |
| 208 | + |
| 209 | +### Low performance |
| 210 | +- Check CPU usage during benchmark |
| 211 | +- Verify no other processes competing for resources |
| 212 | +- Try with different iteration counts |
| 213 | +- Compare against baseline results |
0 commit comments