"""
并行计算性能测试
测试并行计算对群体智能模拟性能的影响
"""

import time
from panda3d.core import Vec3

def performance_comparison_test(swarm_manager, member_counts=[100, 200, 500]):
    """
    性能对比测试
    比较串行和并行计算的性能差异
    """
    print("=== 并行计算性能对比测试 ===")
    
    # 保存原始设置
    original_count = swarm_manager.config.get("member_count", 20)
    original_parallel = swarm_manager.config.get("parallel_computation", True)
    
    for count in member_counts:
        print(f"\n测试群体规模: {count} 个成员")
        
        # 设置群体数量
        swarm_manager.config.set("member_count", count)
        swarm_manager.reset()
        
        # 测试串行计算性能
        print("  测试串行计算...")
        swarm_manager.config.set("parallel_computation", False)
        serial_time = run_performance_test(swarm_manager, 3.0)
        serial_fps = 3.0 / serial_time if serial_time > 0 else 0
        print(f"    串行计算FPS: {serial_fps:.2f}")
        
        # 测试并行计算性能
        print("  测试并行计算...")
        swarm_manager.config.set("parallel_computation", True)
        parallel_time = run_performance_test(swarm_manager, 3.0)
        parallel_fps = 3.0 / parallel_time if parallel_time > 0 else 0
        print(f"    并行计算FPS: {parallel_fps:.2f}")
        
        # 计算性能提升
        if serial_fps > 0:
            speedup = parallel_fps / serial_fps
            print(f"    性能提升: {speedup:.2f}x")
        
    # 恢复原始设置
    swarm_manager.config.set("member_count", original_count)
    swarm_manager.config.set("parallel_computation", original_parallel)
    swarm_manager.reset()
    
    print("\n性能对比测试完成")

def run_performance_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 scalability_test(swarm_manager):
    """
    可扩展性测试
    测试不同CPU核心数对并行计算性能的影响
    """
    print("\n=== 可扩展性测试 ===")
    
    # 设置大规模群体
    original_count = swarm_manager.config.get("member_count", 20)
    swarm_manager.config.set("member_count", 1000)
    swarm_manager.reset()
    
    # 测试不同并行度
    parallel_settings = [1, 2, 4, 8]
    
    for num_threads in parallel_settings:
        print(f"\n测试并行度: {num_threads} 线程")
        
        # 这里我们模拟不同的并行度设置
        # 实际应用中可能需要调整线程池大小
        test_time = run_performance_test(swarm_manager, 2.0)
        test_fps = 2.0 / test_time if test_time > 0 else 0
        print(f"  FPS: {test_fps:.2f}")
        
    # 恢复原始设置
    swarm_manager.config.set("member_count", original_count)
    swarm_manager.reset()
    
    print("\n可扩展性测试完成")

def memory_usage_test(swarm_manager):
    """
    内存使用测试
    测试并行计算对内存使用的影响
    """
    print("\n=== 内存使用测试 ===")
    
    # 设置中等规模群体
    original_count = swarm_manager.config.get("member_count", 20)
    swarm_manager.config.set("member_count", 500)
    swarm_manager.reset()
    
    # 获取初始内存使用
    import psutil
    import os
    process = psutil.Process(os.getpid())
    initial_memory = process.memory_info().rss / 1024 / 1024  # MB
    
    print(f"初始内存使用: {initial_memory:.2f} MB")
    
    # 测试串行计算内存使用
    swarm_manager.config.set("parallel_computation", False)
    run_performance_test(swarm_manager, 1.0)
    serial_memory = process.memory_info().rss / 1024 / 1024  # MB
    print(f"串行计算内存使用: {serial_memory:.2f} MB")
    
    # 测试并行计算内存使用
    swarm_manager.config.set("parallel_computation", True)
    run_performance_test(swarm_manager, 1.0)
    parallel_memory = process.memory_info().rss / 1024 / 1024  # MB
    print(f"并行计算内存使用: {parallel_memory:.2f} MB")
    
    # 计算内存开销
    serial_overhead = serial_memory - initial_memory
    parallel_overhead = parallel_memory - initial_memory
    print(f"串行计算内存开销: {serial_overhead:.2f} MB")
    print(f"并行计算内存开销: {parallel_overhead:.2f} MB")
    
    # 恢复原始设置
    swarm_manager.config.set("member_count", original_count)
    swarm_manager.reset()
    
    print("\n内存使用测试完成")

def run_all_parallel_tests(swarm_manager):
    """
    运行所有并行计算测试
    """
    print("开始运行所有并行计算测试...")
    
    # 性能对比测试
    performance_comparison_test(swarm_manager)
    
    # 可扩展性测试
    scalability_test(swarm_manager)
    
    # 内存使用测试
    memory_usage_test(swarm_manager)
    
    print("\n所有并行计算测试完成!")