]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1174] Updated util and stat code
authorFrancis Dupont <fdupont@isc.org>
Wed, 15 Jul 2020 12:40:07 +0000 (14:40 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 15 Jul 2020 12:40:07 +0000 (14:40 +0200)
src/bin/dhcp4/ctrl_dhcp4_srv.cc
src/bin/dhcp6/ctrl_dhcp6_srv.cc
src/lib/dhcp/iface_mgr.h
src/lib/stats/observation.h
src/lib/stats/tests/context_unittest.cc
src/lib/stats/tests/observation_unittest.cc
src/lib/stats/tests/stats_mgr_unittest.cc
src/lib/util/chrono_time_utils.cc
src/lib/util/chrono_time_utils.h
src/lib/util/tests/chrono_time_utils_unittest.cc

index 79d2a03ea4ad14a83eba884734cb7cab26fe5885..e18bdb90f5f68eb3f7423683dfe0b22d8cbc4152 100644 (file)
@@ -676,7 +676,7 @@ ControlledDhcpv4Srv::commandStatisticSetMaxSampleAgeAllHandler(const string&,
     ConstElementPtr answer = stats_mgr.statisticSetMaxSampleAgeAllHandler(args);
     // Update the default parameter.
     auto duration = stats_mgr.getMaxSampleAgeDefault();
-    long max_age = duration.count(); // do we need to check if this is in seconds?
+    long max_age = toSeconds(duration);
     CfgMgr::instance().getCurrentCfg()->addConfiguredGlobal(
         "statistic-default-sample-age", Element::create(max_age));
     return (answer);
index 64b0b81abb95f1a3df2075183b76e68137c9d7b6..20ea6cfe6bb28fa943bd4c19c2630ea88557bbb6 100644 (file)
@@ -679,7 +679,7 @@ ControlledDhcpv6Srv::commandStatisticSetMaxSampleAgeAllHandler(const string&,
     ConstElementPtr answer = stats_mgr.statisticSetMaxSampleAgeAllHandler(args);
     // Update the default parameter.
     auto duration = stats_mgr.getMaxSampleAgeDefault();
-    long max_age = duration.count(); /// @todo: do we need to check if this is in seconds?
+    long max_age = toSeconds(duration);
     CfgMgr::instance().getCurrentCfg()->addConfiguredGlobal(
         "statistic-default-sample-age", Element::create(max_age));
     return (answer);
index 2a75846d5991723340585c16feee38eeb9ee207a..5c6a47df947eb33b3d5b9cceb1d3b5d895013070 100644 (file)
@@ -144,7 +144,7 @@ public:
     ///
     /// @param name name of the interface
     /// @param ifindex interface index (unique integer identifier)
-    /// @param BadValue when name is empty.
+    /// @throw BadValue when name is empty.
     Iface(const std::string& name, unsigned int ifindex);
 
     /// @brief Destructor.
index 068694f0006ce0f26c44180d9e5f2817937ad48b..f1e0117ef424d40fb7df59c30d365649f26bd54d 100644 (file)
@@ -27,14 +27,25 @@ public:
         isc::Exception(file, line, what) {}
 };
 
-/// @brief Define clock
+/// @brief Define clock type.
 ///
+/// @note: we use the system clock i.e. the wall clock because this
+/// clock can be converted from and to standard Unix time (time_t).
 typedef std::chrono::system_clock SampleClock;
 
-/// @brief Defines duration resolution
+/// @brief Defines duration type.
 ///
+/// @note: the precision depends on the system,
 typedef std::chrono::system_clock::duration StatsDuration;
 
+/// @brief Returns the number of seconds in a duration.
+///
+/// @param dur The duration.
+/// @return The number of seconds in the given duration.
+inline long toSeconds(const StatsDuration& dur) {
+    return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
+}
+
 /// @defgroup stat_samples Specifies supported observation types.
 ///
 /// @brief The list covers all supported types of observations.
@@ -428,7 +439,7 @@ private:
 /// @brief Observation pointer
 typedef boost::shared_ptr<Observation> ObservationPtr;
 
-};
-};
+}
+}
 
 #endif // OBSERVATION_H
index 777db8bd8d80a9b5d9ade75cf535c74b97806c76..92c754ccde5211df767ea940485e6dbe6c5fbaaa 100644 (file)
@@ -124,4 +124,3 @@ TEST(ContextTest, basic) {
     EXPECT_NO_THROW(ctx.clear());
     EXPECT_EQ(0, ctx.size());
 }
-
index c38861112e3de634b2b058f1047c92670f1db0dc..b360cfa0eb637ba04e08cb6fb5eadaaeb1a3439b 100644 (file)
@@ -31,6 +31,13 @@ static const StatsDuration& dur681012(hours(6) + minutes(8) + seconds(10) +
                                       milliseconds(12));
 static const StatsDuration& dur453(minutes(4) + seconds(5) + milliseconds(3));
 
+// This test verifies that the number of seconds can be retrieved.
+TEST(StatsDurationTest, toSeconds) {
+    StatsDuration dur = StatsDuration::zero();
+    dur += hours(1) + minutes(1) + seconds(1) + milliseconds(1);
+    EXPECT_EQ(3661, toSeconds(dur));
+}
+
 /// @brief Test class for Observation
 ///
 /// This simple fixture class initializes four observations:
@@ -609,4 +616,4 @@ TEST_F(ObservationTest, names) {
     EXPECT_EQ("delta", d.getName());
 }
 
-};
+}
index a1482bf8fec75fcfa434f2d15a4f753d7697e67c..d018b05dee7b3a5fca13ca1eb3249970464c49c3 100644 (file)
@@ -45,9 +45,11 @@ public:
     }
 
     /// @brief Destructor
-    /// Removes all statistics.
+    /// Removes all statistics and restores class defaults.
     ~StatsMgrTest() {
         StatsMgr::instance().removeAll();
+        StatsMgr::instance().setMaxSampleAgeDefault(StatsDuration::zero());
+        StatsMgr::instance().setMaxSampleCountDefault(20);
     }
 };
 
@@ -228,7 +230,7 @@ TEST_F(StatsMgrTest, setLimitsDefault) {
     // Set a couple of statistics
     StatsMgr::instance().setValue("alpha", static_cast<int64_t>(1234));
     StatsMgr::instance().setValue("beta", 12.34);
-    StatsMgr::instance().setValue("gamma", std::chrono::seconds(1234));
+    StatsMgr::instance().setValue("gamma", seconds(1234));
     StatsMgr::instance().setValue("delta", "Lorem ipsum");
 
     // check what default applied
@@ -1108,4 +1110,4 @@ TEST_F(StatsMgrTest, commandSetMaxSampleCountAllZero) {
     EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
 }
 
-};
+}
index 4905c8b2e80abab23e689d2f4a51348a39468c52..1bc3a13a7c449cd48fd84b990afe4905469ea88d 100644 (file)
 
 using namespace std::chrono;
 
+namespace isc {
+namespace util {
+
 std::string
-isc::util::clockToText(system_clock::time_point t, size_t fsecs_precision) {
+clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision) {
     time_t tt = system_clock::to_time_t(t);
     struct tm tm;
     localtime_r(&tt, &tm);
@@ -48,8 +51,8 @@ isc::util::clockToText(system_clock::time_point t, size_t fsecs_precision) {
     return (s.str());
 }
 
-std::string
-isc::util::durationToText(system_clock::duration dur, size_t fsecs_precision) {
+template<typename Duration> std::string
+durationToText(Duration dur, size_t fsecs_precision) {
     seconds unfrac = duration_cast<seconds>(dur);
     auto secs = unfrac.count();
     std::stringstream s;
@@ -83,3 +86,15 @@ isc::util::durationToText(system_clock::duration dur, size_t fsecs_precision) {
 
     return (s.str());
 }
+
+// Instantiate for standard clocks.
+template std::string
+durationToText<system_clock::duration>(system_clock::duration dur,
+                                       size_t fsecs_precision);
+
+template std::string
+durationToText<steady_clock::duration>(steady_clock::duration dur,
+                                       size_t fsecs_precision);
+
+} // end of isc::util namespace
+} // end of isc namespace
index cfcecccb8cf3d68d689c0287864fba9bba36f838..3ef08481a9d005a8432ccec04f9a2e30c6eaa38c 100644 (file)
@@ -31,15 +31,18 @@ std::string clockToText(std::chrono::system_clock::time_point t,
 /// @brief Converts StatsDuration to text
 ///
 /// See @ref clockToText for explanation why we chose our own implementation.
+/// @tparam Duration duration type instance for instance
+/// @c std::chrono::system_clock::duration.
 /// @param dur duration value to convert to text
 /// @param fsecs_precision number of digits of precision for fractional seconds.
 /// Zero omits the value.
 ///
 /// @return a string representing time
-std::string durationToText(std::chrono::system_clock::duration,
+template<typename Duration>
+std::string durationToText(Duration dur,
                            size_t fsecs_precision = MAX_FSECS_PRECISION);
 
-}; // end of isc::util namespace
-}; // end of isc namespace
+} // end of isc::util namespace
+} // end of isc namespace
 
 #endif
index 829d4d7e39fde871a423dedd147b7a844bbe00b5..e792a179d2164f477c0bf3c0bc543c654fe9004d 100644 (file)
@@ -146,3 +146,12 @@ TEST(ChronoTimeUtilsTest, bastilleDay) {
     sbast = clockToText(tpbast, MAX_FSECS_PRECISION + 1);
     EXPECT_EQ(expected, sbast);
 }
+
+// Try steady clock duration.
+TEST(ChronoTimeUtilsTest, steadyClock) {
+    steady_clock::duration p12345 = hours(1) + minutes(2) + seconds(3) +
+        milliseconds(4) + microseconds(5);
+    std::string expected("01:02:03.004005");
+    std::string s12345 = durationToText(p12345, 6);
+    EXPECT_EQ(expected, s12345);
+}