| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #ifndef SOPHIAR2_STATISTIC_TIMER_HPP
- #define SOPHIAR2_STATISTIC_TIMER_HPP
- #include "core/timestamp_helper.hpp"
- #include <boost/accumulators/accumulators.hpp>
- #include <boost/accumulators/statistics/max.hpp>
- #include <boost/accumulators/statistics/min.hpp>
- #include <boost/accumulators/statistics/stats.hpp>
- #include <boost/accumulators/statistics/variance.hpp>
- #include <fmt/format.h>
- #include <cassert>
- #include <cmath>
- #include <string>
- #include <type_traits>
- namespace sophiar {
- struct statistic_timer_single_shot_tag;
- struct statistic_timer_start_stop_tag;
- template<typename ModeTag>
- class statistic_timer {
- public:
- void start(timestamp_type current_ts = current_timestamp());
- template<typename CurrentModeTag = ModeTag>
- std::enable_if_t<std::is_same_v<CurrentModeTag, statistic_timer_start_stop_tag>>
- stop(timestamp_type current_ts = current_timestamp()) {
- assert(is_running);
- is_running = false;
- accumulator(current_ts - last_ts);
- }
- void reset() {
- accumulator = {};
- }
- auto get_count() const {
- return boost::accumulators::count(accumulator);
- }
- auto get_min() const {
- return boost::accumulators::min(accumulator);
- }
- auto get_max() const {
- return boost::accumulators::max(accumulator);
- }
- auto get_mean() const {
- return boost::accumulators::mean(accumulator);
- }
- auto get_std() const {
- return std::sqrt(boost::accumulators::variance(accumulator));
- }
- std::string to_string() const;
- private:
- using float_type = double;
- using accumulator_type = boost::accumulators::accumulator_set<
- float_type, boost::accumulators::stats<
- boost::accumulators::tag::min,
- boost::accumulators::tag::max,
- boost::accumulators::tag::variance> >;
- accumulator_type accumulator;
- bool is_running = false;
- timestamp_type last_ts = 0;
- };
- template<>
- void statistic_timer<statistic_timer_single_shot_tag>::start(timestamp_type current_ts) {
- if (last_ts != 0) {
- accumulator(current_ts - last_ts);
- }
- last_ts = current_ts;
- }
- template<>
- void statistic_timer<statistic_timer_start_stop_tag>::start(timestamp_type current_ts) {
- assert(!is_running);
- is_running = true;
- last_ts = current_ts;
- }
- template<>
- std::string statistic_timer<statistic_timer_single_shot_tag>::to_string() const {
- return fmt::format("Cnt: {}, "
- "Min: {:.3f}ms({:.3f}Hz), Max: {:.3f}ms({:.3f}Hz), "
- "Mean: {:.3f}({:.3f})ms({:.3f}Hz)",
- get_count(),
- get_min() / 1e3, 1e6 / get_min(),
- get_max() / 1e3, 1e6 / get_max(),
- get_mean() / 1e3, get_std() / 1e3, 1e6 / get_mean());
- }
- template<>
- std::string statistic_timer<statistic_timer_start_stop_tag>::to_string() const {
- return fmt::format("Cnt: {}, Min: {:.3f}ms, Max: {:.3f}ms, Mean: {:.3f}({:.3f})ms",
- get_count(), get_min() / 1e3, get_max() / 1e3,
- get_mean() / 1e3, get_std() / 1e3);
- }
- }
- #endif //SOPHIAR2_STATISTIC_TIMER_HPP
|