code_profiler
StatsPublisher.cpp
Go to the documentation of this file.
2 
3 #include <ros/ros.h>
4 #include <code_profiler/Statistics.h>
5 
6 // ----------------------------------------------------------------------------------------------------
7 
9 {
10 }
11 
12 // ----------------------------------------------------------------------------------------------------
13 
15 {
16 }
17 
18 // ----------------------------------------------------------------------------------------------------
19 
21 {
22  ros::NodeHandle nh("~");
23  pub_stats_ = nh.advertise<code_profiler::Statistics>("profiler_stats", 1);
24 }
25 
26 // ----------------------------------------------------------------------------------------------------
27 
29 {
30  timers_[label].start();
31  stack_.push(ScopeStat(label));
32 }
33 
34 // ----------------------------------------------------------------------------------------------------
35 
37 {
39  if (it == timers_.end())
40  {
41  std::cout << "code_profiler: no timer found for label '" << label << "'." << std::endl;
42  }
43  else
44  {
45  it->second.stop();
46  }
47 
48  if (!stack_.empty())
49  {
50  stack_.top().timer.stop();
51  stack_.pop();
52  }
53  else
54  {
55  std::cout << "code_profiler: stopTimer() called, but no timer is active." << std::endl;
56  }
57 }
58 
59 // ----------------------------------------------------------------------------------------------------
60 
62 {
63  if (pub_stats_.getTopic() == "")
64  {
65  std::cout << "code_profiler: StatsPublisher not initialzed." << std::endl;
66  return;
67  }
68 
69  code_profiler::Statistics msg;
71  {
72  msg.labels.push_back(it->first);
73  msg.time_secs.push_back(it->second.getElapsedTimeInSec());
74  }
75 
76  pub_stats_.publish(msg);
77 }
StatsPublisher::startTimer
void startTimer(const std::string &label)
Definition: StatsPublisher.cpp:28
std::string
StatsPublisher::stopTimer
void stopTimer(const std::string &label)
Definition: StatsPublisher.cpp:36
StatsPublisher.h
std::map::find
T find(T... args)
StatsPublisher::initialize
void initialize()
Definition: StatsPublisher.cpp:20
StatsPublisher::stack_
std::stack< ScopeStat > stack_
Definition: StatsPublisher.h:45
StatsPublisher::pub_stats_
ros::Publisher pub_stats_
Definition: StatsPublisher.h:41
StatsPublisher::timers_
std::map< std::string, Timer > timers_
Definition: StatsPublisher.h:43
std::cout
StatsPublisher::~StatsPublisher
virtual ~StatsPublisher()
Definition: StatsPublisher.cpp:14
std::stack::pop
T pop(T... args)
std::stack::top
T top(T... args)
StatsPublisher::publish
void publish() const
Definition: StatsPublisher.cpp:61
std::map
ScopeStat
Definition: StatsPublisher.h:11
std::endl
T endl(T... args)
std::map::begin
T begin(T... args)
std::stack::empty
T empty(T... args)
std::stack::push
T push(T... args)
std::map::end
T end(T... args)
StatsPublisher::StatsPublisher
StatsPublisher()
Definition: StatsPublisher.cpp:8