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);
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);
///
/// @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.
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.
/// @brief Observation pointer
typedef boost::shared_ptr<Observation> ObservationPtr;
-};
-};
+}
+}
#endif // OBSERVATION_H
EXPECT_NO_THROW(ctx.clear());
EXPECT_EQ(0, ctx.size());
}
-
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:
EXPECT_EQ("delta", d.getName());
}
-};
+}
}
/// @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);
}
};
// 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
EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
}
-};
+}
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);
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;
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
/// @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
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);
+}