import time
import sys
from pathlib import Path

# Thêm root path
sys.path.append(str(Path(__file__).resolve().parent.parent))

from src.perception.screen_capture import ScreenCapture

def run_benchmark(duration_seconds: int = 5):
    print(f"[*] Bắt đầu kiểm tra tốc độ chụp màn hình trong {duration_seconds} giây...")
    capture = ScreenCapture()
    
    frame_count = 0
    start_time = time.perf_counter()
    end_time = start_time + duration_seconds

    while time.perf_counter() < end_time:
        frame = capture.grab()
        if frame is not None:
            frame_count += 1

    actual_duration = time.perf_counter() - start_time
    avg_fps = frame_count / actual_duration

    print("--------------------------------------------------")
    print(f"Tổng số frame bắt được: {frame_count}")
    print(f"Thời gian đo thực tế:   {actual_duration:.2f} s")
    print(f"Tốc độ trung bình:     {avg_fps:.2f} FPS")
    print(f"Độ trễ mỗi frame:       {(1000 / avg_fps):.2f} ms")
    print("--------------------------------------------------")

if __name__ == "__main__":
    run_benchmark(5)
