Network System
0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_metrics_example.cpp
Go to the documentation of this file.
1
/*****************************************************************************
2
BSD 3-Clause License
3
4
Copyright (c) 2024, 🍀☀🌕🌥 🌊
5
All rights reserved.
6
*****************************************************************************/
7
25
#include <
kcenon/network/metrics/metrics.h
>
26
27
#include <cstdlib>
28
#include <iostream>
29
#include <string>
30
31
using namespace
kcenon::network
;
32
33
int
main
()
34
{
35
std::cout <<
"=== Network Metrics Example ==="
<< std::endl;
36
37
// 1. Histogram for latency tracking
38
std::cout <<
"\n1. Histogram (latency tracking):"
<< std::endl;
39
metrics::histogram
latency_hist;
40
41
// Simulate latency measurements
42
double
latencies[] = {1.2, 2.5, 1.8, 3.1, 2.0, 1.5, 4.2, 2.8, 1.9, 2.3};
43
for
(
double
lat : latencies)
44
{
45
latency_hist.
record
(lat);
46
}
47
48
auto
snapshot = latency_hist.get_snapshot();
49
std::cout <<
" Recorded 10 latency measurements"
<< std::endl;
50
std::cout <<
" Snapshot retrieved"
<< std::endl;
51
52
// 2. Metric reporter (static convenience methods)
53
std::cout <<
"\n2. Metric reporter:"
<< std::endl;
54
metrics::metric_reporter::report_connection_accepted
();
55
std::cout <<
" Reported: connection accepted"
<< std::endl;
56
57
metrics::metric_reporter::report_bytes_sent
(1024);
58
std::cout <<
" Reported: 1024 bytes sent"
<< std::endl;
59
60
metrics::metric_reporter::report_latency
(2.5);
61
std::cout <<
" Reported: 2.5ms latency"
<< std::endl;
62
63
metrics::metric_reporter::report_connection_failed
(
"timeout"
);
64
std::cout <<
" Reported: connection failed (timeout)"
<< std::endl;
65
66
// 3. Standard metric names
67
std::cout <<
"\n3. Standard metric names:"
<< std::endl;
68
std::cout <<
" Active connections: "
<<
metrics::metric_names::CONNECTIONS_ACTIVE
<< std::endl;
69
std::cout <<
" Bytes sent: "
<<
metrics::metric_names::BYTES_SENT
<< std::endl;
70
std::cout <<
" Latency: "
<<
metrics::metric_names::LATENCY_MS
<< std::endl;
71
72
std::cout <<
"\nDone."
<< std::endl;
73
return
0;
74
}
kcenon::network::metrics::histogram
Thread-safe histogram for capturing value distributions.
Definition
histogram.h:106
kcenon::network::metrics::histogram::record
void record(double value)
Record a value observation.
Definition
histogram.cpp:203
kcenon::network::metrics::metric_reporter::report_latency
static void report_latency(double ms)
Report network latency.
Definition
network_metrics.cpp:125
kcenon::network::metrics::metric_reporter::report_connection_failed
static void report_connection_failed(const std::string &reason)
Report a failed connection attempt.
Definition
network_metrics.cpp:79
kcenon::network::metrics::metric_reporter::report_connection_accepted
static void report_connection_accepted()
Report a new connection accepted.
Definition
network_metrics.cpp:65
kcenon::network::metrics::metric_reporter::report_bytes_sent
static void report_bytes_sent(size_t bytes)
Report bytes sent.
Definition
network_metrics.cpp:91
metrics.h
Unified metrics header for network_system.
kcenon::network::metrics::metric_names::CONNECTIONS_ACTIVE
constexpr const char * CONNECTIONS_ACTIVE
Definition
network_metrics.h:30
kcenon::network::metrics::metric_names::LATENCY_MS
constexpr const char * LATENCY_MS
Definition
network_metrics.h:41
kcenon::network::metrics::metric_names::BYTES_SENT
constexpr const char * BYTES_SENT
Definition
network_metrics.h:35
kcenon::network
Main namespace for all Network System components.
main
int main()
Definition
network_metrics_example.cpp:33
examples
network_metrics_example.cpp
Generated by
1.12.0