scenario_simulator_v2 C++ API
execution_timer.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
16 #define OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
17 
18 #include <boost/accumulators/accumulators.hpp>
19 #include <boost/accumulators/statistics.hpp>
20 #include <boost/filesystem.hpp>
21 #include <chrono>
22 #include <cmath>
23 #include <fstream>
24 #include <functional>
25 #include <nlohmann/json.hpp>
26 #include <unordered_map>
27 
29 {
30 inline namespace utility
31 {
32 template <
33  typename Clock = std::chrono::system_clock,
34  typename Accumulators = boost::accumulators::accumulator_set<
35  std::int64_t,
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>>>
39 class ExecutionTimer : private std::unordered_map<std::string, Accumulators>
40 {
41  static constexpr double nanoseconds_to_seconds = 1e-9;
42 
43 public:
44  using std::unordered_map<std::string, Accumulators>::clear;
45 
46  template <typename Thunk, typename... Ts>
47  auto invoke(const std::string & tag, Thunk && thunk)
48  {
49  const auto begin = Clock::now();
50 
51  thunk();
52 
53  const auto end = Clock::now();
54 
55  (*this)[tag](std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count());
56 
57  return end - begin;
58  }
59 
60  auto save(const boost::filesystem::path & output_file) -> void
61  {
62  // the unit of each statistics is seconds
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);
74  }
75 
76  std::ofstream file(output_file);
77  file << json_data.dump(4);
78  }
79 
80  auto getStatistics(const std::string & tag) -> const auto & { return (*this)[tag]; }
81 };
82 } // namespace utility
83 } // namespace openscenario_interpreter
84 
85 #endif // OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
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
Definition: hypot.hpp:22
std::string string
Definition: junit5.hpp:26