Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <algorithm>
00018 #include <array>
00019 #include <numeric>
00020
00021 #include "cartographer/metrics/histogram.h"
00022 #include "cartographer_ros/metrics/internal/counter.h"
00023 #include "cartographer_ros/metrics/internal/gauge.h"
00024 #include "cartographer_ros/metrics/internal/histogram.h"
00025 #include "gtest/gtest.h"
00026
00027 namespace cartographer_ros {
00028 namespace metrics {
00029
00030 TEST(Metrics, GaugeTest) {
00031 Gauge gauge({});
00032 EXPECT_EQ(gauge.Value(), 0.);
00033 gauge.Increment(1.2);
00034 EXPECT_EQ(gauge.Value(), 1.2);
00035 gauge.Increment();
00036 EXPECT_EQ(gauge.Value(), 2.2);
00037 gauge.Decrement(2.2);
00038 EXPECT_EQ(gauge.Value(), 0.);
00039 gauge.Decrement();
00040 EXPECT_EQ(gauge.Value(), -1.);
00041 }
00042
00043 TEST(Metrics, CounterTest) {
00044 Counter counter({});
00045 EXPECT_EQ(counter.Value(), 0.);
00046 counter.Increment(1.2);
00047 EXPECT_EQ(counter.Value(), 1.2);
00048 counter.Increment(0.8);
00049 EXPECT_EQ(counter.Value(), 2.);
00050 counter.Increment();
00051 EXPECT_EQ(counter.Value(), 3.);
00052 }
00053
00054 TEST(Metrics, HistogramFixedWidthTest) {
00055 auto boundaries = ::cartographer::metrics::Histogram::FixedWidth(1, 3);
00056 Histogram histogram({}, boundaries);
00057
00058
00059 std::array<double, 3> values = {{0., 2, 2.5}};
00060 for (const auto& value : values) {
00061 histogram.Observe(value);
00062 }
00063
00064
00065 EXPECT_EQ(histogram.CountsByBucket()[1], 1);
00066 EXPECT_EQ(histogram.CountsByBucket()[2], 0);
00067 EXPECT_EQ(histogram.CountsByBucket()[3], 2);
00068 EXPECT_EQ(histogram.CumulativeCount(), values.size());
00069 EXPECT_EQ(histogram.Sum(), std::accumulate(values.begin(), values.end(), 0.));
00070
00071
00072 histogram.Observe(3.5);
00073
00074
00075 EXPECT_EQ(histogram.CountsByBucket()[kInfiniteBoundary], 1);
00076 }
00077
00078 TEST(Metrics, HistogramScaledPowersOfTest) {
00079 auto boundaries =
00080 ::cartographer::metrics::Histogram::ScaledPowersOf(2, 1, 2048);
00081 Histogram histogram({}, boundaries);
00082
00083
00084 std::array<double, 3> values = {{256, 512, 666}};
00085 for (const auto& value : values) {
00086 histogram.Observe(value);
00087 }
00088
00089
00090 EXPECT_EQ(histogram.CountsByBucket()[256], 0);
00091 EXPECT_EQ(histogram.CountsByBucket()[512], 1);
00092 EXPECT_EQ(histogram.CountsByBucket()[1024], 2);
00093 EXPECT_EQ(histogram.CumulativeCount(), values.size());
00094 EXPECT_EQ(histogram.Sum(), std::accumulate(values.begin(), values.end(), 0.));
00095
00096
00097 histogram.Observe(2048);
00098
00099
00100 EXPECT_EQ(histogram.CountsByBucket()[kInfiniteBoundary], 1);
00101 }
00102
00103 }
00104 }