Skip to content

Commit 2246a89

Browse files
committed
updated performance improvements
1 parent 430c2b4 commit 2246a89

File tree

16 files changed

+1766
-345
lines changed

16 files changed

+1766
-345
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,41 @@ pip install chuk-tool-processor[observability]
259259
# With MCP extras
260260
pip install chuk-tool-processor[mcp]
261261

262+
# With fast JSON serialization (2-3x faster, recommended for production)
263+
pip install chuk-tool-processor[fast-json]
264+
262265
# All extras
263266
pip install chuk-tool-processor[all]
264267
```
265268

266269
</details>
267270

271+
<details>
272+
<summary><strong>Performance Optimization (Optional)</strong></summary>
273+
274+
For **2-3x faster JSON operations**, install with the `fast-json` extra:
275+
276+
```bash
277+
pip install chuk-tool-processor[fast-json]
278+
```
279+
280+
This installs [orjson](https://github.com/ijl/orjson), a fast C-based JSON library. When available, it's automatically used for JSON serialization/deserialization throughout the processor while maintaining full compatibility with stdlib json.
281+
282+
**Benchmarks** (see `benchmarks/` for full results):
283+
284+
| Operation | stdlib json | orjson | Speedup |
285+
|-----------|-------------|--------|---------|
286+
| Simple JSON (100 bytes) | 1.23 µs | 0.45 µs | **2.7x faster** |
287+
| Complex JSON (5 KB) | 12.5 µs | 4.2 µs | **3.0x faster** |
288+
| OpenAI tool calls | 8.9 µs | 3.1 µs | **2.9x faster** |
289+
290+
**Notes:**
291+
- Falls back to stdlib json automatically if orjson is not installed
292+
- Hash computation uses stdlib json for consistency across environments
293+
- No code changes required—just install the extra
294+
295+
</details>
296+
268297
<details>
269298
<summary><strong>Type Checking Support (PEP 561 compliant)</strong></summary>
270299

benchmarks/README.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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

Comments
 (0)