프로파일링 데코레이터 구현 (Profiling Decorator Implementation)
1. 개요
프로파일링 데코레이터는 자식 노드의 tick() 실행 시간을 측정하고 기록하는 투명한 커스텀 데코레이터이다. 행동 트리의 성능 병목 식별, tick 주기 영향 분석, 노드별 실행 시간 통계에 활용된다.
2. 구현
class ProfilingDecorator : public BT::DecoratorNode
{
public:
ProfilingDecorator(const std::string& name,
const BT::NodeConfiguration& config)
: DecoratorNode(name, config),
total_time_ms_(0.0), tick_count_(0),
max_time_ms_(0.0) {}
static BT::PortsList providedPorts()
{
return {
BT::OutputPort<double>("last_ms", "마지막 tick 소요 시간 (ms)"),
BT::OutputPort<double>("avg_ms", "평균 tick 소요 시간 (ms)"),
BT::OutputPort<double>("max_ms", "최대 tick 소요 시간 (ms)")
};
}
private:
BT::NodeStatus tick() override
{
setStatus(BT::NodeStatus::RUNNING);
auto start = std::chrono::steady_clock::now();
auto status = child_node_->executeTick();
auto end = std::chrono::steady_clock::now();
double ms = std::chrono::duration<double, std::milli>(
end - start).count();
++tick_count_;
total_time_ms_ += ms;
max_time_ms_ = std::max(max_time_ms_, ms);
setOutput("last_ms", ms);
setOutput("avg_ms", total_time_ms_ / tick_count_);
setOutput("max_ms", max_time_ms_);
return status; // 투명 전달
}
void halt() override
{
total_time_ms_ = 0.0;
tick_count_ = 0;
max_time_ms_ = 0.0;
DecoratorNode::halt();
}
double total_time_ms_;
int tick_count_;
double max_time_ms_;
};
3. XML에서의 사용
<Profile last_ms="{nav_tick_ms}" avg_ms="{nav_avg_ms}" max_ms="{nav_max_ms}">
<Action ID="NavigateToPose"/>
</Profile>
4. 활용 사례
4.1 성능 병목 식별
<Sequence>
<Profile last_ms="{path_time}">
<Action ID="ComputePath"/>
</Profile>
<Profile last_ms="{follow_time}">
<Action ID="FollowPath"/>
</Profile>
</Sequence>
경로 계산과 경로 추종의 소요 시간을 각각 측정하여 성능 병목을 식별한다.
4.2 실행 시간 경고
블랙보드에 기록된 실행 시간을 조건 노드에서 평가���여, 지연이 발생하면 경고한다.
<Sequence>
<Profile last_ms="{tick_time}">
<Action ID="ProcessSensorData"/>
</Profile>
<Condition ID="IsValueBelow"
value="{tick_time}" threshold="50.0"/>
</Sequence>
5. 설계 시 고려 사항
프로파일링 데코레��터 자체의 오버헤드(시간 측정, 블랙보드 쓰기)가 측정 결과에 포함된다. 이 오버헤드는 수 마이크로초 수준이므로 대부분의 경우 무시할 수 있으나, 극히 짧은 실행 시간을 측정할 때는 고려하여야 한다.
6. 참고 문헌
- BehaviorTree.CPP 공식 문서. https://www.behaviortree.dev/
| 버전 | 날짜 | 변경 사항 |
|---|---|---|
| v0.1 | 2026-04-05 | 초안 작성 |