30 std::cout <<
"=== Basic Monitoring Example ===" << std::endl;
39 std::cout <<
"1. Creating monitoring system with configuration:" << std::endl;
40 std::cout <<
" - History size: " << config.
history_size << std::endl;
41 std::cout <<
" - Collection interval: 1000ms" << std::endl;
47 if (
auto result = perf_monitor.
initialize(); result.is_err()) {
48 std::cerr <<
"Failed to initialize performance monitor: "
49 << result.error().message << std::endl;
53 std::cout <<
"2. Initialized performance monitor" << std::endl;
57 storage_cfg.
type = storage_backend_type::file_json;
58 storage_cfg.
path =
"monitoring_data.json";
60 auto storage = std::make_unique<file_storage_backend>(storage_cfg);
66 std::cout <<
"3. Configured JSON file storage backend" << std::endl;
68 std::cout <<
"4. Monitoring system ready" << std::endl;
69 std::cout << std::endl;
72 std::cout <<
"5. Simulating application workload..." << std::endl;
74 for (
int i = 0; i < 10; ++i) {
75 std::cout <<
" Iteration " << (i + 1) <<
"/10" << std::endl;
80 std::this_thread::sleep_for(100ms);
84 std::this_thread::sleep_for(500ms);
90 <<
"%, Memory: " << (
system_metrics.value().memory_usage_bytes / (1024.0 * 1024.0)) <<
" MB" << std::endl;
94 std::cout << std::endl;
97 std::cout <<
"6. Collecting metrics:" << std::endl;
99 auto metrics_result = perf_monitor.
collect();
100 if (metrics_result.is_ok()) {
101 auto& snapshot = metrics_result.value();
102 std::cout <<
" Total metrics collected: " << snapshot.metrics.size() << std::endl;
104 for (
const auto&
metric : snapshot.metrics) {
105 std::cout <<
" - Metric: " <<
metric.
name << std::endl;
109 std::cout << std::endl;
112 if (
auto result = perf_monitor.
cleanup(); result.is_err()) {
113 std::cerr <<
"Failed to cleanup: "
114 << result.error().message << std::endl;
119 if (
auto result =
storage->flush(); result.is_err()) {
120 std::cerr <<
"Failed to flush storage: "
121 << result.error().message << std::endl;
124 std::cout << std::endl;
125 std::cout <<
"7. Monitoring completed successfully" << std::endl;
126 std::cout <<
" Data saved to: monitoring_data.json" << std::endl;
128 }
catch (
const std::exception& e) {
129 std::cerr <<
"Exception: " << e.what() << std::endl;
133 std::cout << std::endl;
134 std::cout <<
"=== Example completed successfully ===" << std::endl;