Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::timer_data Struct Reference

Timer data with percentile calculations. More...

#include <metric_types.h>

Collaboration diagram for kcenon::monitoring::timer_data:
Collaboration graph

Classes

struct  snapshot
 Get snapshot of current statistics. More...
 

Public Member Functions

 timer_data ()
 Construct timer with default reservoir size.
 
 timer_data (size_t reservoir_size)
 Construct timer with custom reservoir size.
 
void record (double duration_ms)
 Record a duration sample (in milliseconds)
 
template<typename Rep , typename Period >
void record (std::chrono::duration< Rep, Period > duration)
 Record a duration using chrono duration.
 
double get_percentile (double percentile) const
 Get percentile value (0-100)
 
double median () const
 Get median (p50)
 
double p90 () const
 Get p90 value.
 
double p95 () const
 Get p95 value.
 
double p99 () const
 Get p99 value.
 
double p999 () const
 Get p999 value (99.9th percentile)
 
double mean () const noexcept
 Get mean value.
 
uint64_t count () const noexcept
 Get sample count.
 
double min () const noexcept
 Get minimum recorded value.
 
double max () const noexcept
 Get maximum recorded value.
 
double stddev () const
 Get standard deviation.
 
void reset ()
 Reset all data.
 
snapshot get_snapshot () const
 

Public Attributes

std::vector< double > samples
 
size_t max_samples
 
uint64_t total_count = 0
 
double sum = 0.0
 
double min_value = (std::numeric_limits<double>::max)()
 
double max_value = (std::numeric_limits<double>::lowest)()
 
bool sorted = false
 

Static Public Attributes

static constexpr size_t DEFAULT_RESERVOIR_SIZE = 1024
 

Private Member Functions

void ensure_sorted () const
 

Detailed Description

Timer data with percentile calculations.

Stores duration samples and provides percentile calculations. Uses a reservoir sampling approach for memory efficiency.

Examples
custom_metric_types_example.cpp.

Definition at line 359 of file metric_types.h.

Constructor & Destructor Documentation

◆ timer_data() [1/2]

kcenon::monitoring::timer_data::timer_data ( )
inline

Construct timer with default reservoir size.

Definition at line 373 of file metric_types.h.

374 samples.reserve(max_samples);
375 }
std::vector< double > samples
static constexpr size_t DEFAULT_RESERVOIR_SIZE

References max_samples, and samples.

◆ timer_data() [2/2]

kcenon::monitoring::timer_data::timer_data ( size_t reservoir_size)
inlineexplicit

Construct timer with custom reservoir size.

Definition at line 380 of file metric_types.h.

381 : max_samples(reservoir_size) {
382 samples.reserve(max_samples);
383 }

References max_samples, and samples.

Member Function Documentation

◆ count()

uint64_t kcenon::monitoring::timer_data::count ( ) const
inlinenoexcept

Get sample count.

Examples
custom_metric_types_example.cpp.

Definition at line 485 of file metric_types.h.

485 {
486 return total_count;
487 }

References total_count.

Referenced by demonstrate_timer_scope().

Here is the caller graph for this function:

◆ ensure_sorted()

void kcenon::monitoring::timer_data::ensure_sorted ( ) const
inlineprivate

Definition at line 564 of file metric_types.h.

564 {
565 if (!sorted && !samples.empty()) {
566 auto& mutable_samples = const_cast<std::vector<double>&>(samples);
567 std::sort(mutable_samples.begin(), mutable_samples.end());
568 const_cast<bool&>(sorted) = true;
569 }
570 }

References samples, and sorted.

Referenced by get_percentile().

Here is the caller graph for this function:

◆ get_percentile()

double kcenon::monitoring::timer_data::get_percentile ( double percentile) const
inline

Get percentile value (0-100)

Parameters
percentileThe percentile to calculate (e.g., 50 for median, 99 for p99)
Returns
The value at the given percentile

Definition at line 420 of file metric_types.h.

420 {
421 if (samples.empty()) return 0.0;
422 if (percentile <= 0) return min_value;
423 if (percentile >= 100) return max_value;
424
426
427 double rank = (percentile / 100.0) * (samples.size() - 1);
428 size_t lower_idx = static_cast<size_t>(rank);
429 size_t upper_idx = lower_idx + 1;
430 double fraction = rank - lower_idx;
431
432 if (upper_idx >= samples.size()) {
433 return samples[lower_idx];
434 }
435
436 // Linear interpolation between adjacent values
437 return samples[lower_idx] + fraction * (samples[upper_idx] - samples[lower_idx]);
438 }
T percentile(const std::vector< T > &sorted_values, double percentile_value)
Definition statistics.h:130

References ensure_sorted(), max_value, min_value, and samples.

Referenced by median(), p90(), p95(), p99(), and p999().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_snapshot()

snapshot kcenon::monitoring::timer_data::get_snapshot ( ) const
inline

Definition at line 548 of file metric_types.h.

548 {
549 return snapshot{
551 mean(),
552 min(),
553 max(),
554 stddev(),
555 median(),
556 p90(),
557 p95(),
558 p99(),
559 p999()
560 };
561 }
double p99() const
Get p99 value.
double p90() const
Get p90 value.
double mean() const noexcept
Get mean value.
double p95() const
Get p95 value.
double stddev() const
Get standard deviation.
double max() const noexcept
Get maximum recorded value.
double median() const
Get median (p50)
double p999() const
Get p999 value (99.9th percentile)
double min() const noexcept
Get minimum recorded value.

References max(), mean(), median(), min(), p90(), p95(), p99(), p999(), stddev(), and total_count.

Referenced by demonstrate_timer(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ max()

double kcenon::monitoring::timer_data::max ( ) const
inlinenoexcept

Get maximum recorded value.

Examples
custom_metric_types_example.cpp.

Definition at line 499 of file metric_types.h.

499 {
500 return total_count > 0 ? max_value : 0.0;
501 }

References max_value, and total_count.

Referenced by demonstrate_timer_scope(), and get_snapshot().

Here is the caller graph for this function:

◆ mean()

double kcenon::monitoring::timer_data::mean ( ) const
inlinenoexcept

Get mean value.

Examples
custom_metric_types_example.cpp.

Definition at line 478 of file metric_types.h.

478 {
479 return total_count > 0 ? sum / total_count : 0.0;
480 }

References sum, and total_count.

Referenced by demonstrate_timer_scope(), get_snapshot(), and stddev().

Here is the caller graph for this function:

◆ median()

double kcenon::monitoring::timer_data::median ( ) const
inline

Get median (p50)

Definition at line 443 of file metric_types.h.

443 {
444 return get_percentile(50.0);
445 }
double get_percentile(double percentile) const
Get percentile value (0-100)

References get_percentile().

Referenced by get_snapshot(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ min()

double kcenon::monitoring::timer_data::min ( ) const
inlinenoexcept

Get minimum recorded value.

Examples
custom_metric_types_example.cpp.

Definition at line 492 of file metric_types.h.

492 {
493 return total_count > 0 ? min_value : 0.0;
494 }

References min_value, and total_count.

Referenced by demonstrate_timer_scope(), and get_snapshot().

Here is the caller graph for this function:

◆ p90()

double kcenon::monitoring::timer_data::p90 ( ) const
inline

Get p90 value.

Definition at line 450 of file metric_types.h.

450 {
451 return get_percentile(90.0);
452 }

References get_percentile().

Referenced by get_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ p95()

double kcenon::monitoring::timer_data::p95 ( ) const
inline

Get p95 value.

Examples
custom_metric_types_example.cpp.

Definition at line 457 of file metric_types.h.

457 {
458 return get_percentile(95.0);
459 }

References get_percentile().

Referenced by demonstrate_timer_scope(), and get_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ p99()

double kcenon::monitoring::timer_data::p99 ( ) const
inline

Get p99 value.

Definition at line 464 of file metric_types.h.

464 {
465 return get_percentile(99.0);
466 }

References get_percentile().

Referenced by get_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ p999()

double kcenon::monitoring::timer_data::p999 ( ) const
inline

Get p999 value (99.9th percentile)

Definition at line 471 of file metric_types.h.

471 {
472 return get_percentile(99.9);
473 }

References get_percentile().

Referenced by get_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ record() [1/2]

void kcenon::monitoring::timer_data::record ( double duration_ms)
inline

Record a duration sample (in milliseconds)

Definition at line 388 of file metric_types.h.

388 {
389 total_count++;
390 sum += duration_ms;
391 min_value = (std::min)(min_value, duration_ms);
392 max_value = (std::max)(max_value, duration_ms);
393 sorted = false;
394
395 if (samples.size() < max_samples) {
396 samples.push_back(duration_ms);
397 } else {
398 // Reservoir sampling for memory efficiency
399 size_t idx = static_cast<size_t>(rand()) % total_count;
400 if (idx < max_samples) {
401 samples[idx] = duration_ms;
402 }
403 }
404 }

References max_samples, max_value, min_value, samples, sorted, sum, and total_count.

Referenced by demonstrate_timer(), record(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and kcenon::monitoring::timer_scope::~timer_scope().

Here is the caller graph for this function:

◆ record() [2/2]

template<typename Rep , typename Period >
void kcenon::monitoring::timer_data::record ( std::chrono::duration< Rep, Period > duration)
inline

Record a duration using chrono duration.

Definition at line 410 of file metric_types.h.

410 {
411 auto ms = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() / 1000.0;
412 record(ms);
413 }
void record(double duration_ms)
Record a duration sample (in milliseconds)

References record().

Here is the call graph for this function:

◆ reset()

void kcenon::monitoring::timer_data::reset ( )
inline

Reset all data.

Definition at line 522 of file metric_types.h.

522 {
523 samples.clear();
524 samples.reserve(max_samples);
525 total_count = 0;
526 sum = 0.0;
527 min_value = (std::numeric_limits<double>::max)();
528 max_value = (std::numeric_limits<double>::lowest)();
529 sorted = false;
530 }

References max_samples, max_value, min_value, samples, sorted, sum, and total_count.

◆ stddev()

double kcenon::monitoring::timer_data::stddev ( ) const
inline

Get standard deviation.

Definition at line 506 of file metric_types.h.

506 {
507 if (samples.size() < 2) return 0.0;
508
509 double avg = mean();
510 double variance = 0.0;
511 for (double sample : samples) {
512 double diff = sample - avg;
513 variance += diff * diff;
514 }
515 variance /= samples.size();
516 return std::sqrt(variance);
517 }

References mean(), and samples.

Referenced by get_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ DEFAULT_RESERVOIR_SIZE

size_t kcenon::monitoring::timer_data::DEFAULT_RESERVOIR_SIZE = 1024
staticconstexpr

Definition at line 360 of file metric_types.h.

◆ max_samples

size_t kcenon::monitoring::timer_data::max_samples

Definition at line 363 of file metric_types.h.

Referenced by record(), reset(), timer_data(), and timer_data().

◆ max_value

double kcenon::monitoring::timer_data::max_value = (std::numeric_limits<double>::lowest)()

Definition at line 367 of file metric_types.h.

Referenced by get_percentile(), max(), record(), and reset().

◆ min_value

double kcenon::monitoring::timer_data::min_value = (std::numeric_limits<double>::max)()

Definition at line 366 of file metric_types.h.

Referenced by get_percentile(), min(), record(), and reset().

◆ samples

std::vector<double> kcenon::monitoring::timer_data::samples

◆ sorted

bool kcenon::monitoring::timer_data::sorted = false
mutable

Definition at line 368 of file metric_types.h.

Referenced by ensure_sorted(), record(), and reset().

◆ sum

double kcenon::monitoring::timer_data::sum = 0.0

Definition at line 365 of file metric_types.h.

Referenced by mean(), record(), and reset().

◆ total_count

uint64_t kcenon::monitoring::timer_data::total_count = 0

Definition at line 364 of file metric_types.h.

Referenced by count(), get_snapshot(), max(), mean(), min(), record(), and reset().


The documentation for this struct was generated from the following file: