123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <config.h>
- #include <stats/stats_mgr.h>
- #include <exceptions/exceptions.h>
- #include <cc/data.h>
- #include <config/command_interpreter.h>
- #include <util/boost_time_utils.h>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <boost/shared_ptr.hpp>
- #include <gtest/gtest.h>
- #include <iostream>
- #include <sstream>
- using namespace isc;
- using namespace isc::data;
- using namespace isc::stats;
- using namespace isc::config;
- using namespace boost::posix_time;
- namespace {
- /// @brief Fixture class for StatsMgr testing
- ///
- /// Very simple class that makes sure that StatsMgr is indeed instantiated
- /// before the test and any statistics are wiped out after it.
- class StatsMgrTest : public ::testing::Test {
- public:
- /// @brief Constructor
- /// Makes sure that the Statistics Manager is instantiated.
- StatsMgrTest() {
- StatsMgr::instance();
- StatsMgr::instance().removeAll();
- }
- /// @brief Destructor
- /// Removes all statistics.
- ~StatsMgrTest() {
- StatsMgr::instance().removeAll();
- }
- };
- // Basic test for statistics manager interface.
- TEST_F(StatsMgrTest, basic) {
- // Getting an instance
- EXPECT_NO_THROW(StatsMgr::instance());
- // Check that there are no statistics recorded by default.
- EXPECT_EQ(0, StatsMgr::instance().count());
- }
- // Test checks whether it's possible to record and later report
- // an integer statistic.
- TEST_F(StatsMgrTest, integerStat) {
- EXPECT_NO_THROW(StatsMgr::instance().setValue("alpha",
- static_cast<uint64_t>(1234)));
- ObservationPtr alpha;
- EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
- ASSERT_TRUE(alpha);
- std::string exp = "{ \"alpha\": [ [ 1234, \""
- + isc::util::ptimeToText(alpha->getInteger().second) + "\" ] ] }";
- EXPECT_EQ(exp, StatsMgr::instance().get("alpha")->str());
- }
- // Test checks whether it's possible to record and later report
- // a floating point statistic.
- TEST_F(StatsMgrTest, floatStat) {
- EXPECT_NO_THROW(StatsMgr::instance().setValue("beta", 12.34));
- ObservationPtr beta;
- EXPECT_NO_THROW(beta = StatsMgr::instance().getObservation("beta"));
- ASSERT_TRUE(beta);
- std::string exp = "{ \"beta\": [ [ 12.34, \""
- + isc::util::ptimeToText(beta->getFloat().second) + "\" ] ] }";
- EXPECT_EQ(exp, StatsMgr::instance().get("beta")->str());
- }
- // Test checks whether it's possible to record and later report
- // a duration statistic.
- TEST_F(StatsMgrTest, durationStat) {
- EXPECT_NO_THROW(StatsMgr::instance().setValue("gamma",
- microsec::time_duration(1,2,3,4)));
- ObservationPtr gamma;
- EXPECT_NO_THROW(gamma = StatsMgr::instance().getObservation("gamma"));
- ASSERT_TRUE(gamma);
- std::string exp = "{ \"gamma\": [ [ \"01:02:03.000004\", \""
- + isc::util::ptimeToText(gamma->getDuration().second) + "\" ] ] }";
- EXPECT_EQ(exp, StatsMgr::instance().get("gamma")->str());
- }
- // Test checks whether it's possible to record and later report
- // a string statistic.
- TEST_F(StatsMgrTest, stringStat) {
- EXPECT_NO_THROW(StatsMgr::instance().setValue("delta",
- "Lorem ipsum"));
- ObservationPtr delta;
- EXPECT_NO_THROW(delta = StatsMgr::instance().getObservation("delta"));
- ASSERT_TRUE(delta);
- std::string exp = "{ \"delta\": [ [ \"Lorem ipsum\", \""
- + isc::util::ptimeToText(delta->getString().second) + "\" ] ] }";
- EXPECT_EQ(exp, StatsMgr::instance().get("delta")->str());
- }
- // Setting limits is currently not implemented, so those methods should
- // throw.
- TEST_F(StatsMgrTest, setLimits) {
- EXPECT_THROW(StatsMgr::instance().setMaxSampleAge("foo",
- time_duration(1,0,0,0)),
- NotImplemented);
- EXPECT_THROW(StatsMgr::instance().setMaxSampleCount("foo", 100),
- NotImplemented);
- }
- // This test checks whether a single (get("foo")) and all (getAll())
- // statistics are reported properly.
- TEST_F(StatsMgrTest, getGetAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem");
- // Now add some values to them
- StatsMgr::instance().addValue("alpha", static_cast<uint64_t>(5678));
- StatsMgr::instance().addValue("beta", 56.78);
- StatsMgr::instance().addValue("gamma", time_duration(5,6,7,8));
- StatsMgr::instance().addValue("delta", " ipsum");
- // There should be 4 statistics reported
- EXPECT_EQ(4, StatsMgr::instance().count());
- // Now check whether they can be reported back
- ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
- ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
- ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
- ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
- ASSERT_TRUE(rep_alpha);
- ASSERT_TRUE(rep_beta);
- ASSERT_TRUE(rep_gamma);
- ASSERT_TRUE(rep_delta);
- std::string exp_str_alpha = "[ [ 6912, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
- ->getInteger().second) + "\" ] ]";
- std::string exp_str_beta = "[ [ 69.12, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
- ->getFloat().second) + "\" ] ]";
- std::string exp_str_gamma = "[ [ \"06:08:10.000012\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
- ->getDuration().second) + "\" ] ]";
- std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
- ->getString().second) + "\" ] ]";
- // Check that individual stats are reported properly
- EXPECT_EQ("{ \"alpha\": " + exp_str_alpha + " }", rep_alpha->str());
- EXPECT_EQ("{ \"beta\": " + exp_str_beta + " }", rep_beta->str());
- EXPECT_EQ("{ \"gamma\": " + exp_str_gamma + " }", rep_gamma->str());
- EXPECT_EQ("{ \"delta\": " + exp_str_delta + " }", rep_delta->str());
- // Check that non-existent metric is not reported.
- EXPECT_EQ("{ }", StatsMgr::instance().get("epsilon")->str());
- // Check that all of them can be reported at once
- ConstElementPtr rep_all = StatsMgr::instance().getAll();
- ASSERT_TRUE(rep_all);
- // Verifying this is a bit more involved, as we don't know whether the
- // order would be preserved or not.
- EXPECT_EQ(4, rep_all->size());
- ASSERT_TRUE(rep_all->get("alpha"));
- ASSERT_TRUE(rep_all->get("beta"));
- ASSERT_TRUE(rep_all->get("delta"));
- ASSERT_TRUE(rep_all->get("gamma"));
- EXPECT_FALSE(rep_all->get("epsilon"));
- EXPECT_EQ(exp_str_alpha, rep_all->get("alpha")->str());
- EXPECT_EQ(exp_str_beta, rep_all->get("beta")->str());
- EXPECT_EQ(exp_str_gamma, rep_all->get("gamma")->str());
- EXPECT_EQ(exp_str_delta, rep_all->get("delta")->str());
- }
- // This test checks whether existing statistics can be reset.
- TEST_F(StatsMgrTest, reset) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // This should reset alpha to 0
- EXPECT_NO_THROW(StatsMgr::instance().reset("alpha"));
- EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
- // The other stats should remain untouched
- EXPECT_EQ(12.34,
- StatsMgr::instance().getObservation("beta")->getFloat().first);
- EXPECT_EQ(time_duration(1,2,3,4),
- StatsMgr::instance().getObservation("gamma")->getDuration().first);
- EXPECT_EQ("Lorem ipsum",
- StatsMgr::instance().getObservation("delta")->getString().first);
- // Now let's wipe them, too.
- EXPECT_NO_THROW(StatsMgr::instance().reset("beta"));
- EXPECT_NO_THROW(StatsMgr::instance().reset("gamma"));
- EXPECT_NO_THROW(StatsMgr::instance().reset("delta"));
- EXPECT_EQ(0.0,
- StatsMgr::instance().getObservation("beta")->getFloat().first);
- EXPECT_EQ(time_duration(0,0,0,0),
- StatsMgr::instance().getObservation("gamma")->getDuration().first);
- EXPECT_EQ("",
- StatsMgr::instance().getObservation("delta")->getString().first);
- // Resetting statistics should not remove them
- EXPECT_EQ(4, StatsMgr::instance().count());
- }
- // This test checks whether existing statistics can be reset.
- TEST_F(StatsMgrTest, resetAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // This should reset alpha to 0
- EXPECT_NO_THROW(StatsMgr::instance().resetAll());
- EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
- EXPECT_EQ(0.0,
- StatsMgr::instance().getObservation("beta")->getFloat().first);
- EXPECT_EQ(time_duration(0,0,0,0),
- StatsMgr::instance().getObservation("gamma")->getDuration().first);
- EXPECT_EQ("",
- StatsMgr::instance().getObservation("delta")->getString().first);
- // Resetting all statistics should not remove them
- EXPECT_EQ(4, StatsMgr::instance().count());
- }
- // This test checks whether statistics can be removed.
- TEST_F(StatsMgrTest, removeAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // This should reset alpha to 0
- EXPECT_NO_THROW(StatsMgr::instance().removeAll());
- // Resetting all statistics should not remove them
- EXPECT_EQ(0, StatsMgr::instance().count());
- // There should be no such statistics anymore
- EXPECT_EQ("{ }", StatsMgr::instance().get("alpha")->str());
- EXPECT_EQ("{ }", StatsMgr::instance().get("beta")->str());
- EXPECT_EQ("{ }", StatsMgr::instance().get("gamma")->str());
- EXPECT_EQ("{ }", StatsMgr::instance().get("delta")->str());
- // There should be no such statistics anymore
- EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("beta"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("gamma"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("delta"));
- }
- // This is a performance benchmark that checks how long does it take
- // to increment a single statistic million times.
- //
- // Data points:
- // It took 00:00:00.363709 (363ms) on late 2013 Mac with Mac OS X 10.9.5.
- TEST_F(StatsMgrTest, DISABLED_performanceSingleAdd) {
- StatsMgr::instance().removeAll();
- uint32_t cycles = 1000000;
- ptime before = microsec_clock::local_time();
- for (uint32_t i = 0; i < cycles; ++i) {
- StatsMgr::instance().addValue("metric1", 0.1*i);
- }
- ptime after = microsec_clock::local_time();
- time_duration dur = after - before;
- std::cout << "Incrementing a single statistic " << cycles << " times took: "
- << isc::util::durationToText(dur) << std::endl;
- }
- // This is a performance benchmark that checks how long does it take
- // to set absolute value of a single statistic million times.
- //
- // Data points:
- // It took 00:00:00.361003 (361ms) on late 2013 Mac with Mac OS X 10.9.5.
- TEST_F(StatsMgrTest, DISABLED_performanceSingleSet) {
- StatsMgr::instance().removeAll();
- uint32_t cycles = 1000000;
- ptime before = microsec_clock::local_time();
- for (uint32_t i = 0; i < cycles; ++i) {
- StatsMgr::instance().setValue("metric1", 0.1*i);
- }
- ptime after = microsec_clock::local_time();
- time_duration dur = after - before;
- std::cout << "Setting a single statistic " << cycles << " times took: "
- << isc::util::durationToText(dur) << std::endl;
- }
- // This is a performance benchmark that checks how long does it take to
- // increment one statistic a million times, when there is 1000 other statistics
- // present.
- //
- // Data points:
- // 00:00:00.436943 (436ms) on late 2013 Mac with Mac OS X 10.9.5
- TEST_F(StatsMgrTest, DISABLED_performanceMultipleAdd) {
- StatsMgr::instance().removeAll();
- uint32_t cycles = 1000000;
- uint32_t stats = 1000;
- for (uint32_t i = 0; i < stats; ++i) {
- std::stringstream tmp;
- tmp << "statistic" << i;
- StatsMgr::instance().setValue(tmp.str(), static_cast<uint64_t>(i));
- }
- ptime before = microsec_clock::local_time();
- for (uint32_t i = 0; i < cycles; ++i) {
- StatsMgr::instance().addValue("metric1", static_cast<uint64_t>(i));
- }
- ptime after = microsec_clock::local_time();
- time_duration dur = after - before;
- std::cout << "Incrementing one of " << stats << " statistics " << cycles
- << " times took: " << isc::util::durationToText(dur) << std::endl;
- }
- // This is a performance benchmark that checks how long does it take to
- // set one statistic to a given value a million times, when there is 1000 other
- // statistics present.
- //
- // Data points:
- // 00:00:00.424518 (424ms) on late 2013 Mac with Mac OS X 10.9.5
- TEST_F(StatsMgrTest, DISABLED_performanceMultipleSet) {
- StatsMgr::instance().removeAll();
- uint32_t cycles = 1000000;
- uint32_t stats = 1000;
- for (uint32_t i = 0; i < stats; ++i) {
- std::stringstream tmp;
- tmp << "statistic" << i;
- StatsMgr::instance().setValue(tmp.str(), static_cast<uint64_t>(i));
- }
- ptime before = microsec_clock::local_time();
- for (uint32_t i = 0; i < cycles; ++i) {
- StatsMgr::instance().setValue("metric1", static_cast<uint64_t>(i));
- }
- ptime after = microsec_clock::local_time();
- time_duration dur = after - before;
- std::cout << "Setting one of " << stats << " statistics " << cycles
- << " times took: " << isc::util::durationToText(dur) << std::endl;
- }
- // Test checks if statistic-get handler is able to return specified statistic.
- TEST_F(StatsMgrTest, commandStatisticGet) {
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- ElementPtr params = Element::createMap();
- params->set("name", Element::create("alpha"));
- ConstElementPtr rsp = StatsMgr::instance().statisticGetHandler("statistic-get",
- params);
- ObservationPtr alpha;
- EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
- ASSERT_TRUE(alpha);
- std::string exp = "{ \"alpha\": [ [ 1234, \""
- + isc::util::ptimeToText(alpha->getInteger().second) + "\" ] ] }";
- EXPECT_EQ("{ \"arguments\": " + exp + ", \"result\": 0 }", rsp->str());
- }
- // Test checks if statistic-get is able to handle:
- // - a request without parameters
- // - a request with missing statistic name
- // - a request for non-existing statistic.
- TEST_F(StatsMgrTest, commandStatisticGetNegative) {
- // Case 1: a request without parameters
- ConstElementPtr rsp = StatsMgr::instance().statisticGetHandler("statistic-get",
- ElementPtr());
- int status_code;
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 2: a request with missing statistic name
- ElementPtr params = Element::createMap();
- rsp = StatsMgr::instance().statisticGetHandler("statistic-get", params);
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 3: a request for non-existing statistic
- params->set("name", Element::create("alpha"));
- rsp = StatsMgr::instance().statisticGetHandler("statistic-get", params);
- EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", rsp->str());
- }
- // This test checks whether statistc-get-all command returns all statistics
- // correctly.
- TEST_F(StatsMgrTest, commandGetAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // Now get them. They're used to generate expected output
- ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
- ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
- ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
- ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
- ASSERT_TRUE(rep_alpha);
- ASSERT_TRUE(rep_beta);
- ASSERT_TRUE(rep_gamma);
- ASSERT_TRUE(rep_delta);
- std::string exp_str_alpha = "[ [ 1234, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
- ->getInteger().second) + "\" ] ]";
- std::string exp_str_beta = "[ [ 12.34, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
- ->getFloat().second) + "\" ] ]";
- std::string exp_str_gamma = "[ [ \"01:02:03.000004\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
- ->getDuration().second) + "\" ] ]";
- std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
- ->getString().second) + "\" ] ]";
- // Check that all of them can be reported at once
- ConstElementPtr rsp = StatsMgr::instance().statisticGetAllHandler(
- "statistic-get-all", ElementPtr());
- ASSERT_TRUE(rsp);
- int status_code;
- ConstElementPtr rep_all = parseAnswer(status_code, rsp);
- ASSERT_EQ(0, status_code);
- ASSERT_TRUE(rep_all);
- // Verifying this is a bit more involved, as we don't know whether the
- // order would be preserved or not.
- EXPECT_EQ(4, rep_all->size());
- ASSERT_TRUE(rep_all->get("alpha"));
- ASSERT_TRUE(rep_all->get("beta"));
- ASSERT_TRUE(rep_all->get("delta"));
- ASSERT_TRUE(rep_all->get("gamma"));
- EXPECT_FALSE(rep_all->get("epsilon"));
- EXPECT_EQ(exp_str_alpha, rep_all->get("alpha")->str());
- EXPECT_EQ(exp_str_beta, rep_all->get("beta")->str());
- EXPECT_EQ(exp_str_gamma, rep_all->get("gamma")->str());
- EXPECT_EQ(exp_str_delta, rep_all->get("delta")->str());
- }
- // Test checks if statistic-reset handler is able to reset specified statistic.
- TEST_F(StatsMgrTest, commandStatisticReset) {
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- ElementPtr params = Element::createMap();
- params->set("name", Element::create("alpha"));
- ConstElementPtr rsp =
- StatsMgr::instance().statisticResetHandler("statistic-reset", params);
- int status_code;
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
- ObservationPtr alpha;
- EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
- ASSERT_TRUE(alpha);
- // Check that it was indeed reset
- EXPECT_EQ(0, alpha->getInteger().first);
- }
- // Test checks if statistic-reset is able to handle:
- // - a request without parameters
- // - a request with missing statistic name
- // - a request for non-existing statistic.
- TEST_F(StatsMgrTest, commandStatisticResetNegative) {
- // Case 1: a request without parameters
- ConstElementPtr rsp =
- StatsMgr::instance().statisticResetHandler("statistic-reset", ElementPtr());
- int status_code;
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 2: a request with missing statistic name
- ElementPtr params = Element::createMap();
- rsp = StatsMgr::instance().statisticResetHandler("statistic-reset", params);
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 3: a request for non-existing statistic
- params->set("name", Element::create("alpha"));
- rsp = StatsMgr::instance().statisticResetHandler("statistic-reset", params);
- EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'alpha' statistic found\" }",
- rsp->str());
- }
- // This test checks whether statistic-reset-all command really resets all
- // statistics correctly.
- TEST_F(StatsMgrTest, commandResetAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // Now get them. They're used to generate expected output
- ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
- ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
- ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
- ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
- ASSERT_TRUE(rep_alpha);
- ASSERT_TRUE(rep_beta);
- ASSERT_TRUE(rep_gamma);
- ASSERT_TRUE(rep_delta);
- std::string exp_str_alpha = "[ [ 1234, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
- ->getInteger().second) + "\" ] ]";
- std::string exp_str_beta = "[ [ 12.34, \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
- ->getFloat().second) + "\" ] ]";
- std::string exp_str_gamma = "[ [ \"01:02:03.000004\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
- ->getDuration().second) + "\" ] ]";
- std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
- + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
- ->getString().second) + "\" ] ]";
- // Check that all of them can be reset at once
- ConstElementPtr rsp = StatsMgr::instance().statisticResetAllHandler(
- "statistic-reset-all", ElementPtr());
- ASSERT_TRUE(rsp);
- int status_code;
- ConstElementPtr rep_all = parseAnswer(status_code, rsp);
- ASSERT_EQ(0, status_code);
- ASSERT_TRUE(rep_all);
- // Check that they're indeed reset
- EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
- EXPECT_EQ(0.0f,
- StatsMgr::instance().getObservation("beta")->getFloat().first);
- EXPECT_EQ(time_duration(0,0,0,0),
- StatsMgr::instance().getObservation("gamma")->getDuration().first);
- EXPECT_EQ("",
- StatsMgr::instance().getObservation("delta")->getString().first);
- }
- // Test checks if statistic-remove handler is able to remove a statistic.
- TEST_F(StatsMgrTest, commandStatisticRemove) {
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- ElementPtr params = Element::createMap();
- params->set("name", Element::create("alpha"));
- ConstElementPtr rsp =
- StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
- int status_code;
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
- // It should be gone.
- EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
- }
- // Test checks if statistic-remove is able to handle:
- // - a request without parameters
- // - a request with missing statistic name
- // - a request for non-existing statistic.
- TEST_F(StatsMgrTest, commandStatisticRemoveNegative) {
- // Case 1: a request without parameters
- ConstElementPtr rsp =
- StatsMgr::instance().statisticRemoveHandler("statistic-remove", ElementPtr());
- int status_code;
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 2: a request with missing statistic name
- ElementPtr params = Element::createMap();
- rsp = StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
- ASSERT_NO_THROW(parseAnswer(status_code, rsp));
- EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
- // Case 3: a request for non-existing statistic
- params->set("name", Element::create("alpha"));
- rsp = StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
- EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'alpha' statistic found\" }",
- rsp->str());
- }
- // This test checks whether statistic-remove-all command really resets all
- // statistics correctly.
- TEST_F(StatsMgrTest, commandRemoveAll) {
- // Set a couple of statistics
- StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
- StatsMgr::instance().setValue("beta", 12.34);
- StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
- StatsMgr::instance().setValue("delta", "Lorem ipsum");
- // Check that all of them can be reset at once
- ConstElementPtr rsp = StatsMgr::instance().statisticRemoveAllHandler(
- "statistic-remove-all", ElementPtr());
- ASSERT_TRUE(rsp);
- int status_code;
- ConstElementPtr rep_all = parseAnswer(status_code, rsp);
- ASSERT_EQ(0, status_code);
- EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("beta"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("gamma"));
- EXPECT_FALSE(StatsMgr::instance().getObservation("delta"));
- }
- };
|