metrics_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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   // Observe some values that fit in finite buckets.
00059   std::array<double, 3> values = {{0., 2, 2.5}};
00060   for (const auto& value : values) {
00061     histogram.Observe(value);
00062   }
00063   //     1     2     3    inf
00064   //  1  |  0  |  2  |  0  |
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   // Values above the last bucket boundary should go to the "infinite" bucket.
00072   histogram.Observe(3.5);
00073   //     1     2     3    inf
00074   //  1  |  0  |  2  |  1  |
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   // Observe some values that fit in finite buckets.
00084   std::array<double, 3> values = {{256, 512, 666}};
00085   for (const auto& value : values) {
00086     histogram.Observe(value);
00087   }
00088   // ... 256   512   1024  inf
00089   // ...  |  1  |  2  |  0  |
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   // Values above the last bucket boundary should go to the "infinite" bucket.
00097   histogram.Observe(2048);
00098   // ... 256   512   1024  inf
00099   // ...  |  1  |  2  |  1  |
00100   EXPECT_EQ(histogram.CountsByBucket()[kInfiniteBoundary], 1);
00101 }
00102 
00103 }  // namespace metrics
00104 }  // namespace cartographer_ros


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Wed Jul 10 2019 04:10:28