RAG2026-5일32차시 · 고도화된 Modular RAG10 / 18← 이전목차다음 →MetaContents

32차시. 고도화된 Modular RAG

7. 실습 ⑤ — 노드별 성능 프로파일

출처: 원본 PDF 32차시_ModularRAG종합.pdf 14~15쪽 · 쪽 그림 14쪽 15쪽

어디가 병목인지 측정합니다. 33차시 최적화의 기초 자료입니다.

📝 src/profile_nodes.py

출력
import time
from collections import defaultdict
from graph import app
from state import init_state
def _new_stat():
    return {"count": 0, "total": 0.0}
def _sort_key(item):
    return -item[1]["total"]
def profile(questions):
    stats = defaultdict(_new_stat)
    for q in questions:
        s = init_state(q)
        s["deadline"] = time.time() + 60
        last = time.time()
        for step in app.stream(s, {"recursion_limit": 40}):
            now = time.time()
            for node in step.keys():
                stats[node]["count"] += 1
                stats[node]["total"] += now - last
            last = now
    print(f"{'노드':<12} {'호출':>5} {'총시간':>8} {'평균':>8}")
    print("-" * 40)
    rows = sorted(stats.items(), key=_sort_key)
    for node, s in rows:
        avg = s["total"] / s["count"]
        print(f"{node:<12} {s['count']:>5} "
              f"{s['total']:>7.1f}s {avg:>7.2f}s")
profile([t["q"] for t in TESTS])

실행 결과 예시
────────────────────────────────────────
노드            호출    총시간     평균
────────────────────────────────────────
generate         25     48.2s    1.93s   ← 최대 병목
verify           25     26.7s    1.07s
rewrite           5      6.1s    1.22s
classify         20      4.3s    0.22s
retrieve         25      3.8s    0.15s
fallback          3      0.0s    0.00s
bump              3      0.0s    0.00s

🎯 프로파일이 알려주는 것
generate가 전체의 55%
→ 답변 길이를 줄이거나 모델을 바꾸면 효과가 큼
verify가 30%
→ 규칙 필터를 강화하면 호출 자체가 줄어듦
retrieve는 4%
→ 검색 최적화는 효과가 미미함
병목이 아닌 곳을 최적화하는 것은 낭비입니다.
33차시에서 이 자료를 근거로 개선합니다.

← 이전 절32차시 · 6. 성능 변화 총정리다음 절 →32차시 · 8. 실습 ⑥ — 아키텍처 문서 작성