Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/MIUAV/vibe-coding-ros2 --skill performance-profiling명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
SOC 직업 분류 기준
SKILL.md 표시 중
| name | performance-profiling |
| description | 性能分析技能 - 延迟分析、CPU/内存分析、跟踪工具、性能优化 |
| argument-hint | 性能分析 OR profiling OR latency OR perf OR optimization |
| user-invocable | true |
ROS2 性能分析与优化
当需要以下帮助时使用此技能:
import time
import numpy as np
class LatencyAnalyzer:
def __init__(self):
self.latencies = []
def measure_callback_latency(self, callback, *args, **kwargs):
"""测量回调延迟"""
start = time.monotonic()
result = callback(*args, **kwargs)
end = time.monotonic()
latency = (end - start) * 1000 # ms
self.latencies.append(latency)
return result
def get_statistics(self):
"""获取延迟统计"""
if not self.latencies:
return {}
return {
'mean': np.mean(self.latencies),
'std': np.std(self.latencies),
'min': np.min(self.latencies),
'max': np.max(self.latencies),
'p50': np.percentile(self.latencies, 50),
'p95': np.percentile(self.latencies, 95),
'p99': np.percentile(self.latencies, 99)
}
def reset(self):
"""重置统计"""
self.latencies = []
class EndToEndLatencyMeasure:
def __init__(self, node):
self.node = node
self.timestamps = {}
# 订阅消息
self.sub = node.create_subscription(
LaserScan, '/scan', self.scan_callback, 10)
# 发布延迟统计
self.pub = node.create_publisher(Float64, '/latency/scan', 10)
def scan_callback(self, msg):
# 记录接收时间
recv_time = self.node.get_clock().now().nanoseconds
# 获取消息头时间
if msg.header.stamp.sec > 0:
sent_time = msg.header.stamp.sec * 1e9 + msg.header.stamp.nanosec
latency_ms = (recv_time - sent_time) / 1e6
latency_msg = Float64()
latency_msg.data = latency_ms
self.pub.publish(latency_msg)
# top 命令
top -p $(pidof -x node_executable)
# pidstat (sysstat)
pidstat -p $(pidof node) 1
# 内存分析
valgrind --tool=massif ./executable
# 查看结果
ms_print massif.out.*
# CPU 热点分析
perf record -g -p $(pidof node)
perf report
# 使用 rclpy 的性能分析
import cProfile
import pstats
class PerformanceProfiler:
def __init__(self):
self.profiler = cProfile.Profile()
def profile_function(self, func, *args, **kwargs):
"""分析函数性能"""
self.profiler.enable()
result = func(*args, **kwargs)
self.profiler.disable()
stats = pstats.Stats(self.profiler)
stats.sort_stats('cumulative')
stats.print_stats(20)
return result
def get_hotspots(self, n=10):
"""获取热点函数"""
stats = pstats.Stats(self.profiler)
stats.sort_stats('tottime')
return stats.print_stats(n)
# 安装
sudo apt install ros-iron-tracetools
# 启动跟踪
ros2 launch tracetools launch.py
# 跟踪文件
ls ~/.ros/trace/
# 分析
python3 -m tracetools_analyze ~/.ros/trace/.../
class ParallelProcessor:
def __init__(self, num_workers=4):
from concurrent.futures import ThreadPoolExecutor
self.executor = ThreadPoolExecutor(max_workers=num_workers)
def process_parallel(self, items, process_func):
"""并行处理"""
futures = []
for item in items:
future = self.executor.submit(process_func, item)
futures.append(future)
results = [f.result() for f in futures]
return results
def shutdown(self):
self.executor.shutdown()
class ObjectPool:
"""对象池 - 减少内存分配"""
def __init__(self, factory, pool_size=10):
self.factory = factory
self.pool = [factory() for _ in range(pool_size)]
def acquire(self):
if self.pool:
return self.pool.pop()
return self.factory()
def release(self, obj):
if len(self.pool) < 100: # 限制池大小
self.pool.append(obj)
# 实时内核
uname -r # 检查是否为 PREEMPT_RT 内核
# 线程优先级
chrt -f 10 -p $(pidof node) # FIFO 调度,优先级 10
# CPU 亲和性
taskset -c 2-3 $(pidof node) # 绑定到 CPU 2-3