stats_mgr_unittest.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <stats/stats_mgr.h>
  16. #include <exceptions/exceptions.h>
  17. #include <cc/data.h>
  18. #include <config/command_interpreter.h>
  19. #include <util/boost_time_utils.h>
  20. #include <boost/date_time/posix_time/posix_time_types.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <gtest/gtest.h>
  23. #include <iostream>
  24. #include <sstream>
  25. using namespace isc;
  26. using namespace isc::data;
  27. using namespace isc::stats;
  28. using namespace isc::config;
  29. using namespace boost::posix_time;
  30. namespace {
  31. /// @brief Fixture class for StatsMgr testing
  32. ///
  33. /// Very simple class that makes sure that StatsMgr is indeed instantiated
  34. /// before the test and any statistics are wiped out after it.
  35. class StatsMgrTest : public ::testing::Test {
  36. public:
  37. /// @brief Constructor
  38. /// Makes sure that the Statistics Manager is instantiated.
  39. StatsMgrTest() {
  40. StatsMgr::instance();
  41. StatsMgr::instance().removeAll();
  42. }
  43. /// @brief Destructor
  44. /// Removes all statistics.
  45. ~StatsMgrTest() {
  46. StatsMgr::instance().removeAll();
  47. }
  48. };
  49. // Basic test for statistics manager interface.
  50. TEST_F(StatsMgrTest, basic) {
  51. // Getting an instance
  52. EXPECT_NO_THROW(StatsMgr::instance());
  53. // Check that there are no statistics recorded by default.
  54. EXPECT_EQ(0, StatsMgr::instance().count());
  55. }
  56. // Test checks whether it's possible to record and later report
  57. // an integer statistic.
  58. TEST_F(StatsMgrTest, integerStat) {
  59. EXPECT_NO_THROW(StatsMgr::instance().setValue("alpha",
  60. static_cast<uint64_t>(1234)));
  61. ObservationPtr alpha;
  62. EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
  63. ASSERT_TRUE(alpha);
  64. std::string exp = "{ \"alpha\": [ [ 1234, \""
  65. + isc::util::ptimeToText(alpha->getInteger().second) + "\" ] ] }";
  66. EXPECT_EQ(exp, StatsMgr::instance().get("alpha")->str());
  67. }
  68. // Test checks whether it's possible to record and later report
  69. // a floating point statistic.
  70. TEST_F(StatsMgrTest, floatStat) {
  71. EXPECT_NO_THROW(StatsMgr::instance().setValue("beta", 12.34));
  72. ObservationPtr beta;
  73. EXPECT_NO_THROW(beta = StatsMgr::instance().getObservation("beta"));
  74. ASSERT_TRUE(beta);
  75. std::string exp = "{ \"beta\": [ [ 12.34, \""
  76. + isc::util::ptimeToText(beta->getFloat().second) + "\" ] ] }";
  77. EXPECT_EQ(exp, StatsMgr::instance().get("beta")->str());
  78. }
  79. // Test checks whether it's possible to record and later report
  80. // a duration statistic.
  81. TEST_F(StatsMgrTest, durationStat) {
  82. EXPECT_NO_THROW(StatsMgr::instance().setValue("gamma",
  83. microsec::time_duration(1,2,3,4)));
  84. ObservationPtr gamma;
  85. EXPECT_NO_THROW(gamma = StatsMgr::instance().getObservation("gamma"));
  86. ASSERT_TRUE(gamma);
  87. std::string exp = "{ \"gamma\": [ [ \"01:02:03.000004\", \""
  88. + isc::util::ptimeToText(gamma->getDuration().second) + "\" ] ] }";
  89. EXPECT_EQ(exp, StatsMgr::instance().get("gamma")->str());
  90. }
  91. // Test checks whether it's possible to record and later report
  92. // a string statistic.
  93. TEST_F(StatsMgrTest, stringStat) {
  94. EXPECT_NO_THROW(StatsMgr::instance().setValue("delta",
  95. "Lorem ipsum"));
  96. ObservationPtr delta;
  97. EXPECT_NO_THROW(delta = StatsMgr::instance().getObservation("delta"));
  98. ASSERT_TRUE(delta);
  99. std::string exp = "{ \"delta\": [ [ \"Lorem ipsum\", \""
  100. + isc::util::ptimeToText(delta->getString().second) + "\" ] ] }";
  101. EXPECT_EQ(exp, StatsMgr::instance().get("delta")->str());
  102. }
  103. // Setting limits is currently not implemented, so those methods should
  104. // throw.
  105. TEST_F(StatsMgrTest, setLimits) {
  106. EXPECT_THROW(StatsMgr::instance().setMaxSampleAge("foo",
  107. time_duration(1,0,0,0)),
  108. NotImplemented);
  109. EXPECT_THROW(StatsMgr::instance().setMaxSampleCount("foo", 100),
  110. NotImplemented);
  111. }
  112. // This test checks whether a single (get("foo")) and all (getAll())
  113. // statistics are reported properly.
  114. TEST_F(StatsMgrTest, getGetAll) {
  115. // Set a couple of statistics
  116. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  117. StatsMgr::instance().setValue("beta", 12.34);
  118. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  119. StatsMgr::instance().setValue("delta", "Lorem");
  120. // Now add some values to them
  121. StatsMgr::instance().addValue("alpha", static_cast<uint64_t>(5678));
  122. StatsMgr::instance().addValue("beta", 56.78);
  123. StatsMgr::instance().addValue("gamma", time_duration(5,6,7,8));
  124. StatsMgr::instance().addValue("delta", " ipsum");
  125. // There should be 4 statistics reported
  126. EXPECT_EQ(4, StatsMgr::instance().count());
  127. // Now check whether they can be reported back
  128. ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
  129. ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
  130. ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
  131. ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
  132. ASSERT_TRUE(rep_alpha);
  133. ASSERT_TRUE(rep_beta);
  134. ASSERT_TRUE(rep_gamma);
  135. ASSERT_TRUE(rep_delta);
  136. std::string exp_str_alpha = "[ [ 6912, \""
  137. + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
  138. ->getInteger().second) + "\" ] ]";
  139. std::string exp_str_beta = "[ [ 69.12, \""
  140. + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
  141. ->getFloat().second) + "\" ] ]";
  142. std::string exp_str_gamma = "[ [ \"06:08:10.000012\", \""
  143. + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
  144. ->getDuration().second) + "\" ] ]";
  145. std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
  146. + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
  147. ->getString().second) + "\" ] ]";
  148. // Check that individual stats are reported properly
  149. EXPECT_EQ("{ \"alpha\": " + exp_str_alpha + " }", rep_alpha->str());
  150. EXPECT_EQ("{ \"beta\": " + exp_str_beta + " }", rep_beta->str());
  151. EXPECT_EQ("{ \"gamma\": " + exp_str_gamma + " }", rep_gamma->str());
  152. EXPECT_EQ("{ \"delta\": " + exp_str_delta + " }", rep_delta->str());
  153. // Check that non-existent metric is not reported.
  154. EXPECT_EQ("{ }", StatsMgr::instance().get("epsilon")->str());
  155. // Check that all of them can be reported at once
  156. ConstElementPtr rep_all = StatsMgr::instance().getAll();
  157. ASSERT_TRUE(rep_all);
  158. // Verifying this is a bit more involved, as we don't know whether the
  159. // order would be preserved or not.
  160. EXPECT_EQ(4, rep_all->size());
  161. ASSERT_TRUE(rep_all->get("alpha"));
  162. ASSERT_TRUE(rep_all->get("beta"));
  163. ASSERT_TRUE(rep_all->get("delta"));
  164. ASSERT_TRUE(rep_all->get("gamma"));
  165. EXPECT_FALSE(rep_all->get("epsilon"));
  166. EXPECT_EQ(exp_str_alpha, rep_all->get("alpha")->str());
  167. EXPECT_EQ(exp_str_beta, rep_all->get("beta")->str());
  168. EXPECT_EQ(exp_str_gamma, rep_all->get("gamma")->str());
  169. EXPECT_EQ(exp_str_delta, rep_all->get("delta")->str());
  170. }
  171. // This test checks whether existing statistics can be reset.
  172. TEST_F(StatsMgrTest, reset) {
  173. // Set a couple of statistics
  174. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  175. StatsMgr::instance().setValue("beta", 12.34);
  176. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  177. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  178. // This should reset alpha to 0
  179. EXPECT_NO_THROW(StatsMgr::instance().reset("alpha"));
  180. EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
  181. // The other stats should remain untouched
  182. EXPECT_EQ(12.34,
  183. StatsMgr::instance().getObservation("beta")->getFloat().first);
  184. EXPECT_EQ(time_duration(1,2,3,4),
  185. StatsMgr::instance().getObservation("gamma")->getDuration().first);
  186. EXPECT_EQ("Lorem ipsum",
  187. StatsMgr::instance().getObservation("delta")->getString().first);
  188. // Now let's wipe them, too.
  189. EXPECT_NO_THROW(StatsMgr::instance().reset("beta"));
  190. EXPECT_NO_THROW(StatsMgr::instance().reset("gamma"));
  191. EXPECT_NO_THROW(StatsMgr::instance().reset("delta"));
  192. EXPECT_EQ(0.0,
  193. StatsMgr::instance().getObservation("beta")->getFloat().first);
  194. EXPECT_EQ(time_duration(0,0,0,0),
  195. StatsMgr::instance().getObservation("gamma")->getDuration().first);
  196. EXPECT_EQ("",
  197. StatsMgr::instance().getObservation("delta")->getString().first);
  198. // Resetting statistics should not remove them
  199. EXPECT_EQ(4, StatsMgr::instance().count());
  200. }
  201. // This test checks whether existing statistics can be reset.
  202. TEST_F(StatsMgrTest, resetAll) {
  203. // Set a couple of statistics
  204. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  205. StatsMgr::instance().setValue("beta", 12.34);
  206. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  207. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  208. // This should reset alpha to 0
  209. EXPECT_NO_THROW(StatsMgr::instance().resetAll());
  210. EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
  211. EXPECT_EQ(0.0,
  212. StatsMgr::instance().getObservation("beta")->getFloat().first);
  213. EXPECT_EQ(time_duration(0,0,0,0),
  214. StatsMgr::instance().getObservation("gamma")->getDuration().first);
  215. EXPECT_EQ("",
  216. StatsMgr::instance().getObservation("delta")->getString().first);
  217. // Resetting all statistics should not remove them
  218. EXPECT_EQ(4, StatsMgr::instance().count());
  219. }
  220. // This test checks whether statistics can be removed.
  221. TEST_F(StatsMgrTest, removeAll) {
  222. // Set a couple of statistics
  223. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  224. StatsMgr::instance().setValue("beta", 12.34);
  225. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  226. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  227. // This should reset alpha to 0
  228. EXPECT_NO_THROW(StatsMgr::instance().removeAll());
  229. // Resetting all statistics should not remove them
  230. EXPECT_EQ(0, StatsMgr::instance().count());
  231. // There should be no such statistics anymore
  232. EXPECT_EQ("{ }", StatsMgr::instance().get("alpha")->str());
  233. EXPECT_EQ("{ }", StatsMgr::instance().get("beta")->str());
  234. EXPECT_EQ("{ }", StatsMgr::instance().get("gamma")->str());
  235. EXPECT_EQ("{ }", StatsMgr::instance().get("delta")->str());
  236. // There should be no such statistics anymore
  237. EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
  238. EXPECT_FALSE(StatsMgr::instance().getObservation("beta"));
  239. EXPECT_FALSE(StatsMgr::instance().getObservation("gamma"));
  240. EXPECT_FALSE(StatsMgr::instance().getObservation("delta"));
  241. }
  242. // This is a performance benchmark that checks how long does it take
  243. // to increment a single statistic million times.
  244. //
  245. // Data points:
  246. // It took 00:00:00.363709 (363ms) on late 2013 Mac with Mac OS X 10.9.5.
  247. TEST_F(StatsMgrTest, DISABLED_performanceSingleAdd) {
  248. StatsMgr::instance().removeAll();
  249. uint32_t cycles = 1000000;
  250. ptime before = microsec_clock::local_time();
  251. for (uint32_t i = 0; i < cycles; ++i) {
  252. StatsMgr::instance().addValue("metric1", 0.1*i);
  253. }
  254. ptime after = microsec_clock::local_time();
  255. time_duration dur = after - before;
  256. std::cout << "Incrementing a single statistic " << cycles << " times took: "
  257. << isc::util::durationToText(dur) << std::endl;
  258. }
  259. // This is a performance benchmark that checks how long does it take
  260. // to set absolute value of a single statistic million times.
  261. //
  262. // Data points:
  263. // It took 00:00:00.361003 (361ms) on late 2013 Mac with Mac OS X 10.9.5.
  264. TEST_F(StatsMgrTest, DISABLED_performanceSingleSet) {
  265. StatsMgr::instance().removeAll();
  266. uint32_t cycles = 1000000;
  267. ptime before = microsec_clock::local_time();
  268. for (uint32_t i = 0; i < cycles; ++i) {
  269. StatsMgr::instance().setValue("metric1", 0.1*i);
  270. }
  271. ptime after = microsec_clock::local_time();
  272. time_duration dur = after - before;
  273. std::cout << "Setting a single statistic " << cycles << " times took: "
  274. << isc::util::durationToText(dur) << std::endl;
  275. }
  276. // This is a performance benchmark that checks how long does it take to
  277. // increment one statistic a million times, when there is 1000 other statistics
  278. // present.
  279. //
  280. // Data points:
  281. // 00:00:00.436943 (436ms) on late 2013 Mac with Mac OS X 10.9.5
  282. TEST_F(StatsMgrTest, DISABLED_performanceMultipleAdd) {
  283. StatsMgr::instance().removeAll();
  284. uint32_t cycles = 1000000;
  285. uint32_t stats = 1000;
  286. for (uint32_t i = 0; i < stats; ++i) {
  287. std::stringstream tmp;
  288. tmp << "statistic" << i;
  289. StatsMgr::instance().setValue(tmp.str(), static_cast<uint64_t>(i));
  290. }
  291. ptime before = microsec_clock::local_time();
  292. for (uint32_t i = 0; i < cycles; ++i) {
  293. StatsMgr::instance().addValue("metric1", static_cast<uint64_t>(i));
  294. }
  295. ptime after = microsec_clock::local_time();
  296. time_duration dur = after - before;
  297. std::cout << "Incrementing one of " << stats << " statistics " << cycles
  298. << " times took: " << isc::util::durationToText(dur) << std::endl;
  299. }
  300. // This is a performance benchmark that checks how long does it take to
  301. // set one statistic to a given value a million times, when there is 1000 other
  302. // statistics present.
  303. //
  304. // Data points:
  305. // 00:00:00.424518 (424ms) on late 2013 Mac with Mac OS X 10.9.5
  306. TEST_F(StatsMgrTest, DISABLED_performanceMultipleSet) {
  307. StatsMgr::instance().removeAll();
  308. uint32_t cycles = 1000000;
  309. uint32_t stats = 1000;
  310. for (uint32_t i = 0; i < stats; ++i) {
  311. std::stringstream tmp;
  312. tmp << "statistic" << i;
  313. StatsMgr::instance().setValue(tmp.str(), static_cast<uint64_t>(i));
  314. }
  315. ptime before = microsec_clock::local_time();
  316. for (uint32_t i = 0; i < cycles; ++i) {
  317. StatsMgr::instance().setValue("metric1", static_cast<uint64_t>(i));
  318. }
  319. ptime after = microsec_clock::local_time();
  320. time_duration dur = after - before;
  321. std::cout << "Setting one of " << stats << " statistics " << cycles
  322. << " times took: " << isc::util::durationToText(dur) << std::endl;
  323. }
  324. // Test checks if statistic-get handler is able to return specified statistic.
  325. TEST_F(StatsMgrTest, commandStatisticGet) {
  326. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  327. ElementPtr params = Element::createMap();
  328. params->set("name", Element::create("alpha"));
  329. ConstElementPtr rsp = StatsMgr::instance().statisticGetHandler("statistic-get",
  330. params);
  331. ObservationPtr alpha;
  332. EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
  333. ASSERT_TRUE(alpha);
  334. std::string exp = "{ \"alpha\": [ [ 1234, \""
  335. + isc::util::ptimeToText(alpha->getInteger().second) + "\" ] ] }";
  336. EXPECT_EQ("{ \"arguments\": " + exp + ", \"result\": 0 }", rsp->str());
  337. }
  338. // Test checks if statistic-get is able to handle:
  339. // - a request without parameters
  340. // - a request with missing statistic name
  341. // - a request for non-existing statistic.
  342. TEST_F(StatsMgrTest, commandStatisticGetNegative) {
  343. // Case 1: a request without parameters
  344. ConstElementPtr rsp = StatsMgr::instance().statisticGetHandler("statistic-get",
  345. ElementPtr());
  346. int status_code;
  347. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  348. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  349. // Case 2: a request with missing statistic name
  350. ElementPtr params = Element::createMap();
  351. rsp = StatsMgr::instance().statisticGetHandler("statistic-get", params);
  352. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  353. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  354. // Case 3: a request for non-existing statistic
  355. params->set("name", Element::create("alpha"));
  356. rsp = StatsMgr::instance().statisticGetHandler("statistic-get", params);
  357. EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", rsp->str());
  358. }
  359. // This test checks whether statistc-get-all command returns all statistics
  360. // correctly.
  361. TEST_F(StatsMgrTest, commandGetAll) {
  362. // Set a couple of statistics
  363. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  364. StatsMgr::instance().setValue("beta", 12.34);
  365. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  366. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  367. // Now get them. They're used to generate expected output
  368. ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
  369. ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
  370. ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
  371. ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
  372. ASSERT_TRUE(rep_alpha);
  373. ASSERT_TRUE(rep_beta);
  374. ASSERT_TRUE(rep_gamma);
  375. ASSERT_TRUE(rep_delta);
  376. std::string exp_str_alpha = "[ [ 1234, \""
  377. + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
  378. ->getInteger().second) + "\" ] ]";
  379. std::string exp_str_beta = "[ [ 12.34, \""
  380. + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
  381. ->getFloat().second) + "\" ] ]";
  382. std::string exp_str_gamma = "[ [ \"01:02:03.000004\", \""
  383. + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
  384. ->getDuration().second) + "\" ] ]";
  385. std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
  386. + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
  387. ->getString().second) + "\" ] ]";
  388. // Check that all of them can be reported at once
  389. ConstElementPtr rsp = StatsMgr::instance().statisticGetAllHandler(
  390. "statistic-get-all", ElementPtr());
  391. ASSERT_TRUE(rsp);
  392. int status_code;
  393. ConstElementPtr rep_all = parseAnswer(status_code, rsp);
  394. ASSERT_EQ(0, status_code);
  395. ASSERT_TRUE(rep_all);
  396. // Verifying this is a bit more involved, as we don't know whether the
  397. // order would be preserved or not.
  398. EXPECT_EQ(4, rep_all->size());
  399. ASSERT_TRUE(rep_all->get("alpha"));
  400. ASSERT_TRUE(rep_all->get("beta"));
  401. ASSERT_TRUE(rep_all->get("delta"));
  402. ASSERT_TRUE(rep_all->get("gamma"));
  403. EXPECT_FALSE(rep_all->get("epsilon"));
  404. EXPECT_EQ(exp_str_alpha, rep_all->get("alpha")->str());
  405. EXPECT_EQ(exp_str_beta, rep_all->get("beta")->str());
  406. EXPECT_EQ(exp_str_gamma, rep_all->get("gamma")->str());
  407. EXPECT_EQ(exp_str_delta, rep_all->get("delta")->str());
  408. }
  409. // Test checks if statistic-reset handler is able to reset specified statistic.
  410. TEST_F(StatsMgrTest, commandStatisticReset) {
  411. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  412. ElementPtr params = Element::createMap();
  413. params->set("name", Element::create("alpha"));
  414. ConstElementPtr rsp =
  415. StatsMgr::instance().statisticResetHandler("statistic-reset", params);
  416. int status_code;
  417. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  418. EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
  419. ObservationPtr alpha;
  420. EXPECT_NO_THROW(alpha = StatsMgr::instance().getObservation("alpha"));
  421. ASSERT_TRUE(alpha);
  422. // Check that it was indeed reset
  423. EXPECT_EQ(0, alpha->getInteger().first);
  424. }
  425. // Test checks if statistic-reset is able to handle:
  426. // - a request without parameters
  427. // - a request with missing statistic name
  428. // - a request for non-existing statistic.
  429. TEST_F(StatsMgrTest, commandStatisticResetNegative) {
  430. // Case 1: a request without parameters
  431. ConstElementPtr rsp =
  432. StatsMgr::instance().statisticResetHandler("statistic-reset", ElementPtr());
  433. int status_code;
  434. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  435. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  436. // Case 2: a request with missing statistic name
  437. ElementPtr params = Element::createMap();
  438. rsp = StatsMgr::instance().statisticResetHandler("statistic-reset", params);
  439. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  440. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  441. // Case 3: a request for non-existing statistic
  442. params->set("name", Element::create("alpha"));
  443. rsp = StatsMgr::instance().statisticResetHandler("statistic-reset", params);
  444. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'alpha' statistic found\" }",
  445. rsp->str());
  446. }
  447. // This test checks whether statistic-reset-all command really resets all
  448. // statistics correctly.
  449. TEST_F(StatsMgrTest, commandResetAll) {
  450. // Set a couple of statistics
  451. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  452. StatsMgr::instance().setValue("beta", 12.34);
  453. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  454. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  455. // Now get them. They're used to generate expected output
  456. ConstElementPtr rep_alpha = StatsMgr::instance().get("alpha");
  457. ConstElementPtr rep_beta = StatsMgr::instance().get("beta");
  458. ConstElementPtr rep_gamma = StatsMgr::instance().get("gamma");
  459. ConstElementPtr rep_delta = StatsMgr::instance().get("delta");
  460. ASSERT_TRUE(rep_alpha);
  461. ASSERT_TRUE(rep_beta);
  462. ASSERT_TRUE(rep_gamma);
  463. ASSERT_TRUE(rep_delta);
  464. std::string exp_str_alpha = "[ [ 1234, \""
  465. + isc::util::ptimeToText(StatsMgr::instance().getObservation("alpha")
  466. ->getInteger().second) + "\" ] ]";
  467. std::string exp_str_beta = "[ [ 12.34, \""
  468. + isc::util::ptimeToText(StatsMgr::instance().getObservation("beta")
  469. ->getFloat().second) + "\" ] ]";
  470. std::string exp_str_gamma = "[ [ \"01:02:03.000004\", \""
  471. + isc::util::ptimeToText(StatsMgr::instance().getObservation("gamma")
  472. ->getDuration().second) + "\" ] ]";
  473. std::string exp_str_delta = "[ [ \"Lorem ipsum\", \""
  474. + isc::util::ptimeToText(StatsMgr::instance().getObservation("delta")
  475. ->getString().second) + "\" ] ]";
  476. // Check that all of them can be reset at once
  477. ConstElementPtr rsp = StatsMgr::instance().statisticResetAllHandler(
  478. "statistic-reset-all", ElementPtr());
  479. ASSERT_TRUE(rsp);
  480. int status_code;
  481. ConstElementPtr rep_all = parseAnswer(status_code, rsp);
  482. ASSERT_EQ(0, status_code);
  483. ASSERT_TRUE(rep_all);
  484. // Check that they're indeed reset
  485. EXPECT_EQ(0, StatsMgr::instance().getObservation("alpha")->getInteger().first);
  486. EXPECT_EQ(0.0f,
  487. StatsMgr::instance().getObservation("beta")->getFloat().first);
  488. EXPECT_EQ(time_duration(0,0,0,0),
  489. StatsMgr::instance().getObservation("gamma")->getDuration().first);
  490. EXPECT_EQ("",
  491. StatsMgr::instance().getObservation("delta")->getString().first);
  492. }
  493. // Test checks if statistic-remove handler is able to remove a statistic.
  494. TEST_F(StatsMgrTest, commandStatisticRemove) {
  495. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  496. ElementPtr params = Element::createMap();
  497. params->set("name", Element::create("alpha"));
  498. ConstElementPtr rsp =
  499. StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
  500. int status_code;
  501. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  502. EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
  503. // It should be gone.
  504. EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
  505. }
  506. // Test checks if statistic-remove is able to handle:
  507. // - a request without parameters
  508. // - a request with missing statistic name
  509. // - a request for non-existing statistic.
  510. TEST_F(StatsMgrTest, commandStatisticRemoveNegative) {
  511. // Case 1: a request without parameters
  512. ConstElementPtr rsp =
  513. StatsMgr::instance().statisticRemoveHandler("statistic-remove", ElementPtr());
  514. int status_code;
  515. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  516. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  517. // Case 2: a request with missing statistic name
  518. ElementPtr params = Element::createMap();
  519. rsp = StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
  520. ASSERT_NO_THROW(parseAnswer(status_code, rsp));
  521. EXPECT_EQ(status_code, CONTROL_RESULT_ERROR);
  522. // Case 3: a request for non-existing statistic
  523. params->set("name", Element::create("alpha"));
  524. rsp = StatsMgr::instance().statisticRemoveHandler("statistic-remove", params);
  525. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'alpha' statistic found\" }",
  526. rsp->str());
  527. }
  528. // This test checks whether statistic-remove-all command really resets all
  529. // statistics correctly.
  530. TEST_F(StatsMgrTest, commandRemoveAll) {
  531. // Set a couple of statistics
  532. StatsMgr::instance().setValue("alpha", static_cast<uint64_t>(1234));
  533. StatsMgr::instance().setValue("beta", 12.34);
  534. StatsMgr::instance().setValue("gamma", time_duration(1,2,3,4));
  535. StatsMgr::instance().setValue("delta", "Lorem ipsum");
  536. // Check that all of them can be reset at once
  537. ConstElementPtr rsp = StatsMgr::instance().statisticRemoveAllHandler(
  538. "statistic-remove-all", ElementPtr());
  539. ASSERT_TRUE(rsp);
  540. int status_code;
  541. ConstElementPtr rep_all = parseAnswer(status_code, rsp);
  542. ASSERT_EQ(0, status_code);
  543. EXPECT_FALSE(StatsMgr::instance().getObservation("alpha"));
  544. EXPECT_FALSE(StatsMgr::instance().getObservation("beta"));
  545. EXPECT_FALSE(StatsMgr::instance().getObservation("gamma"));
  546. EXPECT_FALSE(StatsMgr::instance().getObservation("delta"));
  547. }
  548. };