service_server_statistics.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2015, 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 SOUTHWEST RESEARCH INSTITUTE 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 #ifndef SWRI_ROSCPP_SERVICE_SERVER_STATISTICS_H_
00030 #define SWRI_ROSCPP_SERVICE_SERVER_STATISTICS_H_
00031 
00032 #include <ros/time.h>
00033 
00034 namespace swri
00035 {
00036 class ServiceServerImpl;
00037 class ServiceServerStatistics
00038 {
00039   int servings_;
00040   int succeeded_;
00041   int failed_;
00042   bool last_failed_;
00043 
00044   ros::WallDuration total_time_;
00045   ros::WallDuration min_time_;
00046   ros::WallDuration max_time_;
00047 
00048   void merge(bool success, const ros::WallDuration &runtime);
00049   friend class ServiceServerImpl;
00050 
00051  public:
00052   ServiceServerStatistics() { reset(); }
00053   void reset();
00054 
00055   int servings() const { return servings_; }
00056   int succeeded() const { return succeeded_; }
00057   int failed() const { return failed_; }
00058   bool lastFailed() const { return last_failed_; }
00059 
00060   ros::WallDuration meanTime() const;
00061   ros::WallDuration minTime() const { return min_time_; }
00062   ros::WallDuration maxTime() const { return max_time_; }
00063 };  // struct ServiceServerStatistics
00064 
00065 
00066 inline
00067 void ServiceServerStatistics::reset()
00068 {
00069   servings_ = 0;
00070   succeeded_ = 0;
00071   failed_ = 0;
00072   last_failed_ = false;
00073   total_time_ = ros::WallDuration(0);
00074   min_time_ = ros::WallDuration(0);
00075   max_time_ = ros::WallDuration(0);
00076 }
00077 
00078 inline
00079 ros::WallDuration ServiceServerStatistics::meanTime() const
00080 {
00081   if (servings_ == 0) {
00082     return ros::WallDuration(0);
00083   } else {
00084     return ros::WallDuration(total_time_.toSec() / servings_);
00085   }
00086 }
00087 
00088 inline
00089 void ServiceServerStatistics::merge(
00090   bool success, const ros::WallDuration &runtime)
00091 {
00092   servings_++;
00093   if (success) {
00094     succeeded_++;
00095     last_failed_ = false;
00096   } else {
00097     failed_++;
00098     last_failed_ = true;
00099   }
00100 
00101   if (servings_ == 1) {
00102     total_time_ = runtime;
00103     min_time_ = runtime;
00104     max_time_ = runtime;
00105   } else {
00106     total_time_ += runtime;
00107     min_time_ = std::min(min_time_, runtime);
00108     max_time_ = std::max(max_time_, runtime);
00109   }
00110 }
00111 }  // namespace swri
00112 #endif  // SWRI_ROSCPP_SERVICE_SERVER_STATISTICS_H_


swri_roscpp
Author(s):
autogenerated on Tue Oct 3 2017 03:19:27