rate_timer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_COMMON_RATE_TIMER_H_
18 #define CARTOGRAPHER_COMMON_RATE_TIMER_H_
19 
20 #include <chrono>
21 #include <deque>
22 #include <iomanip>
23 #include <numeric>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
31 
32 namespace cartographer {
33 namespace common {
34 
35 // Computes the rate at which pulses come in.
36 template <typename ClockType = std::chrono::steady_clock>
37 class RateTimer {
38  public:
39  // Computes the rate at which pulses come in over 'window_duration' in wall
40  // time.
41  explicit RateTimer(const common::Duration window_duration)
42  : window_duration_(window_duration) {}
44 
45  RateTimer(const RateTimer&) = delete;
46  RateTimer& operator=(const RateTimer&) = delete;
47 
48  // Returns the pulse rate in Hz.
49  double ComputeRate() const {
50  if (events_.empty()) {
51  return 0.;
52  }
53  return static_cast<double>(events_.size() - 1) /
54  common::ToSeconds((events_.back().time - events_.front().time));
55  }
56 
57  // Returns the ratio of the pulse rate (with supplied times) to the wall time
58  // rate. For example, if a sensor produces pulses at 10 Hz, but we call Pulse
59  // at 20 Hz wall time, this will return 2.
60  double ComputeWallTimeRateRatio() const {
61  if (events_.empty()) {
62  return 0.;
63  }
64  return common::ToSeconds((events_.back().time - events_.front().time)) /
65  std::chrono::duration_cast<std::chrono::duration<double>>(
66  events_.back().wall_time - events_.front().wall_time)
67  .count();
68  }
69 
70  // Records an event that will contribute to the computed rate.
72  events_.push_back(Event{time, ClockType::now()});
73  while (events_.size() > 2 &&
74  (events_.back().wall_time - events_.front().wall_time) >
76  events_.pop_front();
77  }
78  }
79 
80  // Returns a debug string representation.
81  std::string DebugString() const {
82  if (events_.size() < 2) {
83  return "unknown";
84  }
85  std::ostringstream out;
86  out << std::fixed << std::setprecision(2) << ComputeRate() << " Hz "
87  << DeltasDebugString() << " (pulsed at "
88  << ComputeWallTimeRateRatio() * 100. << "% real time)";
89  return out.str();
90  }
91 
92  private:
93  struct Event {
95  typename ClockType::time_point wall_time;
96  };
97 
98  // Computes all differences in seconds between consecutive pulses.
99  std::vector<double> ComputeDeltasInSeconds() const {
100  CHECK_GT(events_.size(), 1);
101  const size_t count = events_.size() - 1;
102  std::vector<double> result;
103  result.reserve(count);
104  for (size_t i = 0; i != count; ++i) {
105  result.push_back(
106  common::ToSeconds(events_[i + 1].time - events_[i].time));
107  }
108  return result;
109  }
110 
111  // Returns the average and standard deviation of the deltas.
112  std::string DeltasDebugString() const {
113  const auto deltas = ComputeDeltasInSeconds();
114  const double sum = std::accumulate(deltas.begin(), deltas.end(), 0.);
115  const double mean = sum / deltas.size();
116 
117  double squared_sum = 0.;
118  for (const double x : deltas) {
119  squared_sum += common::Pow2(x - mean);
120  }
121  const double sigma = std::sqrt(squared_sum / (deltas.size() - 1));
122 
123  std::ostringstream out;
124  out << std::scientific << std::setprecision(2) << mean << " s +/- " << sigma
125  << " s";
126  return out.str();
127  }
128 
129  std::deque<Event> events_;
131 };
132 
133 } // namespace common
134 } // namespace cartographer
135 
136 #endif // CARTOGRAPHER_COMMON_RATE_TIMER_H_
double ComputeWallTimeRateRatio() const
Definition: rate_timer.h:60
const common::Duration window_duration_
Definition: rate_timer.h:130
constexpr T Pow2(T a)
Definition: math.h:50
std::deque< Event > events_
Definition: rate_timer.h:129
UniversalTimeScaleClock::time_point Time
Definition: time.h:44
static time_point time
RateTimer & operator=(const RateTimer &)=delete
std::vector< double > ComputeDeltasInSeconds() const
Definition: rate_timer.h:99
UniversalTimeScaleClock::duration Duration
Definition: time.h:43
int count
Definition: submap_3d.cc:33
RateTimer(const common::Duration window_duration)
Definition: rate_timer.h:41
std::string DebugString() const
Definition: rate_timer.h:81
double ToSeconds(const Duration duration)
Definition: time.cc:29
std::string DeltasDebugString() const
Definition: rate_timer.h:112
ClockType::time_point wall_time
Definition: rate_timer.h:95
void Pulse(common::Time time)
Definition: rate_timer.h:71


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58