"""
性能测试示例
测试群体智能插件的性能表现
"""

import time
from panda3d.core import Vec3

def performance_test(swarm_manager, member_counts=[50, 100, 200, 500]):
    """
    性能测试函数
    测试不同群体规模下的性能表现
    """
    print("=== 群体智能插件性能测试 ===")
    
    # 保存原始设置
    original_count = swarm_manager.config.get("member_count", 20)
    
    for count in member_counts:
        print(f"\n测试群体规模: {count} 个成员")
        
        # 设置群体数量
        swarm_manager.config.set("member_count", count)
        swarm_manager.reset()
        
        # 运行测试
        test_duration = 5.0  # 测试5秒
        start_time = time.time()
        frame_count = 0
        
        # 简单的性能测试循环
        while time.time() - start_time < test_duration:
            swarm_manager._update_swarm()
            frame_count += 1
            
        # 计算FPS
        elapsed_time = time.time() - start_time
        fps = frame_count / elapsed_time
        
        print(f"  帧数: {frame_count}")
        print(f"  耗时: {elapsed_time:.2f} 秒")
        print(f"  平均FPS: {fps:.2f}")
        
        # 显示群体统计信息
        stats = swarm_manager.get_stats()
        print(f"  群体统计: {stats}")
        
    # 恢复原始设置
    swarm_manager.config.set("member_count", original_count)
    swarm_manager.reset()
    
    print("\n性能测试完成")

def behavior_performance_test(swarm_manager):
    """
    行为性能测试
    测试不同行为对性能的影响
    """
    print("\n=== 行为性能测试 ===")
    
    # 基准测试（仅基本Boids行为）
    print("\n基准测试（仅基本Boids行为）:")
    swarm_manager.config.set("obstacle_avoidance_enabled", False)
    swarm_manager.config.set("wander_enabled", False)
    swarm_manager.config.set("predator_avoid_enabled", False)
    swarm_manager.config.set("formation_enabled", False)
    swarm_manager.config.set("path_following_enabled", False)
    
    baseline_duration = run_behavior_test(swarm_manager, 3.0)
    baseline_fps = 3.0 / baseline_duration if baseline_duration > 0 else 0
    print(f"  基准FPS: {baseline_fps:.2f}")
    
    # 避障行为测试
    print("\n避障行为测试:")
    swarm_manager.config.set("obstacle_avoidance_enabled", True)
    # 添加一些障碍物
    for i in range(5):
        pos = Vec3(i*10-20, 0, 10)
        swarm_manager.create_obstacle(pos, 3.0)
        
    obstacle_duration = run_behavior_test(swarm_manager, 3.0)
    obstacle_fps = 3.0 / obstacle_duration if obstacle_duration > 0 else 0
    print(f"  避障FPS: {obstacle_fps:.2f}")
    print(f"  性能下降: {((baseline_fps - obstacle_fps) / baseline_fps * 100):.1f}%")
    
    # 清理障碍物
    swarm_manager.obstacles.clear()
    
    # 游走行为测试
    print("\n游走行为测试:")
    swarm_manager.config.set("obstacle_avoidance_enabled", False)
    swarm_manager.config.set("wander_enabled", True)
    
    wander_duration = run_behavior_test(swarm_manager, 3.0)
    wander_fps = 3.0 / wander_duration if wander_duration > 0 else 0
    print(f"  游走FPS: {wander_fps:.2f}")
    print(f"  性能下降: {((baseline_fps - wander_fps) / baseline_fps * 100):.1f}%")
    
    # 恢复默认设置
    swarm_manager.config.set("wander_enabled", False)
    
    print("\n行为性能测试完成")

def run_behavior_test(swarm_manager, duration):
    """
    运行行为测试
    """
    start_time = time.time()
    frame_count = 0
    
    while time.time() - start_time < duration:
        swarm_manager._update_swarm()
        frame_count += 1
        
    return time.time() - start_time

def stress_test(swarm_manager):
    """
    压力测试
    测试极端条件下的性能表现
    """
    print("\n=== 压力测试 ===")
    
    # 极大群体规模
    print("\n极大群体规模测试 (1000个成员):")
    original_count = swarm_manager.config.get("member_count", 20)
    swarm_manager.config.set("member_count", 1000)
    swarm_manager.reset()
    
    duration = run_behavior_test(swarm_manager, 5.0)
    fps = 5.0 / duration if duration > 0 else 0
    print(f"  FPS: {fps:.2f}")
    
    # 恢复原始设置
    swarm_manager.config.set("member_count", original_count)
    swarm_manager.reset()
    
    # 大量障碍物
    print("\n大量障碍物测试 (100个障碍物):")
    for i in range(100):
        pos = Vec3(
            (i % 10) * 10 - 45,
            (i // 10) * 10 - 45,
            10
        )
        swarm_manager.create_obstacle(pos, 2.0)
        
    duration = run_behavior_test(swarm_manager, 3.0)
    fps = 3.0 / duration if duration > 0 else 0
    print(f"  FPS: {fps:.2f}")
    
    # 清理障碍物
    swarm_manager.obstacles.clear()
    
    print("\n压力测试完成")

def run_all_performance_tests(swarm_manager):
    """
    运行所有性能测试
    """
    print("开始运行所有性能测试...")
    
    # 基本性能测试
    performance_test(swarm_manager)
    
    # 行为性能测试
    behavior_performance_test(swarm_manager)
    
    # 压力测试
    stress_test(swarm_manager)
    
    print("\n所有性能测试完成!")