stopwatch.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 #pragma once
00030 
00031 #include <ros/time.h>
00032 #include <ros/console.h>
00033 
00034 namespace mapviz
00035 {
00036 /* This class measures the wall time of an interval and keeps track of
00037  * the number of intervals, the average duration, and the maximum
00038  * duration.  This is used to provide some simple measurements to keep
00039  * an eye on performance.
00040  */
00041 class Stopwatch
00042 {
00043  public:
00044   Stopwatch()
00045     :
00046     count_(0)
00047   {
00048   }
00049 
00050   /* Start measuring a new time interval. */
00051   void start()
00052   {
00053     start_ = ros::WallTime::now();
00054   }
00055 
00056   /* End the current time interval and update the measurements.
00057    * Behavior is undefined if start() was not called prior to this.
00058    */
00059   void stop()
00060   {
00061     ros::WallDuration dt = ros::WallTime::now() - start_;
00062     count_ += 1;
00063     total_time_ += dt;
00064     max_time_ = std::max(max_time_, dt);
00065   }
00066 
00067   /* Return the number of intervals measured. */
00068   int count() const { return count_; }
00069 
00070   /* Returns the longest observed duration. */
00071   ros::WallDuration maxTime() const { return max_time_; }
00072 
00073   /* Returns the average duration spent in the interval. */
00074   ros::WallDuration avgTime() const
00075   {
00076     if (count_)
00077     {
00078       return total_time_*(1.0/count_);
00079     }
00080     else
00081     {
00082       return ros::WallDuration();
00083     }
00084   }
00085 
00086   /* Print measurement info to the ROS console. */
00087   void printInfo(const std::string &name) const
00088   {
00089     if (count_)
00090     {
00091       ROS_INFO("%s -- calls: %d, avg time: %.2fms, max time: %.2fms",
00092                name.c_str(),
00093                count_,
00094                avgTime().toSec()*1000.0,
00095                maxTime().toSec()*1000.0);
00096     }
00097     else
00098     {
00099       ROS_INFO("%s -- calls: %d, avg time: --ms, max time: --ms",
00100                name.c_str(),
00101                count_);
00102     }
00103   }
00104 
00105  private:
00106   int count_;
00107   ros::WallDuration total_time_;
00108   ros::WallDuration max_time_;
00109 
00110   ros::WallTime start_;
00111 };  // class PluginInstrumentation
00112 }  // namespace mapviz


mapviz
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 18:50:58