"""
性能测试用例
"""

import pytest
import time
import asyncio
from api.user_api import UserAPI
from api.role_api import RoleAPI


@pytest.mark.performance
class TestPerformance:
    """性能测试类"""
    
    @pytest.mark.asyncio
    async def test_api_response_time(self, authenticated_client):
        """测试API响应时间"""
        user_api = UserAPI(authenticated_client)
        
        start_time = time.time()
        response = await user_api.get_all_users()
        end_time = time.time()
        
        response_time = (end_time - start_time) * 1000
        
        assert response.status_code == 200
        assert response_time < 1000, f"API响应时间 {response_time}ms 超过1000ms阈值"
    
    @pytest.mark.asyncio
    async def test_concurrent_requests(self, authenticated_client):
        """测试并发请求性能"""
        user_api = UserAPI(authenticated_client)
        
        async def make_request():
            return await user_api.get_all_users()
        
        start_time = time.time()
        tasks = [make_request() for _ in range(10)]
        responses = await asyncio.gather(*tasks)
        end_time = time.time()
        
        total_time = (end_time - start_time) * 1000
        avg_time = total_time / 10
        
        assert all(r.status_code == 200 for r in responses)
        assert avg_time < 500, f"平均响应时间 {avg_time}ms 超过500ms阈值"
    
    @pytest.mark.asyncio
    async def test_large_dataset_query(self, authenticated_client):
        """测试大数据集查询性能"""
        user_api = UserAPI(authenticated_client)
        
        start_time = time.time()
        response = await user_api.get_users_by_page(page=1, size=100)
        end_time = time.time()
        
        response_time = (end_time - start_time) * 1000
        
        assert response.status_code == 200
        assert response_time < 2000, f"大数据集查询时间 {response_time}ms 超过2000ms阈值"