15 #ifndef OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
16 #define OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
18 #include <boost/accumulators/accumulators.hpp>
19 #include <boost/accumulators/statistics.hpp>
20 #include <boost/filesystem.hpp>
25 #include <nlohmann/json.hpp>
26 #include <unordered_map>
30 inline namespace utility
33 typename Clock = std::chrono::system_clock,
34 typename Accumulators = boost::accumulators::accumulator_set<
36 boost::accumulators::stats<
37 boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::mean,
38 boost::accumulators::tag::variance, boost::accumulators::tag::count>>>
41 static constexpr
double nanoseconds_to_seconds = 1e-9;
44 using std::unordered_map<std::string, Accumulators>::clear;
46 template <
typename Thunk,
typename... Ts>
49 const auto begin = Clock::now();
53 const auto end = Clock::now();
55 (*this)[tag](std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count());
60 auto save(
const boost::filesystem::path & output_file) ->
void
63 nlohmann::json json_data;
64 for (
const auto & [name, statistics] : *
this) {
65 json_data[name +
"/min"] =
66 boost::accumulators::extract::min(statistics) * nanoseconds_to_seconds;
67 json_data[name +
"/max"] =
68 boost::accumulators::extract::max(statistics) * nanoseconds_to_seconds;
69 json_data[name +
"/mean"] =
70 boost::accumulators::extract::mean(statistics) * nanoseconds_to_seconds;
71 json_data[name +
"/stddev"] =
72 std::sqrt(boost::accumulators::extract::variance(statistics)) * nanoseconds_to_seconds;
73 json_data[name +
"/count"] = boost::accumulators::extract::count(statistics);
76 std::ofstream file(output_file);
77 file << json_data.dump(4);
Definition: execution_timer.hpp:40
auto save(const boost::filesystem::path &output_file) -> void
Definition: execution_timer.hpp:60
auto getStatistics(const std::string &tag) -> const auto &
Definition: execution_timer.hpp:80
auto invoke(const std::string &tag, Thunk &&thunk)
Definition: execution_timer.hpp:47
std::string string
Definition: junit5.hpp:26