utils_performance_monitor.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. #include "../base/types.h"
  3. #include <memory>
  4. #include <chrono>
  5. #include <string>
  6. #include <map>
  7. #include <vector>
  8. #include <atomic>
  9. #include <mutex>
  10. #include <functional>
  11. namespace av {
  12. namespace utils {
  13. using namespace av::base;
  14. // 性能指标类型
  15. enum class MetricType {
  16. COUNTER, // 计数器
  17. GAUGE, // 仪表盘(当前值)
  18. HISTOGRAM, // 直方图
  19. TIMER, // 计时器
  20. RATE // 速率
  21. };
  22. // 性能统计信息
  23. struct PerformanceStats {
  24. std::string name; // 指标名称
  25. MetricType type; // 指标类型
  26. double value = 0.0; // 当前值
  27. double minValue = 0.0; // 最小值
  28. double maxValue = 0.0; // 最大值
  29. double avgValue = 0.0; // 平均值
  30. uint64_t count = 0; // 计数
  31. uint64_t totalCount = 0; // 总计数
  32. double rate = 0.0; // 速率(每秒)
  33. std::chrono::steady_clock::time_point lastUpdateTime; // 最后更新时间
  34. std::chrono::steady_clock::time_point startTime; // 开始时间
  35. };
  36. // 性能监控配置
  37. struct MonitorConfig {
  38. bool enableMonitoring = true; // 启用监控
  39. bool enableLogging = true; // 启用日志记录
  40. bool enableReporting = true; // 启用报告
  41. double reportInterval = 10.0; // 报告间隔(秒)
  42. size_t maxHistorySize = 1000; // 最大历史记录数
  43. bool enableAlerts = false; // 启用告警
  44. double alertThreshold = 0.0; // 告警阈值
  45. };
  46. // 性能告警信息
  47. struct PerformanceAlert {
  48. std::string metricName; // 指标名称
  49. MetricType type; // 指标类型
  50. double currentValue; // 当前值
  51. double threshold; // 阈值
  52. std::string message; // 告警消息
  53. std::chrono::steady_clock::time_point timestamp; // 时间戳
  54. };
  55. // 性能报告回调
  56. using PerformanceReportCallback = std::function<void(const std::map<std::string, PerformanceStats>&)>;
  57. using PerformanceAlertCallback = std::function<void(const PerformanceAlert&)>;
  58. // 性能监控器
  59. class PerformanceMonitor {
  60. public:
  61. explicit PerformanceMonitor(const MonitorConfig& config = MonitorConfig());
  62. virtual ~PerformanceMonitor();
  63. // 基本控制
  64. ErrorCode initialize();
  65. ErrorCode start();
  66. ErrorCode stop();
  67. ErrorCode reset();
  68. ErrorCode close();
  69. // 指标管理
  70. ErrorCode registerMetric(const std::string& name, MetricType type, double alertThreshold = 0.0);
  71. ErrorCode unregisterMetric(const std::string& name);
  72. bool hasMetric(const std::string& name) const;
  73. std::vector<std::string> getMetricNames() const;
  74. // 计数器操作
  75. ErrorCode incrementCounter(const std::string& name, double value = 1.0);
  76. ErrorCode decrementCounter(const std::string& name, double value = 1.0);
  77. ErrorCode setCounter(const std::string& name, double value);
  78. double getCounter(const std::string& name) const;
  79. // 仪表盘操作
  80. ErrorCode setGauge(const std::string& name, double value);
  81. ErrorCode updateGauge(const std::string& name, double delta);
  82. double getGauge(const std::string& name) const;
  83. // 直方图操作
  84. ErrorCode recordHistogram(const std::string& name, double value);
  85. PerformanceStats getHistogramStats(const std::string& name) const;
  86. // 计时器操作
  87. ErrorCode startTimer(const std::string& name);
  88. ErrorCode stopTimer(const std::string& name);
  89. ErrorCode recordTimer(const std::string& name, double duration);
  90. double getTimerAverage(const std::string& name) const;
  91. // 速率操作
  92. ErrorCode recordRate(const std::string& name, double value = 1.0);
  93. double getRate(const std::string& name) const;
  94. // 统计信息
  95. PerformanceStats getStats(const std::string& name) const;
  96. std::map<std::string, PerformanceStats> getAllStats() const;
  97. void resetStats(const std::string& name);
  98. void resetAllStats();
  99. // 配置管理
  100. void setConfig(const MonitorConfig& config);
  101. MonitorConfig getConfig() const;
  102. void enableMonitoring(bool enable);
  103. void enableLogging(bool enable);
  104. void enableReporting(bool enable);
  105. void setReportInterval(double interval);
  106. // 回调设置
  107. void setReportCallback(PerformanceReportCallback callback);
  108. void setAlertCallback(PerformanceAlertCallback callback);
  109. // 报告和导出
  110. std::string generateReport() const;
  111. ErrorCode exportToFile(const std::string& filename) const;
  112. ErrorCode exportToJson(const std::string& filename) const;
  113. ErrorCode exportToCsv(const std::string& filename) const;
  114. // 告警管理
  115. void setAlertThreshold(const std::string& name, double threshold);
  116. std::vector<PerformanceAlert> getActiveAlerts() const;
  117. void clearAlerts();
  118. private:
  119. // 内部方法
  120. void updateMetric(const std::string& name, double value, bool isIncrement = false);
  121. void checkAlerts(const std::string& name, double value);
  122. void generatePeriodicReport();
  123. void logMetricUpdate(const std::string& name, double value);
  124. bool isMetricValid(const std::string& name) const;
  125. void cleanupOldData();
  126. void updateRates();
  127. // 统计计算
  128. void updateStatistics(PerformanceStats& stats, double value);
  129. double calculateRate(const PerformanceStats& stats) const;
  130. void updateHistogram(PerformanceStats& stats, double value);
  131. private:
  132. MonitorConfig config_;
  133. // 指标存储
  134. std::map<std::string, PerformanceStats> metrics_;
  135. std::map<std::string, MetricType> metricTypes_;
  136. std::map<std::string, double> alertThresholds_;
  137. // 计时器状态
  138. std::map<std::string, std::chrono::steady_clock::time_point> timerStarts_;
  139. // 历史数据
  140. std::map<std::string, std::vector<double>> historyData_;
  141. // 告警信息
  142. std::vector<PerformanceAlert> activeAlerts_;
  143. // 回调函数
  144. PerformanceReportCallback reportCallback_;
  145. PerformanceAlertCallback alertCallback_;
  146. // 状态管理
  147. std::atomic<bool> initialized_;
  148. std::atomic<bool> running_;
  149. std::atomic<bool> monitoring_;
  150. // 线程同步
  151. mutable std::mutex metricsMutex_;
  152. mutable std::mutex alertsMutex_;
  153. mutable std::mutex configMutex_;
  154. // 时间管理
  155. std::chrono::steady_clock::time_point startTime_;
  156. std::chrono::steady_clock::time_point lastReportTime_;
  157. };
  158. // 性能监控作用域计时器
  159. class ScopedTimer {
  160. public:
  161. ScopedTimer(PerformanceMonitor* monitor, const std::string& name);
  162. ~ScopedTimer();
  163. // 禁止拷贝
  164. ScopedTimer(const ScopedTimer&) = delete;
  165. ScopedTimer& operator=(const ScopedTimer&) = delete;
  166. private:
  167. PerformanceMonitor* monitor_;
  168. std::string name_;
  169. std::chrono::steady_clock::time_point startTime_;
  170. };
  171. // 性能监控宏
  172. #define PERF_MONITOR_COUNTER_INC(monitor, name) \
  173. do { if (monitor) monitor->incrementCounter(name); } while(0)
  174. #define PERF_MONITOR_COUNTER_DEC(monitor, name) \
  175. do { if (monitor) monitor->decrementCounter(name); } while(0)
  176. #define PERF_MONITOR_GAUGE_SET(monitor, name, value) \
  177. do { if (monitor) monitor->setGauge(name, value); } while(0)
  178. #define PERF_MONITOR_HISTOGRAM_RECORD(monitor, name, value) \
  179. do { if (monitor) monitor->recordHistogram(name, value); } while(0)
  180. #define PERF_MONITOR_RATE_RECORD(monitor, name) \
  181. do { if (monitor) monitor->recordRate(name); } while(0)
  182. #define PERF_MONITOR_TIMER_SCOPE(monitor, name) \
  183. av::utils::ScopedTimer _timer(monitor, name)
  184. // 系统性能监控器
  185. class SystemPerformanceMonitor {
  186. public:
  187. SystemPerformanceMonitor();
  188. virtual ~SystemPerformanceMonitor();
  189. // 系统指标收集
  190. ErrorCode collectCpuUsage();
  191. ErrorCode collectMemoryUsage();
  192. ErrorCode collectDiskUsage();
  193. ErrorCode collectNetworkUsage();
  194. ErrorCode collectGpuUsage();
  195. // 获取系统指标
  196. double getCpuUsage() const;
  197. double getMemoryUsage() const;
  198. double getDiskUsage() const;
  199. double getNetworkUsage() const;
  200. double getGpuUsage() const;
  201. // 系统信息
  202. std::string getSystemInfo() const;
  203. std::string getCpuInfo() const;
  204. std::string getMemoryInfo() const;
  205. std::string getGpuInfo() const;
  206. // 集成到性能监控器
  207. ErrorCode integrateWithMonitor(PerformanceMonitor* monitor);
  208. private:
  209. void updateSystemMetrics();
  210. private:
  211. std::atomic<double> cpuUsage_;
  212. std::atomic<double> memoryUsage_;
  213. std::atomic<double> diskUsage_;
  214. std::atomic<double> networkUsage_;
  215. std::atomic<double> gpuUsage_;
  216. mutable std::mutex systemMutex_;
  217. std::chrono::steady_clock::time_point lastUpdateTime_;
  218. };
  219. // 性能监控工厂
  220. class PerformanceMonitorFactory {
  221. public:
  222. // 创建标准性能监控器
  223. static std::unique_ptr<PerformanceMonitor> createStandardMonitor();
  224. // 创建轻量级监控器
  225. static std::unique_ptr<PerformanceMonitor> createLightweightMonitor();
  226. // 创建详细监控器
  227. static std::unique_ptr<PerformanceMonitor> createDetailedMonitor();
  228. // 创建实时监控器
  229. static std::unique_ptr<PerformanceMonitor> createRealtimeMonitor();
  230. // 创建系统监控器
  231. static std::unique_ptr<SystemPerformanceMonitor> createSystemMonitor();
  232. };
  233. } // namespace utils
  234. } // namespace av