scenario_simulator_v2 C++ API
stop_watch.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 TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
16 #define TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
17 
18 #include <chrono>
19 #include <iostream>
20 #include <optional>
21 #include <rclcpp/rclcpp.hpp>
22 #include <string>
23 
24 namespace traffic_simulator
25 {
26 namespace helper
27 {
28 template <typename T>
29 class StopWatch
30 {
31 public:
32  explicit StopWatch(const std::string & name, bool verbose = false, bool autostart = true)
34  {
35  if (autostart) {
36  start();
37  }
38  }
39 
40  void start()
41  {
42  end_time = std::nullopt;
43  start_time = std::chrono::system_clock::now();
44  }
45  void stop() { end_time = std::chrono::system_clock::now(); }
46  void print()
47  {
48  if (verbose) {
49  if (start_time && end_time) {
50  double elapsed =
51  std::chrono::duration_cast<T>(end_time.value() - start_time.value()).count();
52  if (typeid(T) == typeid(std::chrono::microseconds)) {
53  RCLCPP_INFO_STREAM(
54  rclcpp::get_logger(name),
55  "elapsed time in stop watch " << name << " : " << elapsed << " microseconds");
56  }
57  if (typeid(T) == typeid(std::chrono::milliseconds)) {
58  RCLCPP_INFO_STREAM(
59  rclcpp::get_logger(name),
60  "elapsed time in stop watch " << name << " : " << elapsed << " milliseconds");
61  }
62  if (typeid(T) == typeid(std::chrono::seconds)) {
63  RCLCPP_INFO_STREAM(
64  rclcpp::get_logger(name),
65  "elapsed time in stop watch " << name << " : " << elapsed << " seconds");
66  }
67  }
68  } else {
69  if (start_time) {
70  RCLCPP_ERROR_STREAM(
71  rclcpp::get_logger(name), "Stop watch : " << name << " did not started.");
72  }
73  if (end_time) {
74  RCLCPP_ERROR_STREAM(
75  rclcpp::get_logger(name), "Stop watch : " << name << " did not stopped.");
76  }
77  }
78  }
79 
80 public:
82  const bool verbose;
83 
84 private:
85  std::optional<std::chrono::system_clock::time_point> start_time, end_time;
86 };
87 } // namespace helper
88 } // namespace traffic_simulator
89 
90 #endif // TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
Definition: stop_watch.hpp:30
const bool verbose
Definition: stop_watch.hpp:82
void start()
Definition: stop_watch.hpp:40
void stop()
Definition: stop_watch.hpp:45
StopWatch(const std::string &name, bool verbose=false, bool autostart=true)
Definition: stop_watch.hpp:32
void print()
Definition: stop_watch.hpp:46
const std::string name
Definition: stop_watch.hpp:81
Definition: api.hpp:33
std::string string
Definition: junit5.hpp:26