#!/usr/bin/env python3
"""Benchmark memory pool vs no memory pool."""

import time
from cutysoup import CutySoup

# Test HTML
test_html = """
<html>
<body>
    <div class="container">
        <h1>Title</h1>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
        </ul>
        <div class="content">
            <span>Span 1</span>
            <span>Span 2</span>
        </div>
    </div>
</body>
</html>
"""

def benchmark_with_pool():
    """Benchmark with memory pool enabled."""
    soup = CutySoup(test_html)
    
    iterations = 2000
    start_time = time.perf_counter()
    
    for _ in range(iterations):
        divs = soup.find_all('div')
        paragraphs = soup.find_all('p')
        list_items = soup.find_all('li')
        spans = soup.find_all('span')
        
        # Access properties
        for div in divs:
            _ = div.name
            _ = div.get('class', [])
    
    end_time = time.perf_counter()
    return end_time - start_time

def benchmark_without_pool():
    """Benchmark with memory pool disabled."""
    # Temporarily disable the pool
    from cutysoup.core import _tag_pool
    original_pool = _tag_pool
    
    # Set pool to None to disable it
    import cutysoup.core
    cutysoup.core._tag_pool = None
    
    try:
        soup = CutySoup(test_html)
        
        iterations = 2000
        start_time = time.perf_counter()
        
        for _ in range(iterations):
            divs = soup.find_all('div')
            paragraphs = soup.find_all('p')
            list_items = soup.find_all('li')
            spans = soup.find_all('span')
            
            # Access properties
            for div in divs:
                _ = div.name
                _ = div.get('class', [])
        
        end_time = time.perf_counter()
        return end_time - start_time
    
    finally:
        # Restore the original pool
        cutysoup.core._tag_pool = original_pool

def main():
    print("Memory Pool Performance Benchmark")
    print("=" * 40)
    
    # Warm up
    soup = CutySoup(test_html)
    _ = soup.find_all('div')
    
    # Benchmark with pool
    print("Running with memory pool...")
    time_with_pool = benchmark_with_pool()
    
    # Benchmark without pool  
    print("Running without memory pool...")
    time_without_pool = benchmark_without_pool()
    
    print("\nResults:")
    print(f"With memory pool:    {time_with_pool:.4f}s")
    print(f"Without memory pool: {time_without_pool:.4f}s")
    print(f"Speedup:             {time_without_pool/time_with_pool:.2f}x")
    print(f"Performance gain:    {((time_without_pool-time_with_pool)/time_without_pool)*100:.1f}%")
    
    # Show pool stats
    from cutysoup.core import _tag_pool
    if _tag_pool:
        stats = _tag_pool.get_pool_stats()
        print(f"\nPool Statistics:")
        print(f"Pool hits:    {stats['hits']}")
        print(f"Pool misses:  {stats['misses']}")
        print(f"Hit rate:     {stats['hit_rate']:.1%}")

if __name__ == '__main__':
    main()