#!/bin/bash

# Benchmark runner script for vectorlite embedding generation
# This script runs comprehensive benchmarks and generates performance reports

set -e

echo "🚀 Starting VectorLite Embedding Benchmarks"
echo "============================================="

# Create benchmark results directory
mkdir -p benchmark_results

# Run all benchmarks
echo "📊 Running embedding generation benchmarks..."
cargo bench --bench embedding_benchmarks -- --output-format html

# Move results to our directory
if [ -d "target/criterion" ]; then
    cp -r target/criterion/* benchmark_results/
    echo "✅ Benchmark results saved to benchmark_results/"
fi

# Generate summary report
echo "📈 Generating performance summary..."

cat > benchmark_results/PERFORMANCE_SUMMARY.md << EOF
# VectorLite Embedding Generation Performance Report

Generated on: $(date)

## Benchmark Overview

This report contains comprehensive performance benchmarks for the VectorLite embedding generation system.

## Key Metrics

- **Single Embedding Generation**: Measures latency for individual text processing
- **Batch Processing**: Evaluates throughput for multiple texts processed together
- **Text Length Impact**: Analyzes performance across different input text lengths
- **Memory Usage**: Tracks memory consumption during embedding generation
- **Initialization Time**: Measures model loading and setup time
- **Consistency**: Validates that repeated processing produces identical results

## Benchmark Categories

### 1. Single Embedding Generation
- Tests individual text processing performance
- Measures latency across different text lengths
- Sample size: 100 iterations per test

### 2. Batch Embedding Generation
- Evaluates throughput with batch sizes: 1, 5, 10, 20, 50
- Measures efficiency of processing multiple texts together
- Sample size: 50 iterations per test

### 3. Text Length Impact
- Short text: "AI" (2 characters)
- Medium text: ~100 characters
- Long text: ~500 characters  
- Very long text: ~1000+ characters

### 4. Memory Usage Analysis
- Tracks memory consumption for single and batch operations
- Helps identify memory optimization opportunities

### 5. Initialization Performance
- Measures model loading time
- Critical for cold start scenarios

## Usage

To run benchmarks:
\`\`\`bash
cargo bench --bench embedding_benchmarks
\`\`\`

To run specific benchmark groups:
\`\`\`bash
cargo bench --bench embedding_benchmarks single_embedding_generation
cargo bench --bench embedding_benchmarks batch_embedding_generation
\`\`\`

## Results Location

- HTML reports: \`benchmark_results/\`
- Raw data: \`target/criterion/\`

## Next Steps

1. Analyze performance bottlenecks
2. Implement optimizations based on results
3. Compare different model configurations
4. Test on different hardware configurations
5. Establish performance baselines for regression testing

EOF

echo "✅ Performance summary generated: benchmark_results/PERFORMANCE_SUMMARY.md"

# Run a quick performance test
echo "⚡ Running quick performance test..."
cargo bench --bench embedding_benchmarks single_embedding_generation -- --sample-size 10

echo "🎉 Benchmark suite completed successfully!"
echo "📁 Results available in: benchmark_results/"
echo "🌐 Open benchmark_results/index.html in your browser to view detailed reports"
