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 <chrono>
19 #include <cmath>
20 #include <functional>
21 #include <unordered_map>
22 
24 {
25 inline namespace utility
26 {
27 template <typename Clock = std::chrono::system_clock>
29 {
30  class Statistics
31  {
32  std::int64_t ns_max = 0;
33 
34  std::int64_t ns_min = std::numeric_limits<std::int64_t>::max();
35 
36  std::int64_t ns_sum = 0;
37 
38  std::int64_t ns_square_sum = 0;
39 
40  int count = 0;
41 
42  public:
43  template <typename Duration>
44  auto add(Duration diff) -> void
45  {
46  std::int64_t diff_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count();
47  count++;
48  ns_max = std::max(ns_max, diff_ns);
49  ns_min = std::min(ns_max, diff_ns);
50  ns_sum += diff_ns;
51  ns_square_sum += std::pow(diff_ns, 2);
52  }
53 
54  template <typename T>
55  auto max() const
56  {
57  return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_max));
58  }
59 
60  template <typename T>
61  auto min() const
62  {
63  return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_min));
64  }
65 
66  template <typename T>
67  auto mean() const
68  {
69  return std::chrono::duration_cast<T>(std::chrono::nanoseconds(ns_sum / count));
70  }
71 
72  template <typename T>
73  auto standardDeviation() const
74  {
75  std::int64_t mean_of_square = ns_square_sum / count;
76  std::int64_t square_of_mean = std::pow(ns_sum / count, 2);
77  std::int64_t var = mean_of_square - square_of_mean;
78  double standard_deviation = std::sqrt(var);
79  return std::chrono::duration_cast<T>(
80  std::chrono::nanoseconds(static_cast<std::int64_t>(standard_deviation)));
81  }
82 
83  friend auto operator<<(std::ostream & os, const Statistics & statistics) -> std::ostream &
84  {
85  using namespace std::chrono;
86 
87  return os << "mean = " << statistics.template mean<milliseconds>().count() << " ms, "
88  << "max = " << statistics.template max<milliseconds>().count() << " ms, "
89  << "standard deviation = "
90  << statistics.template standardDeviation<milliseconds>().count() / 1000.0 << " ms";
91  }
92  };
93 
94  std::unordered_map<std::string, Statistics> statistics_map;
95 
96 public:
97  template <typename Thunk, typename... Ts>
98  auto invoke(const std::string & tag, Thunk && thunk)
99  {
100  const auto begin = Clock::now();
101 
102  thunk();
103 
104  const auto end = Clock::now();
105 
106  statistics_map[tag].add(end - begin);
107 
108  return end - begin;
109  }
110 
111  auto clear() { statistics_map.clear(); }
112 
113  auto getStatistics(const std::string & tag) -> const auto & { return statistics_map[tag]; }
114 
115  auto begin() const { return statistics_map.begin(); }
116 
117  auto end() const { return statistics_map.end(); }
118 };
119 } // namespace utility
120 } // namespace openscenario_interpreter
121 
122 #endif // OPENSCENARIO_INTERPRETER__UTILITY__EXECUTION_TIMER_HPP_
Definition: execution_timer.hpp:29
auto end() const
Definition: execution_timer.hpp:117
auto invoke(const std::string &tag, Thunk &&thunk)
Definition: execution_timer.hpp:98
auto clear()
Definition: execution_timer.hpp:111
auto getStatistics(const std::string &tag) -> const auto &
Definition: execution_timer.hpp:113
auto begin() const
Definition: execution_timer.hpp:115
decltype(auto) operator<<(std::basic_ostream< Ts... > &os, const AttributeHighlighter &highlight)
Definition: highlighter.hpp:39
Definition: escape_sequence.hpp:22
std::string string
Definition: junit5.hpp:26