Skip to content

Commit 57d327c

Browse files
authored
Added jitter measurement to examples_rclcpp_cbg_executor. (#328)
Signed-off-by: Ralph Lange <[email protected]>
1 parent 200a16c commit 57d327c

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

rclcpp/executors/cbg_executor/src/examples_rclcpp_cbg_executor/ping_node.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <algorithm>
1818
#include <functional>
1919
#include <memory>
20+
#include <vector>
2021

2122
#include "./utilities.hpp"
2223

@@ -65,18 +66,15 @@ void PingNode::print_statistics(std::chrono::seconds experiment_duration) const
6566
{
6667
size_t ping_count = rtt_data_.size();
6768

68-
size_t high_pong_count = 0;
69-
size_t low_pong_count = 0;
70-
rclcpp::Duration high_rtt_sum(0, 0);
71-
rclcpp::Duration low_rtt_sum(0, 0);
69+
std::vector<double> high_rtts;
70+
std::vector<double> low_rtts;
71+
7272
for (const auto & entry : rtt_data_) {
7373
if (entry.high_received_.nanoseconds() >= entry.sent_.nanoseconds()) {
74-
++high_pong_count;
75-
high_rtt_sum = high_rtt_sum + (entry.high_received_ - entry.sent_);
74+
high_rtts.push_back((entry.high_received_ - entry.sent_).seconds());
7675
}
7776
if (entry.low_received_.nanoseconds() >= entry.sent_.nanoseconds()) {
78-
++low_pong_count;
79-
low_rtt_sum = low_rtt_sum + (entry.low_received_ - entry.sent_);
77+
low_rtts.push_back((entry.low_received_ - entry.sent_).seconds());
8078
}
8179
}
8280

@@ -87,18 +85,22 @@ void PingNode::print_statistics(std::chrono::seconds experiment_duration) const
8785
ping_count, ideal_ping_count, 100 * ping_count / ideal_ping_count);
8886
RCLCPP_INFO(
8987
get_logger(), "High prio path: Received %zu pongs, i.e. for %zu%% of the pings.",
90-
high_pong_count, 100 * high_pong_count / ping_count);
91-
if (high_pong_count > 0) {
92-
double high_rtt_avg = (high_rtt_sum.seconds() * 1000.0 / high_pong_count);
88+
high_rtts.size(), 100 * high_rtts.size() / ping_count);
89+
if (!high_rtts.empty()) {
90+
double high_rtt_avg = calc_average(high_rtts) * 1000.0;
9391
RCLCPP_INFO(get_logger(), "High prio path: Average RTT is %3.1fms.", high_rtt_avg);
92+
double high_rtt_jitter = calc_std_deviation(high_rtts) * 1000.0;
93+
RCLCPP_INFO(get_logger(), "High prio path: Jitter of RTT is %5.3fms.", high_rtt_jitter);
9494
}
9595

9696
RCLCPP_INFO(
9797
get_logger(), "Low prio path: Received %zu pongs, i.e. for %zu%% of the pings.",
98-
low_pong_count, 100 * low_pong_count / ping_count);
99-
if (low_pong_count > 0) {
100-
double low_rtt_avg = (low_rtt_sum.seconds() * 1000.0 / low_pong_count);
98+
low_rtts.size(), 100 * low_rtts.size() / ping_count);
99+
if (!low_rtts.empty()) {
100+
double low_rtt_avg = calc_average(low_rtts) * 1000.0;
101101
RCLCPP_INFO(get_logger(), "Low prio path: Average RTT is %3.1fms.", low_rtt_avg);
102+
double low_rtt_jitter = calc_std_deviation(low_rtts) * 1000.0;
103+
RCLCPP_INFO(get_logger(), "Low prio path: Jitter of RTT is %5.3fms.", low_rtt_jitter);
102104
}
103105
}
104106

rclcpp/executors/cbg_executor/src/examples_rclcpp_cbg_executor/utilities.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
#ifndef EXAMPLES_RCLCPP_CBG_EXECUTOR__UTILITIES_HPP_
1616
#define EXAMPLES_RCLCPP_CBG_EXECUTOR__UTILITIES_HPP_
1717

18+
#include <cmath>
19+
1820
#include <chrono>
21+
#include <functional>
1922
#include <string>
2023
#include <thread>
24+
#include <vector>
2125

2226
#ifdef _WIN32 // i.e., Windows platform.
2327
#include <windows.h>
@@ -230,6 +234,24 @@ inline std::chrono::nanoseconds get_current_thread_time()
230234
#endif
231235
}
232236

237+
/// Calculates the average of the given vector of doubles.
238+
inline double calc_average(const std::vector<double> & v)
239+
{
240+
double avg = std::accumulate(v.begin(), v.end(), 0.0, std::plus<double>()) / v.size();
241+
return avg;
242+
}
243+
244+
/// Calculates the standard deviation of the given vector of doubles.
245+
inline double calc_std_deviation(const std::vector<double> & v)
246+
{
247+
double mean = calc_average(v);
248+
double sum_squares = 0.0;
249+
for (const double d : v) {
250+
sum_squares += (d - mean) * (d - mean);
251+
}
252+
return std::sqrt(sum_squares / v.size());
253+
}
254+
233255
} // namespace examples_rclcpp_cbg_executor
234256

235257
#endif // EXAMPLES_RCLCPP_CBG_EXECUTOR__UTILITIES_HPP_

0 commit comments

Comments
 (0)