Files
security-ai-edge/analyze_latency.py
16337 c17f983ab3 perf: batch=1 优化减少延迟
- settings: batch_size=41
- tensorrt_engine: BATCH_SIZE=41
- preprocessor: 移除 padding 逻辑,直接 batch=1
- 预处理延迟从 17ms  5ms
2026-02-02 15:25:13 +08:00

57 lines
2.0 KiB
Python

"""详细延迟分析 - 简化版"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import time
import numpy as np
import cv2
from config.settings import get_settings
from core.preprocessor import ImagePreprocessor
settings = get_settings()
preprocessor = ImagePreprocessor(settings.inference)
# 模拟 100 次推理
img = np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8)
roi_mock = type('ROI', (), {'x1': 300, 'y1': 100, 'x2': 1000, 'y2': 800, 'enabled': True})()
times_preprocess = []
times_single = []
times_batch = []
for _ in range(100):
# 1. preprocess_single
start = time.perf_counter()
cropped = preprocessor.preprocess_single(img, roi_mock)
t = (time.perf_counter() - start) * 1000
times_single.append(t)
# 2. preprocess_batch (1→4)
start = time.perf_counter()
batch_data, _ = preprocessor._batch_preprocessor.preprocess_batch([cropped[0]])
t = (time.perf_counter() - start) * 1000
times_batch.append(t)
# 3. 完整 preprocess (single + batch)
start = time.perf_counter()
cropped = preprocessor.preprocess_single(img, roi_mock)
batch_data, _ = preprocessor._batch_preprocessor.preprocess_batch([cropped[0]])
t = (time.perf_counter() - start) * 1000
times_preprocess.append(t)
print("延迟分析 (100次平均):")
print(f" preprocess_single (ROI + resize): {np.mean(times_single):.2f}ms")
print(f" preprocess_batch (padding 1→4): {np.mean(times_batch):.2f}ms")
print(f" 完整预处理: {np.mean(times_preprocess):.2f}ms")
print()
print(f"TensorRT 推理 (batch=1): ~2.5ms (基准测试)")
print(f"TensorRT 推理 (batch=4): ~5.0ms (基准测试)")
print()
print("推算总延迟:")
print(f" 方案A (batch=1): {np.mean(times_single):.2f} + 2.5 + 后处理 ≈ 10-15ms")
print(f" 方案B (batch=4 实际只推理1帧): {np.mean(times_preprocess):.2f} + 5 + 后处理 ≈ 55-65ms")
print()
print("结论:延迟主要来自 batch padding 和不必要的 4帧推理开销")