stopwatch.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 #pragma once
30 
31 #include <ros/time.h>
32 #include <ros/console.h>
33 
34 namespace mapviz
35 {
36 /* This class measures the wall time of an interval and keeps track of
37  * the number of intervals, the average duration, and the maximum
38  * duration. This is used to provide some simple measurements to keep
39  * an eye on performance.
40  */
41 class Stopwatch
42 {
43  public:
45  :
46  count_(0)
47  {
48  }
49 
50  /* Start measuring a new time interval. */
51  void start()
52  {
54  }
55 
56  /* End the current time interval and update the measurements.
57  * Behavior is undefined if start() was not called prior to this.
58  */
59  void stop()
60  {
62  count_ += 1;
63  total_time_ += dt;
64  max_time_ = std::max(max_time_, dt);
65  }
66 
67  /* Return the number of intervals measured. */
68  int count() const { return count_; }
69 
70  /* Returns the longest observed duration. */
71  ros::WallDuration maxTime() const { return max_time_; }
72 
73  /* Returns the average duration spent in the interval. */
75  {
76  if (count_)
77  {
78  return total_time_*(1.0/count_);
79  }
80  else
81  {
82  return ros::WallDuration();
83  }
84  }
85 
86  /* Print measurement info to the ROS console. */
87  void printInfo(const std::string &name) const
88  {
89  if (count_)
90  {
91  ROS_INFO("%s -- calls: %d, avg time: %.2fms, max time: %.2fms",
92  name.c_str(),
93  count_,
94  avgTime().toSec()*1000.0,
95  maxTime().toSec()*1000.0);
96  }
97  else
98  {
99  ROS_INFO("%s -- calls: %d, avg time: --ms, max time: --ms",
100  name.c_str(),
101  count_);
102  }
103  }
104 
105  private:
106  int count_;
109 
111 }; // class PluginInstrumentation
112 } // namespace mapviz
int count() const
Definition: stopwatch.h:68
ros::WallDuration avgTime() const
Definition: stopwatch.h:74
ros::WallTime start_
Definition: stopwatch.h:110
ros::WallDuration total_time_
Definition: stopwatch.h:107
void printInfo(const std::string &name) const
Definition: stopwatch.h:87
#define ROS_INFO(...)
ros::WallDuration max_time_
Definition: stopwatch.h:108
static WallTime now()
ros::WallDuration maxTime() const
Definition: stopwatch.h:71


mapviz
Author(s): Marc Alban
autogenerated on Fri Dec 16 2022 03:59:30