diff --git a/sf-stream/include/StreamStats.hpp b/sf-stream/include/StreamStats.hpp new file mode 100644 index 0000000..a91db57 --- /dev/null +++ b/sf-stream/include/StreamStats.hpp @@ -0,0 +1,30 @@ +#ifndef SF_DAQ_BUFFER_STREAMSTATS_HPP +#define SF_DAQ_BUFFER_STREAMSTATS_HPP + +#include +#include +#include + +class StreamStats { + const std::string detector_name_; + const std::string stream_name_; + const size_t stats_modulo_; + + int image_counter_; + int n_corrupted_images_; + int n_sync_lost_images_; + std::chrono::time_point stats_interval_start_; + + void reset_counters(); + void print_stats(); + +public: + StreamStats(const std::string &detector_name, + const std::string &stream_name, + const size_t stats_modulo); + + void record_stats(const ImageMetadata &meta, const uint32_t n_lost_pulses); +}; + + +#endif //SF_DAQ_BUFFER_STREAMSTATS_HPP diff --git a/sf-stream/src/StreamStats.cpp b/sf-stream/src/StreamStats.cpp new file mode 100644 index 0000000..190f1bc --- /dev/null +++ b/sf-stream/src/StreamStats.cpp @@ -0,0 +1,65 @@ +#include "StreamStats.hpp" + +#include + +using namespace std; +using namespace chrono; + +StreamStats::StreamStats( + const std::string &detector_name, + const std::string &stream_name, + const size_t stats_modulo) : + detector_name_(detector_name), + stream_name_(stream_name), + stats_modulo_(stats_modulo) +{ + reset_counters(); +} + +void StreamStats::reset_counters() +{ + image_counter_ = 0; + n_sync_lost_images_ = 0; + n_corrupted_images_ = 0; + stats_interval_start_ = steady_clock::now(); +} + +void StreamStats::record_stats( + const ImageMetadata &meta, const uint32_t n_lost_pulses) +{ + image_counter_++; + n_sync_lost_images_ += n_lost_pulses; + + if (!meta.is_good_image) { + n_corrupted_images_++; + } + + if (image_counter_ == stats_modulo_) { + print_stats(); + reset_counters(); + } +} + +void StreamStats::print_stats() +{ + auto interval_ms_duration = duration_cast( + steady_clock::now()-stats_interval_start_).count(); + // * 1000 because milliseconds, + 250 because of truncation. + int rep_rate = ((image_counter_ * 1000) + 250) / interval_ms_duration; + uint64_t timestamp = time_point_cast( + system_clock::now()).time_since_epoch().count(); + + // Output in InfluxDB line protocol + cout << "sf-stream"; + cout << ",detector_name=" << detector_name_; + cout << ",stream_name=" << stream_name_; + cout << " "; + cout << "n_processed_images=" << image_counter_ << "i"; + cout << ",n_corrupted_images=" << n_corrupted_images_ << "i"; + cout << ",n_sync_lost_images=" << n_sync_lost_images_ << "i"; + cout << ",repetition_rate=" << rep_rate << "i"; + cout << " "; + cout << timestamp; + cout << endl; +} +