#include <iostream>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
std::cout << "=== Basic Monitoring Example ===" << std::endl;
try {
std::cout << "1. Creating monitoring system with configuration:" << std::endl;
std::cout <<
" - History size: " << config.
history_size << std::endl;
std::cout << " - Collection interval: 1000ms" << std::endl;
if (
auto result = perf_monitor.
initialize(); result.is_err()) {
std::cerr << "Failed to initialize performance monitor: "
<< result.error().message << std::endl;
return 1;
}
std::cout << "2. Initialized performance monitor" << std::endl;
storage_cfg.
type = storage_backend_type::file_json;
storage_cfg.
path =
"monitoring_data.json";
auto storage = std::make_unique<file_storage_backend>(storage_cfg);
std::cout << "3. Configured JSON file storage backend" << std::endl;
std::cout << "4. Monitoring system ready" << std::endl;
std::cout << std::endl;
std::cout << "5. Simulating application workload..." << std::endl;
for (int i = 0; i < 10; ++i) {
std::cout << " Iteration " << (i + 1) << "/10" << std::endl;
{
auto timer = perf_monitor.
time_operation(
"iteration_" + std::to_string(i));
std::this_thread::sleep_for(100ms);
}
std::this_thread::sleep_for(500ms);
<<
"%, Memory: " << (
system_metrics.value().memory_usage_bytes / (1024.0 * 1024.0)) <<
" MB" << std::endl;
}
}
std::cout << std::endl;
std::cout << "6. Collecting metrics:" << std::endl;
auto metrics_result = perf_monitor.
collect();
if (metrics_result.is_ok()) {
auto& snapshot = metrics_result.value();
std::cout << " Total metrics collected: " << snapshot.metrics.size() << std::endl;
for (
const auto&
metric : snapshot.metrics) {
std::cout <<
" - Metric: " <<
metric.
name << std::endl;
}
}
std::cout << std::endl;
if (
auto result = perf_monitor.
cleanup(); result.is_err()) {
std::cerr << "Failed to cleanup: "
<< result.error().message << std::endl;
return 1;
}
if (auto result = storage->flush(); result.is_err()) {
std::cerr << "Failed to flush storage: "
<< result.error().message << std::endl;
}
std::cout << std::endl;
std::cout << "7. Monitoring completed successfully" << std::endl;
std::cout << " Data saved to: monitoring_data.json" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
std::cout << std::endl;
std::cout << "=== Example completed successfully ===" << std::endl;
return 0;
}
common::Result< system_metrics > get_current_metrics() const
Get current system metrics.
Core monitoring system interface definitions.
Result pattern type definitions for monitoring system.
Storage backend type definitions for metric persistence.
Basic metric structure for interface compatibility.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval
storage_backend_type type