#!/usr/bin/env python3
"""
#exonware/xwdata/tests/3.advance/test_performance.py
Comprehensive performance benchmarks for xwdata.
Priority #4: Performance Excellence
Company: eXonware.com
Author: eXonware Backend Team
Email: connect@exonware.com
Version: 0.0.1
Generation Date: 07-Jan-2025
"""

import pytest
import time
import asyncio
from pathlib import Path
from exonware.xwdata import XWData
@pytest.mark.xwdata_advance
@pytest.mark.xwdata_performance

class TestFormatConversionPerformance:
    """Performance tests for format conversion."""
    @pytest.mark.asyncio

    async def test_json_to_yaml_conversion_performance(self):
        """Test JSON to YAML conversion performance."""
        data = {"key": "value" * 1000, "numbers": list(range(10000))}
        xwdata = XWData(data, format="json")
        start = time.time()
        yaml_result = await xwdata.serialize("yaml")
        elapsed = time.time() - start
        # Conversion should complete in < 1 second
        assert elapsed < 1.0, f"JSON to YAML conversion too slow: {elapsed:.3f}s"
        assert yaml_result is not None
    @pytest.mark.asyncio

    async def test_large_data_conversion_performance(self):
        """Test format conversion performance with large data."""
        large_data = {"items": [{"id": i, "data": "x" * 100} for i in range(10000)]}
        xwdata = XWData(large_data, format="json")
        start = time.time()
        yaml_result = await xwdata.serialize("yaml")
        elapsed = time.time() - start
        # Large data conversion should complete in < 5 seconds
        assert elapsed < 5.0, f"Large data conversion too slow: {elapsed:.3f}s"
@pytest.mark.xwdata_advance
@pytest.mark.xwdata_performance

class TestCOWPerformance:
    """Performance tests for Copy-on-Write operations."""
    @pytest.mark.asyncio

    async def test_cow_set_performance(self):
        """Test COW set operation performance."""
        data = {"key": "value", "numbers": list(range(1000))}
        xwdata = XWData(data)
        # Test multiple COW operations
        start = time.time()
        for i in range(100):
            xwdata = await xwdata.set(f"key_{i}", f"value_{i}")
        elapsed = time.time() - start
        # 100 COW operations should complete in < 1 second
        assert elapsed < 1.0, f"COW set operations too slow: {elapsed:.3f}s for 100 operations"
    @pytest.mark.asyncio

    async def test_cow_merge_performance(self):
        """Test COW merge operation performance."""
        data1 = {"a": 1, "b": 2, "c": {"x": 10, "y": 20}}
        data2 = {"c": {"z": 30}, "d": 4}
        xwdata1 = XWData(data1)
        xwdata2 = XWData(data2)
        start = time.time()
        merged = await xwdata1.merge(xwdata2)
        elapsed = time.time() - start
        # Merge should complete in < 0.1 seconds
        assert elapsed < 0.1, f"COW merge too slow: {elapsed:.3f}s"
@pytest.mark.xwdata_advance
@pytest.mark.xwdata_performance

class TestSerializationPerformance:
    """Performance tests for serialization operations."""
    @pytest.mark.asyncio

    async def test_json_serialization_performance(self):
        """Test JSON serialization performance."""
        data = {"key": "value" * 1000, "numbers": list(range(10000))}
        xwdata = XWData(data)
        start = time.time()
        serialized = await xwdata.serialize("json")
        elapsed = time.time() - start
        # Serialization should complete in < 0.5 seconds
        assert elapsed < 0.5, f"JSON serialization too slow: {elapsed:.3f}s"
        assert serialized is not None
    @pytest.mark.asyncio

    async def test_yaml_serialization_performance(self):
        """Test YAML serialization performance."""
        data = {"key": "value" * 1000, "numbers": list(range(10000))}
        xwdata = XWData(data)
        start = time.time()
        serialized = await xwdata.serialize("yaml")
        elapsed = time.time() - start
        # Serialization should complete in < 1 second
        assert elapsed < 1.0, f"YAML serialization too slow: {elapsed:.3f}s"
        assert serialized is not None
@pytest.mark.xwdata_advance
@pytest.mark.xwdata_performance

class TestFileOperationsPerformance:
    """Performance tests for file operations."""
    @pytest.mark.asyncio

    async def test_save_performance(self, tmp_path):
        """Test save operation performance."""
        data = {"key": "value", "numbers": list(range(1000))}
        xwdata = XWData(data)
        test_file = tmp_path / "test.json"
        start = time.time()
        await xwdata.save(test_file)
        elapsed = time.time() - start
        # Save should complete in < 0.5 seconds
        assert elapsed < 0.5, f"Save operation too slow: {elapsed:.3f}s"
        assert test_file.exists()
    @pytest.mark.asyncio

    async def test_load_performance(self, tmp_path):
        """Test load operation performance."""
        data = {"key": "value", "numbers": list(range(1000))}
        xwdata = XWData(data)
        test_file = tmp_path / "test.json"
        await xwdata.save(test_file)
        start = time.time()
        loaded = XWData.load(test_file)
        elapsed = time.time() - start
        # Load should complete in < 0.5 seconds
        assert elapsed < 0.5, f"Load operation too slow: {elapsed:.3f}s"
        assert loaded is not None
@pytest.mark.xwdata_advance
@pytest.mark.xwdata_performance

class TestMemoryPerformance:
    """Performance tests for memory usage."""
    @pytest.mark.asyncio

    async def test_large_dataset_memory_efficiency(self):
        """Test memory efficiency with large datasets."""
        import sys
        import gc
        # Create large dataset
        large_data = {"items": [{"id": i, "value": "x" * 1000} for i in range(10000)]}
        # Measure memory before
        gc.collect()
        before_size = sys.getsizeof(large_data)
        # Create XWData instance
        xwdata = XWData(large_data)
        # Clear local reference
        del large_data
        gc.collect()
        # Verify data integrity
        result = await xwdata.get("items.0.id")
        assert result == 0
