This guide explains how to test, monitor, and optimize the Snake Agent's performance in the RAVANA system.
- Overview
- Running Performance Tests
- Monitoring Snake Agent Performance
- Configuration Optimization
- Threading and Multiprocessing Optimization
- Error Handling and Auto-Recovery
- UV Dependency Management
- Best Practices
The Snake Agent is an autonomous code analysis and improvement system that runs continuously in the background to monitor, analyze, and enhance the RAVANA codebase. This guide provides instructions on how to test its performance, monitor its operation, and optimize its capabilities.
To run the comprehensive Snake Agent performance test suite:
python test_snake_agent_performance.pyThis test suite includes:
- Initialization tests for both basic and enhanced snake agents
- Configuration validation tests
- Threading manager performance tests
- Process manager performance tests
- Resource usage measurements
- Detailed performance analysis with varying thread counts
For specific tests, you can run:
# Run with pytest for more detailed output
python -m pytest tests/test_enhanced_snake_agent.py -v
# Run the simple core functionality test
python tests/simple_snake_test.pyTo monitor the Snake Agent in real-time, use the performance monitoring script:
python snake_performance_monitor.py --short-testThis will initialize a Snake Agent instance and collect performance metrics over a short period.
To analyze previously collected performance data:
python snake_performance_monitor.py --analyze-file snake_performance.json- Processing Rate: Tasks processed per minute
- Error Rate: Percentage of failed tasks
- Resource Usage: CPU and memory consumption
- Queue Sizes: Thread and process queue backlogs
- Active Workers: Number of active threads and processes
- Task Completion: Files analyzed, experiments completed, improvements applied
The Snake Agent can be tuned using environment variables in the core/config.py file:
# Enhanced threading configuration
SNAKE_MAX_THREADS = int(os.environ.get("SNAKE_MAX_THREADS", "12"))
SNAKE_MAX_PROCESSES = int(os.environ.get("SNAKE_MAX_PROCESSES", "6"))
SNAKE_ANALYSIS_THREADS = int(os.environ.get("SNAKE_ANALYSIS_THREADS", "4"))
# Performance monitoring
SNAKE_PERF_MONITORING = bool(os.environ.get("SNAKE_PERF_MONITORING", "True").lower() in ["true", "1", "yes"])
# Task and queue settings
SNAKE_TASK_TIMEOUT = float(os.environ.get("SNAKE_TASK_TIMEOUT", "600.0")) # 10 minutes
SNAKE_MAX_QUEUE_SIZE = int(os.environ.get("SNAKE_MAX_QUEUE_SIZE", "2000"))
SNAKE_MONITOR_INTERVAL = float(os.environ.get("SNAKE_MONITOR_INTERVAL", "1.0")) # 1 secondFor high-performance environments:
export SNAKE_MAX_THREADS=16
export SNAKE_MAX_PROCESSES=8
export SNAKE_ANALYSIS_THREADS=6
export SNAKE_MAX_QUEUE_SIZE=5000
export SNAKE_PERF_MONITORING=TrueFor resource-constrained environments:
export SNAKE_MAX_THREADS=4
export SNAKE_MAX_PROCESSES=2
export SNAKE_ANALYSIS_THREADS=2
export SNAKE_MAX_QUEUE_SIZE=1000
export SNAKE_PERF_MONITORING=FalseThe Snake Agent uses a hybrid threading and multiprocessing architecture:
Threading Components:
- File Monitor Threads: Monitor file system changes
- Indexing Threads: Perform code analysis and indexing (parallelized)
- Study Threads: Perform deep analysis (serialized)
- Communication Threads: Handle communication with the main system
Process Components:
- Experiment Processes: Run experiments safely in isolated processes
- Analysis Processes: Perform deep analysis on CPU-intensive tasks
- Improvement Processes: Handle improvement implementations
The optimal thread configuration depends on your system:
- Analysis Threads: Should be set to 25-50% of max threads, depending on workload
- Study Threads: Usually kept at 1 since some analysis tasks need to be serialized
- File Monitor Threads: Usually 1 is sufficient
- Communication Threads: Usually 1 is sufficient
Queue sizes should be large enough to handle bursts of activity but not so large that they consume excessive memory:
- For heavy workloads: 2000-5000 items per queue
- For light workloads: 500-1000 items per queue
The Snake Agent includes several error handling mechanisms:
- Component-Level Isolation: Errors in one component don't affect others
- Exponential Backoff: Failed components retry with increasing delays
- Graceful Degradation: System continues operating even if some components fail
- Detailed Logging: All errors are logged with full context
The system includes auto-recovery capabilities:
- Thread Restart: Failed threads are automatically restarted
- Process Restart: Failed processes are automatically restarted
- State Recovery: Agent state is persisted and restored on restart
- Connection Recovery: Lost connections are automatically reestablished
Watch for these key error indicators:
# Check the agent status for error counts
status = await snake_agent.get_status()
error_metrics = {
"thread_errors": sum(thread.get("error_count", 0) for thread in status.get("threading_status", {}).values()),
"process_errors": sum(process.get("tasks_failed", 0) for process in status.get("process_status", {}).values()),
}RAVANA uses UV for fast Python package management. To set it up:
-
Install UV (if not already installed):
# Install using the official installation script curl -LsSf https://astral.sh/uv/install.sh | sh # Or with Homebrew on macOS brew install uv # Or with pip pip install uv
-
Create a virtual environment:
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies using UV:
# Install all dependencies quickly uv pip install -r requirements.txt # Or install from pyproject.toml uv pip install -e .
- Faster installation: UV is significantly faster than pip
- Dependency resolution: Better and faster dependency resolution
- Virtual environment management: Built-in venv management
- Consistent environments: More reproducible builds
# Update all dependencies to the latest compatible versions
uv pip sync requirements.txt
# Install a specific package
uv pip install package_name
# Update pyproject.toml dependencies
uv pip compile pyproject.toml --output-file requirements.txt- Monitor Resource Usage: Regularly check CPU and memory usage to determine optimal thread/process counts
- Adjust Queue Sizes: Tune queue sizes based on your workload patterns
- Use Appropriate Thread Counts: Don't set thread counts higher than your CPU can effectively handle
- Enable Performance Monitoring: Keep performance monitoring enabled in production for ongoing optimization
- Regular Testing: Run performance tests regularly as the codebase evolves
- Logging and Monitoring: Keep detailed logs and monitor system metrics continuously
- State Persistence: Ensure state is properly persisted for recovery
- Error Handling: Implement proper error handling and alerting
- Regular Maintenance: Perform regular cleanup and optimization tasks
- Backup and Recovery: Implement proper backup and recovery procedures
- Testing: Write comprehensive tests for new Snake Agent functionality
- Performance Testing: Always test performance impact of new features
- Error Handling: Implement comprehensive error handling in new code
- Documentation: Document new features and configuration options
- Code Quality: Maintain high code quality standards for self-improvement code
-
High Memory Usage:
- Reduce thread and queue counts
- Check for memory leaks in custom analysis code
- Implement proper resource cleanup
-
Low Processing Rate:
- Increase thread counts (but not beyond CPU capacity)
- Optimize analysis algorithms
- Check for I/O bottlenecks
-
High Error Rates:
- Review logs for root causes
- Improve error handling in analysis code
- Validate input data more thoroughly
- Start with conservative settings (4-8 threads, 2-4 processes)
- Monitor performance under load
- Gradually increase thread/process counts while monitoring metrics
- Stop increasing when performance plateaus or degrades
- Monitor resource usage to ensure you're not overloading the system
- Repeat the process periodically as workload changes
This guide provides a comprehensive overview of how to test, monitor, and optimize the Snake Agent for peak performance. Regular monitoring and tuning will ensure the agent continues to run efficiently as the RAVANA system evolves.