| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #pragma once
- #include "../base/types.h"
- #include <memory>
- #include <chrono>
- #include <string>
- #include <map>
- #include <vector>
- #include <atomic>
- #include <mutex>
- #include <functional>
- namespace av {
- namespace utils {
- using namespace av::base;
- // 性能指标类型
- enum class MetricType {
- COUNTER, // 计数器
- GAUGE, // 仪表盘(当前值)
- HISTOGRAM, // 直方图
- TIMER, // 计时器
- RATE // 速率
- };
- // 性能统计信息
- struct PerformanceStats {
- std::string name; // 指标名称
- MetricType type; // 指标类型
- double value = 0.0; // 当前值
- double minValue = 0.0; // 最小值
- double maxValue = 0.0; // 最大值
- double avgValue = 0.0; // 平均值
- uint64_t count = 0; // 计数
- uint64_t totalCount = 0; // 总计数
- double rate = 0.0; // 速率(每秒)
- std::chrono::steady_clock::time_point lastUpdateTime; // 最后更新时间
- std::chrono::steady_clock::time_point startTime; // 开始时间
- };
- // 性能监控配置
- struct MonitorConfig {
- bool enableMonitoring = true; // 启用监控
- bool enableLogging = true; // 启用日志记录
- bool enableReporting = true; // 启用报告
- double reportInterval = 10.0; // 报告间隔(秒)
- size_t maxHistorySize = 1000; // 最大历史记录数
- bool enableAlerts = false; // 启用告警
- double alertThreshold = 0.0; // 告警阈值
- };
- // 性能告警信息
- struct PerformanceAlert {
- std::string metricName; // 指标名称
- MetricType type; // 指标类型
- double currentValue; // 当前值
- double threshold; // 阈值
- std::string message; // 告警消息
- std::chrono::steady_clock::time_point timestamp; // 时间戳
- };
- // 性能报告回调
- using PerformanceReportCallback = std::function<void(const std::map<std::string, PerformanceStats>&)>;
- using PerformanceAlertCallback = std::function<void(const PerformanceAlert&)>;
- // 性能监控器
- class PerformanceMonitor {
- public:
- explicit PerformanceMonitor(const MonitorConfig& config = MonitorConfig());
- virtual ~PerformanceMonitor();
- // 基本控制
- ErrorCode initialize();
- ErrorCode start();
- ErrorCode stop();
- ErrorCode reset();
- ErrorCode close();
- // 指标管理
- ErrorCode registerMetric(const std::string& name, MetricType type, double alertThreshold = 0.0);
- ErrorCode unregisterMetric(const std::string& name);
- bool hasMetric(const std::string& name) const;
- std::vector<std::string> getMetricNames() const;
- // 计数器操作
- ErrorCode incrementCounter(const std::string& name, double value = 1.0);
- ErrorCode decrementCounter(const std::string& name, double value = 1.0);
- ErrorCode setCounter(const std::string& name, double value);
- double getCounter(const std::string& name) const;
- // 仪表盘操作
- ErrorCode setGauge(const std::string& name, double value);
- ErrorCode updateGauge(const std::string& name, double delta);
- double getGauge(const std::string& name) const;
- // 直方图操作
- ErrorCode recordHistogram(const std::string& name, double value);
- PerformanceStats getHistogramStats(const std::string& name) const;
- // 计时器操作
- ErrorCode startTimer(const std::string& name);
- ErrorCode stopTimer(const std::string& name);
- ErrorCode recordTimer(const std::string& name, double duration);
- double getTimerAverage(const std::string& name) const;
- // 速率操作
- ErrorCode recordRate(const std::string& name, double value = 1.0);
- double getRate(const std::string& name) const;
- // 统计信息
- PerformanceStats getStats(const std::string& name) const;
- std::map<std::string, PerformanceStats> getAllStats() const;
- void resetStats(const std::string& name);
- void resetAllStats();
- // 配置管理
- void setConfig(const MonitorConfig& config);
- MonitorConfig getConfig() const;
- void enableMonitoring(bool enable);
- void enableLogging(bool enable);
- void enableReporting(bool enable);
- void setReportInterval(double interval);
- // 回调设置
- void setReportCallback(PerformanceReportCallback callback);
- void setAlertCallback(PerformanceAlertCallback callback);
- // 报告和导出
- std::string generateReport() const;
- ErrorCode exportToFile(const std::string& filename) const;
- ErrorCode exportToJson(const std::string& filename) const;
- ErrorCode exportToCsv(const std::string& filename) const;
- // 告警管理
- void setAlertThreshold(const std::string& name, double threshold);
- std::vector<PerformanceAlert> getActiveAlerts() const;
- void clearAlerts();
- private:
- // 内部方法
- void updateMetric(const std::string& name, double value, bool isIncrement = false);
- void checkAlerts(const std::string& name, double value);
- void generatePeriodicReport();
- void logMetricUpdate(const std::string& name, double value);
- bool isMetricValid(const std::string& name) const;
- void cleanupOldData();
- void updateRates();
-
- // 统计计算
- void updateStatistics(PerformanceStats& stats, double value);
- double calculateRate(const PerformanceStats& stats) const;
- void updateHistogram(PerformanceStats& stats, double value);
-
- private:
- MonitorConfig config_;
-
- // 指标存储
- std::map<std::string, PerformanceStats> metrics_;
- std::map<std::string, MetricType> metricTypes_;
- std::map<std::string, double> alertThresholds_;
-
- // 计时器状态
- std::map<std::string, std::chrono::steady_clock::time_point> timerStarts_;
-
- // 历史数据
- std::map<std::string, std::vector<double>> historyData_;
-
- // 告警信息
- std::vector<PerformanceAlert> activeAlerts_;
-
- // 回调函数
- PerformanceReportCallback reportCallback_;
- PerformanceAlertCallback alertCallback_;
-
- // 状态管理
- std::atomic<bool> initialized_;
- std::atomic<bool> running_;
- std::atomic<bool> monitoring_;
-
- // 线程同步
- mutable std::mutex metricsMutex_;
- mutable std::mutex alertsMutex_;
- mutable std::mutex configMutex_;
-
- // 时间管理
- std::chrono::steady_clock::time_point startTime_;
- std::chrono::steady_clock::time_point lastReportTime_;
- };
- // 性能监控作用域计时器
- class ScopedTimer {
- public:
- ScopedTimer(PerformanceMonitor* monitor, const std::string& name);
- ~ScopedTimer();
-
- // 禁止拷贝
- ScopedTimer(const ScopedTimer&) = delete;
- ScopedTimer& operator=(const ScopedTimer&) = delete;
-
- private:
- PerformanceMonitor* monitor_;
- std::string name_;
- std::chrono::steady_clock::time_point startTime_;
- };
- // 性能监控宏
- #define PERF_MONITOR_COUNTER_INC(monitor, name) \
- do { if (monitor) monitor->incrementCounter(name); } while(0)
- #define PERF_MONITOR_COUNTER_DEC(monitor, name) \
- do { if (monitor) monitor->decrementCounter(name); } while(0)
- #define PERF_MONITOR_GAUGE_SET(monitor, name, value) \
- do { if (monitor) monitor->setGauge(name, value); } while(0)
- #define PERF_MONITOR_HISTOGRAM_RECORD(monitor, name, value) \
- do { if (monitor) monitor->recordHistogram(name, value); } while(0)
- #define PERF_MONITOR_RATE_RECORD(monitor, name) \
- do { if (monitor) monitor->recordRate(name); } while(0)
- #define PERF_MONITOR_TIMER_SCOPE(monitor, name) \
- av::utils::ScopedTimer _timer(monitor, name)
- // 系统性能监控器
- class SystemPerformanceMonitor {
- public:
- SystemPerformanceMonitor();
- virtual ~SystemPerformanceMonitor();
- // 系统指标收集
- ErrorCode collectCpuUsage();
- ErrorCode collectMemoryUsage();
- ErrorCode collectDiskUsage();
- ErrorCode collectNetworkUsage();
- ErrorCode collectGpuUsage();
-
- // 获取系统指标
- double getCpuUsage() const;
- double getMemoryUsage() const;
- double getDiskUsage() const;
- double getNetworkUsage() const;
- double getGpuUsage() const;
-
- // 系统信息
- std::string getSystemInfo() const;
- std::string getCpuInfo() const;
- std::string getMemoryInfo() const;
- std::string getGpuInfo() const;
-
- // 集成到性能监控器
- ErrorCode integrateWithMonitor(PerformanceMonitor* monitor);
-
- private:
- void updateSystemMetrics();
-
- private:
- std::atomic<double> cpuUsage_;
- std::atomic<double> memoryUsage_;
- std::atomic<double> diskUsage_;
- std::atomic<double> networkUsage_;
- std::atomic<double> gpuUsage_;
-
- mutable std::mutex systemMutex_;
- std::chrono::steady_clock::time_point lastUpdateTime_;
- };
- // 性能监控工厂
- class PerformanceMonitorFactory {
- public:
- // 创建标准性能监控器
- static std::unique_ptr<PerformanceMonitor> createStandardMonitor();
-
- // 创建轻量级监控器
- static std::unique_ptr<PerformanceMonitor> createLightweightMonitor();
-
- // 创建详细监控器
- static std::unique_ptr<PerformanceMonitor> createDetailedMonitor();
-
- // 创建实时监控器
- static std::unique_ptr<PerformanceMonitor> createRealtimeMonitor();
-
- // 创建系统监控器
- static std::unique_ptr<SystemPerformanceMonitor> createSystemMonitor();
- };
- } // namespace utils
- } // namespace av
|