stats_mgr.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // Copyright (C) 2012 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. #ifndef __STATS_MGR_H
  15. #define __STATS_MGR_H
  16. #include <iostream>
  17. #include <map>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/multi_index_container.hpp>
  21. #include <boost/multi_index/hashed_index.hpp>
  22. #include <boost/multi_index/sequenced_index.hpp>
  23. #include <boost/multi_index/global_fun.hpp>
  24. #include <boost/multi_index/mem_fun.hpp>
  25. #include <boost/date_time/posix_time/posix_time.hpp>
  26. #include <exceptions/exceptions.h>
  27. namespace isc {
  28. namespace perfdhcp {
  29. /// \brief Statistics Manager
  30. ///
  31. /// This class template is a storage for various performance statistics
  32. /// collected during performance tests execution with perfdhcp tool.
  33. ///
  34. /// Statistics Manager holds lists of sent and received packets and
  35. /// groups them into exchanges. For example: DHCPDISCOVER message and
  36. /// corresponding DHCPOFFER messages belong to one exchange, DHCPREQUEST
  37. /// and corresponding DHCPACK message belong to another exchange etc.
  38. /// In order to update statistics for a particular exchange type, client
  39. /// class passes sent and received packets. Internally, Statistics Manager
  40. /// tries to match transaction id of received packet with sent packet
  41. /// stored on the list of sent packets. When packets are matched the
  42. /// round trip time can be calculated.
  43. ///
  44. /// \param T class representing DHCPv4 or DHCPv6 packet.
  45. template <class T = dhcp::Pkt4>
  46. class StatsMgr : public boost::noncopyable {
  47. public:
  48. /// \brief Custom Counter
  49. ///
  50. /// This class represents custom statistics counters. Client class
  51. /// may create unlimited number of counters. Such counters are
  52. /// being stored in map in Statistics Manager and access using
  53. /// unique string key.
  54. class CustomCounter {
  55. public:
  56. /// \brief Constructor.
  57. ///
  58. /// This constructor sets counter name. This name is used in
  59. /// log file to report value of each counter.
  60. ///
  61. /// \param name name of the counter used in log file.
  62. CustomCounter(const std::string& name) :
  63. counter_(0),
  64. name_(name) { };
  65. /// \brief Increment operator.
  66. const CustomCounter& operator++() {
  67. ++counter_;
  68. return(*this);
  69. }
  70. /// \brief Increment operator.
  71. const CustomCounter& operator++(int) {
  72. CustomCounter& this_counter(*this);
  73. operator++();
  74. return(this_counter);
  75. }
  76. /// \brief Return counter value.
  77. ///
  78. /// Method returns counter value.
  79. ///
  80. /// \return counter value.
  81. uint64_t getValue() const { return(counter_); }
  82. /// \brief Return counter name.
  83. ///
  84. /// Method returns counter name.
  85. ///
  86. /// \return counter name.
  87. const std::string& getName() const { return(name_); }
  88. private:
  89. /// \brief Default constructor.
  90. ///
  91. /// Default constrcutor is private because we don't want client
  92. /// class to call it because we want client class to specify
  93. /// counter's name.
  94. CustomCounter() { };
  95. uint64_t counter_; ///< Counter's value.
  96. std::string name_; ///< Counter's name.
  97. };
  98. typedef typename boost::shared_ptr<CustomCounter> CustomCounterPtr;
  99. /// DHCP packet exchange types.
  100. enum ExchangeType {
  101. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  102. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  103. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  104. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  105. };
  106. /// \brief Exchange Statistics.
  107. ///
  108. /// This class collects statistics for exchanges. Parent class
  109. /// may define number of different packet exchanges like:
  110. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  111. /// statistics will be collected for each of those separately in
  112. /// corresponding instance of ExchangeStats.
  113. class ExchangeStats {
  114. public:
  115. /// \brief Hash transaction id of the packet.
  116. ///
  117. /// Function hashes transaction id of the packet. Hashing is
  118. /// non-unique. Many packets may have the same hash value and thus
  119. /// they belong to the same packet buckets. Packet buckets are
  120. /// used for unordered packets search with multi index container.
  121. ///
  122. /// \param packet packet which transaction id is to be hashed.
  123. /// \throw isc::BadValue if packet is null.
  124. /// \return transaction id hash.
  125. static uint32_t hashTransid(const boost::shared_ptr<T>& packet) {
  126. if (!packet) {
  127. isc_throw(BadValue, "Packet is null");
  128. }
  129. return(packet->getTransid() & 1023);
  130. }
  131. /// \brief List of packets (sent or received).
  132. ///
  133. /// List of packets based on multi index container allows efficient
  134. /// search of packets based on their sequence (order in which they
  135. /// were inserted) as well as based on their hashed transaction id.
  136. /// The first index (sequenced) provides the way to use container
  137. /// as a regular list (including iterators, removal of elements from
  138. /// the middle of the collection etc.). This index is meant to be used
  139. /// more frequently than the latter one and it is based on the
  140. /// assumption that responses from the DHCP server are received in
  141. /// order. In this case, when next packet is received it can be
  142. /// matched with next packet on the list of sent packets. This
  143. /// prevents intensive searches on the list of sent packets every
  144. /// time new packet arrives. In many cases however packets can be
  145. /// dropped by the server or may be sent out of order and we still
  146. /// want to have ability to search packets using transaction id.
  147. /// The second index can be used for this purpose. This index is
  148. /// hashing transaction ids using custom function \ref hashTransid.
  149. /// Note that other possibility would be to simply specify index
  150. /// that uses transaction id directly (instead of hashing with
  151. /// \ref hashTransid). In this case however we have chosen to use
  152. /// hashing function because it shortens the index size to just
  153. /// 1023 values maximum. Search operation on this index generally
  154. /// returns the range of packets that have the same transaction id
  155. /// hash assigned but most often these ranges will be short so further
  156. /// search within a range to find a packet with pacrticular transaction
  157. /// id will not be intensive.
  158. ///
  159. /// Example 1: Add elements to the list
  160. /// \code
  161. /// PktList packets_collection();
  162. /// boost::shared_ptr<Pkt4> pkt1(new Pkt4(...));
  163. /// boost::shared_ptr<Pkt4> pkt2(new Pkt4(...));
  164. /// // Add new packet to the container, it will be available through
  165. /// // both indexes
  166. /// packets_collection.push_back(pkt1);
  167. /// // Here is another way to add packet to the container. The result
  168. /// // is exactly the same as previously.
  169. /// packets_collection.template get<0>().push_back(pkt2);
  170. /// \endcode
  171. ///
  172. /// Example 2: Access elements through sequencial index
  173. /// \code
  174. /// PktList packets_collection();
  175. /// ... # Add elements to the container
  176. /// for (PktListIterator it = packets_collection.begin();
  177. /// it != packets_collection.end();
  178. /// ++it) {
  179. /// boost::shared_ptr<Pkt4> pkt = *it;
  180. /// # Do something with packet;
  181. /// }
  182. /// \endcode
  183. ///
  184. /// Example 3: Access elements through hashed index
  185. /// \code
  186. /// // Get the instance of the second search index.
  187. /// PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  188. /// // Get the range (bucket) of packets sharing the same transaction
  189. /// // id hash.
  190. /// std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  191. /// idx.equal_range(hashTransid(rcvd_packet));
  192. /// // Iterate through the returned bucket.
  193. /// for (PktListTransidHashIterator it = p.first; it != p.second;
  194. /// ++it) {
  195. /// boost::shared_ptr pkt = *it;
  196. /// ... # Do something with the packet (e.g. check transaction id)
  197. /// }
  198. /// \endcode
  199. typedef boost::multi_index_container<
  200. // Container holds shared_ptr<Pkt4> or shared_ptr<Pkt6> objects.
  201. boost::shared_ptr<T>,
  202. // List container indexes.
  203. boost::multi_index::indexed_by<
  204. // Sequenced index provides the way to use this container
  205. // in the same way as std::list.
  206. boost::multi_index::sequenced<>,
  207. // The other index keeps products of transaction id.
  208. boost::multi_index::hashed_non_unique<
  209. // Specify hash function to get the product of
  210. // transaction id. This product is obtained by calling
  211. // hashTransid() function.
  212. boost::multi_index::global_fun<
  213. // Hashing function takes shared_ptr<Pkt4> or
  214. // shared_ptr<Pkt6> as argument.
  215. const boost::shared_ptr<T>&,
  216. // ... and returns uint32 value.
  217. uint32_t,
  218. // ... and here is a reference to it.
  219. &ExchangeStats::hashTransid
  220. >
  221. >
  222. >
  223. > PktList;
  224. /// Packet list iterator for sequencial access to elements.
  225. typedef typename PktList::iterator PktListIterator;
  226. /// Packet list index to search packets using transaction id hash.
  227. typedef typename PktList::template nth_index<1>::type
  228. PktListTransidHashIndex;
  229. /// Packet list iterator to access packets using transaction id hash.
  230. typedef typename PktListTransidHashIndex::const_iterator
  231. PktListTransidHashIterator;
  232. /// \brief Constructor
  233. ///
  234. /// \param xchg_type exchange type
  235. /// \param archive_enabled if true packets archive mode is enabled.
  236. /// In this mode all packets are stored throughout the test execution.
  237. ExchangeStats(const ExchangeType xchg_type, const bool archive_enabled)
  238. : xchg_type_(xchg_type),
  239. sent_packets_(),
  240. rcvd_packets_(),
  241. archived_packets_(),
  242. archive_enabled_(archive_enabled),
  243. min_delay_(std::numeric_limits<double>::max()),
  244. max_delay_(0.),
  245. sum_delay_(0.),
  246. sum_delay_squared_(0.),
  247. orphans_(0),
  248. unordered_lookup_size_sum_(0),
  249. unordered_lookups_(0),
  250. ordered_lookups_(0),
  251. sent_packets_num_(0),
  252. rcvd_packets_num_(0)
  253. {
  254. next_sent_ = sent_packets_.begin();
  255. }
  256. /// \brief Add new packet to list of sent packets.
  257. ///
  258. /// Method adds new packet to list of sent packets.
  259. ///
  260. /// \param packet packet object to be added.
  261. /// \throw isc::BadValue if packet is null.
  262. void appendSent(const boost::shared_ptr<T>& packet) {
  263. if (!packet) {
  264. isc_throw(BadValue, "Packet is null");
  265. }
  266. ++sent_packets_num_;
  267. sent_packets_.template get<0>().push_back(packet);
  268. }
  269. /// \brief Add new packet to list of received packets.
  270. ///
  271. /// Method adds new packet to list of received packets.
  272. ///
  273. /// \param packet packet object to be added.
  274. /// \throw isc::BadValue if packet is null.
  275. void appendRcvd(const boost::shared_ptr<T>& packet) {
  276. if (!packet) {
  277. isc_throw(BadValue, "Packet is null");
  278. }
  279. rcvd_packets_.push_back(packet);
  280. }
  281. /// \brief Update delay counters.
  282. ///
  283. /// Method updates delay counters based on timestamps of
  284. /// sent and received packets.
  285. ///
  286. /// \param sent_packet sent packet
  287. /// \param rcvd_packet received packet
  288. /// \throw isc::BadValue if sent or received packet is null.
  289. /// \throw isc::Unexpected if failed to calculate timestamps
  290. void updateDelays(const boost::shared_ptr<T>& sent_packet,
  291. const boost::shared_ptr<T>& rcvd_packet) {
  292. if (!sent_packet) {
  293. isc_throw(BadValue, "Sent packet is null");
  294. }
  295. if (!rcvd_packet) {
  296. isc_throw(BadValue, "Received packet is null");
  297. }
  298. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  299. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  300. if (sent_time.is_not_a_date_time() ||
  301. rcvd_time.is_not_a_date_time()) {
  302. isc_throw(Unexpected,
  303. "Timestamp must be set for sent and "
  304. "received packet to measure RTT");
  305. }
  306. boost::posix_time::time_period period(sent_time, rcvd_time);
  307. // We don't bother calculating deltas in nanoseconds. It is much
  308. // more convenient to use seconds instead because we are going to
  309. // sum them up.
  310. double delta =
  311. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  312. if (delta < 0) {
  313. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  314. "greater than received packet's timestamp");
  315. }
  316. // Record the minimum delay between sent and received packets.
  317. if (delta < min_delay_) {
  318. min_delay_ = delta;
  319. }
  320. // Record the maximum delay between sent and received packets.
  321. if (delta > max_delay_) {
  322. max_delay_ = delta;
  323. }
  324. // Update delay sum and square sum. That will be used to calculate
  325. // mean delays.
  326. sum_delay_ += delta;
  327. sum_delay_squared_ += delta * delta;
  328. }
  329. /// \brief Match received packet with the corresponding sent packet.
  330. ///
  331. /// Method finds packet with specified transaction id on the list
  332. /// of sent packets. It is used to match received packet with
  333. /// corresponding sent packet.
  334. /// Since packets from the server most often come in the same order
  335. /// as they were sent by client, this method will first check if
  336. /// next sent packet matches. If it doesn't, function will search
  337. /// the packet using indexing by transaction id. This reduces
  338. /// packet search time significantly.
  339. ///
  340. /// \param rcvd_packet received packet to be matched with sent packet.
  341. /// \throw isc::BadValue if received packet is null.
  342. /// \return packet having specified transaction or NULL if packet
  343. /// not found
  344. boost::shared_ptr<T>
  345. matchPackets(const boost::shared_ptr<T>& rcvd_packet) {
  346. if (!rcvd_packet) {
  347. isc_throw(BadValue, "Received packet is null");
  348. }
  349. if (sent_packets_.size() == 0) {
  350. // List of sent packets is empty so there is no sense
  351. // to continue looking fo the packet. It also means
  352. // that the received packet we got has no corresponding
  353. // sent packet so orphans counter has to be updated.
  354. ++orphans_;
  355. return(boost::shared_ptr<T>());
  356. } else if (next_sent_ == sent_packets_.end()) {
  357. // Even if there are still many unmatched packets on the
  358. // list we might hit the end of it because of unordered
  359. // lookups. The next logical step is to reset iterator.
  360. next_sent_ = sent_packets_.begin();
  361. }
  362. // With this variable we will be signalling success or failure
  363. // to find the packet.
  364. bool packet_found = false;
  365. // Most likely responses are sent from the server in the same
  366. // order as client's requests to the server. We are caching
  367. // next sent packet and first try to match it with the next
  368. // incoming packet. We are successful if there is no
  369. // packet drop or out of order packets sent. This is actually
  370. // the fastest way to look for packets.
  371. if ((*next_sent_)->getTransid() == rcvd_packet->getTransid()) {
  372. ++ordered_lookups_;
  373. packet_found = true;
  374. } else {
  375. // If we are here, it means that we were unable to match the
  376. // next incoming packet with next sent packet so we need to
  377. // take a little more expensive approach to look packets using
  378. // alternative index (transaction id & 1023).
  379. PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  380. // Packets are grouped using trasaction id masked with value
  381. // of 1023. For instance, packets with transaction id equal to
  382. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  383. // When using alternative index we don't find the packet but
  384. // bucket of packets and we need to iterate through the bucket
  385. // to find the one that has desired transaction id.
  386. std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  387. idx.equal_range(hashTransid(rcvd_packet));
  388. // We want to keep statistics of unordered lookups to make
  389. // sure that there is a right balance between number of
  390. // unordered lookups and ordered lookups. If number of unordered
  391. // lookups is high it may mean that many packets are lost or
  392. // sent out of order.
  393. ++unordered_lookups_;
  394. // We also want to keep the mean value of the bucket. The lower
  395. // bucket size the better. If bucket sizes appear to big we
  396. // might want to increase number of buckets.
  397. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  398. for (PktListTransidHashIterator it = p.first; it != p.second;
  399. ++it) {
  400. if ((*it)->getTransid() == rcvd_packet->getTransid()) {
  401. packet_found = true;
  402. next_sent_ =
  403. sent_packets_.template project<0>(it);
  404. break;
  405. }
  406. }
  407. }
  408. if (!packet_found) {
  409. // If we are here, it means that both ordered lookup and
  410. // unordered lookup failed. Searched packet is not on the list.
  411. ++orphans_;
  412. return(boost::shared_ptr<T>());
  413. }
  414. // Packet is matched so we count it. We don't count unmatched packets
  415. // as they are counted as orphans with a separate counter.
  416. ++rcvd_packets_num_;
  417. boost::shared_ptr<T> sent_packet(*next_sent_);
  418. // If packet was found, we assume it will be never searched
  419. // again. We want to delete this packet from the list to
  420. // improve performance of future searches.
  421. next_sent_ = eraseSent(next_sent_);
  422. return(sent_packet);
  423. }
  424. /// \brief Return minumum delay between sent and received packet.
  425. ///
  426. /// Method returns minimum delay between sent and received packet.
  427. ///
  428. /// \return minimum delay between packets.
  429. double getMinDelay() const { return(min_delay_); }
  430. /// \brief Return maxmimum delay between sent and received packet.
  431. ///
  432. /// Method returns maximum delay between sent and received packet.
  433. ///
  434. /// \return maximum delay between packets.
  435. double getMaxDelay() const { return(max_delay_); }
  436. /// \brief Return avarage packet delay.
  437. ///
  438. /// Method returns average packet delay. If no packets have been
  439. /// received for this exchange avg delay can't be calculated and
  440. /// thus method throws exception.
  441. ///
  442. /// \throw isc::InvalidOperation if no packets for this exchange
  443. /// have been received yet.
  444. /// \return average packet delay.
  445. double getAvgDelay() const {
  446. if (rcvd_packets_num_ == 0) {
  447. isc_throw(InvalidOperation, "no packets received");
  448. }
  449. return(sum_delay_ / rcvd_packets_num_);
  450. }
  451. /// \brief Return standard deviation of packet delay.
  452. ///
  453. /// Method returns standard deviation of packet delay. If no
  454. /// packets have been received for this exchange, the standard
  455. /// deviation can't be calculated and thus method throws
  456. /// exception.
  457. ///
  458. /// \throw isc::InvalidOperation if number of received packets
  459. /// for the exchange is equal to zero.
  460. /// \return standard deviation of packet delay.
  461. double getStdDevDelay() const {
  462. if (rcvd_packets_num_ == 0) {
  463. isc_throw(InvalidOperation, "no packets received");
  464. }
  465. return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
  466. getAvgDelay() * getAvgDelay()));
  467. }
  468. /// \brief Return number of orphant packets.
  469. ///
  470. /// Method returns number of received packets that had no matching
  471. /// sent packet. It is possible that such packet was late or not
  472. /// for us.
  473. ///
  474. /// \return number of orphant received packets.
  475. uint64_t getOrphans() const { return(orphans_); }
  476. /// \brief Return average unordered lookup set size.
  477. ///
  478. /// Method returns average unordered lookup set size.
  479. /// This value changes every time \ref ExchangeStats::matchPackets
  480. /// function performs unordered packet lookup.
  481. ///
  482. /// \throw isc::InvalidOperation if there have been no unordered
  483. /// lookups yet.
  484. /// \return average unordered lookup set size.
  485. double getAvgUnorderedLookupSetSize() const {
  486. if (unordered_lookups_ == 0) {
  487. isc_throw(InvalidOperation, "no unordered lookups");
  488. }
  489. return(static_cast<double>(unordered_lookup_size_sum_) /
  490. static_cast<double>(unordered_lookups_));
  491. }
  492. /// \brief Return number of unordered sent packets lookups
  493. ///
  494. /// Method returns number of unordered sent packet lookups.
  495. /// Unordered lookup is used when received packet was sent
  496. /// out of order by server - transaction id of received
  497. /// packet does not match transaction id of next sent packet.
  498. ///
  499. /// \return number of unordered lookups.
  500. uint64_t getUnorderedLookups() const { return(unordered_lookups_); }
  501. /// \brief Return number of ordered sent packets lookups
  502. ///
  503. /// Method returns number of ordered sent packet lookups.
  504. /// Ordered lookup is used when packets are received in the
  505. /// same order as they were sent to the server.
  506. /// If packets are skipped or received out of order, lookup
  507. /// function will use unordered lookup (with hash table).
  508. ///
  509. /// \return number of ordered lookups.
  510. uint64_t getOrderedLookups() const { return(ordered_lookups_); }
  511. /// \brief Return total number of sent packets
  512. ///
  513. /// Method returns total number of sent packets.
  514. ///
  515. /// \return number of sent packets.
  516. uint64_t getSentPacketsNum() const { return(sent_packets_num_); }
  517. /// \brief Return total number of received packets
  518. ///
  519. /// Method returns total number of received packets.
  520. ///
  521. /// \return number of received packets.
  522. uint64_t getRcvdPacketsNum() const { return(rcvd_packets_num_); }
  523. /// \brief Return number of dropped packets.
  524. ///
  525. /// Method returns number of dropped packets.
  526. ///
  527. /// \return number of dropped packets.
  528. uint64_t getDroppedPacketsNum() const {
  529. uint64_t drops = 0;
  530. if (getSentPacketsNum() > getRcvdPacketsNum()) {
  531. drops = getSentPacketsNum() - getRcvdPacketsNum();
  532. }
  533. return(drops);
  534. }
  535. /// \brief Print main statistics for packet exchange.
  536. ///
  537. /// Method prints main statistics for particular exchange.
  538. /// Statistics includes: number of sent and received packets,
  539. /// number of dropped packets and number of orphans.
  540. void printMainStats() const {
  541. using namespace std;
  542. cout << "sent packets: " << getSentPacketsNum() << endl
  543. << "received packets: " << getRcvdPacketsNum() << endl
  544. << "drops: " << getDroppedPacketsNum() << endl
  545. << "orphans: " << getOrphans() << endl;
  546. }
  547. /// \brief Print round trip time packets statistics.
  548. ///
  549. /// Method prints round trip time packets statistics. Statistics
  550. /// includes minimum packet delay, maximum packet delay, average
  551. /// packet delay and standard deviation of delays. Packet delay
  552. /// is a duration between sending a packet to server and receiving
  553. /// response from server.
  554. void printRTTStats() const {
  555. using namespace std;
  556. try {
  557. cout << fixed << setprecision(3)
  558. << "min delay: " << getMinDelay() * 1e3 << " ms" << endl
  559. << "avg delay: " << getAvgDelay() * 1e3 << " ms" << endl
  560. << "max delay: " << getMaxDelay() * 1e3 << " ms" << endl
  561. << "std deviation: " << getStdDevDelay() * 1e3 << " ms"
  562. << endl;
  563. } catch (const Exception& e) {
  564. cout << "Unavailable! No packets received." << endl;
  565. }
  566. }
  567. //// \brief Print timestamps for sent and received packets.
  568. ///
  569. /// Method prints timestamps for all sent and received packets for
  570. /// packet exchange. In order to run this method the packets
  571. /// archiving mode has to be enabled during object constructions.
  572. /// Otherwise sent packets are not stored during tests execution
  573. /// and this method has no ability to get and print their timestamps.
  574. ///
  575. /// \throw isc::InvalidOperation if found packet with no timestamp or
  576. /// if packets archive mode is disabled.
  577. void printTimestamps() {
  578. // If archive mode is disabled there is no sense to proceed
  579. // because we don't have packets and their timestamps.
  580. if (!archive_enabled_) {
  581. isc_throw(isc::InvalidOperation,
  582. "packets archive mode is disabled");
  583. }
  584. if (rcvd_packets_num_ == 0) {
  585. std::cout << "Unavailable! No packets received." << std::endl;
  586. }
  587. // We will be using boost::posix_time extensivelly here
  588. using namespace boost::posix_time;
  589. // Iterate through all received packets.
  590. for (PktListIterator it = rcvd_packets_.begin();
  591. it != rcvd_packets_.end();
  592. ++it) {
  593. boost::shared_ptr<T> rcvd_packet = *it;
  594. PktListTransidHashIndex& idx =
  595. archived_packets_.template get<1>();
  596. std::pair<PktListTransidHashIterator,
  597. PktListTransidHashIterator> p =
  598. idx.equal_range(hashTransid(rcvd_packet));
  599. for (PktListTransidHashIterator it_archived = p.first;
  600. it_archived != p.second;
  601. ++it) {
  602. if ((*it_archived)->getTransid() ==
  603. rcvd_packet->getTransid()) {
  604. boost::shared_ptr<T> sent_packet = *it_archived;
  605. // Get sent and received packet times.
  606. ptime sent_time = sent_packet->getTimestamp();
  607. ptime rcvd_time = rcvd_packet->getTimestamp();
  608. // All sent and received packets should have timestamps
  609. // set but if there is a bug somewhere and packet does
  610. // not have timestamp we want to catch this here.
  611. if (sent_time.is_not_a_date_time() ||
  612. rcvd_time.is_not_a_date_time()) {
  613. isc_throw(InvalidOperation,
  614. "packet time is not set");
  615. }
  616. // Calculate durations of packets from beginning of epoch.
  617. ptime epoch_time(min_date_time);
  618. time_period sent_period(epoch_time, sent_time);
  619. time_period rcvd_period(epoch_time, rcvd_time);
  620. // Print timestamps for sent and received packet.
  621. std::cout << "sent / received: "
  622. << to_iso_string(sent_period.length())
  623. << " / "
  624. << to_iso_string(rcvd_period.length())
  625. << std::endl;
  626. break;
  627. }
  628. }
  629. }
  630. }
  631. private:
  632. /// \brief Private default constructor.
  633. ///
  634. /// Default constructor is private because we want the client
  635. /// class to specify exchange type explicitely.
  636. ExchangeStats();
  637. /// \brief Erase packet from the list of sent packets.
  638. ///
  639. /// Method erases packet from the list of sent packets.
  640. ///
  641. /// \param it iterator pointing to packet to be erased.
  642. /// \return iterator pointing to packet following erased
  643. /// packet or sent_packets_.end() if packet not found.
  644. PktListIterator eraseSent(const PktListIterator it) {
  645. if (archive_enabled_) {
  646. // We don't want to keep list of all sent packets
  647. // because it will affect packet lookup performance.
  648. // If packet is matched with received packet we
  649. // move it to list of archived packets. List of
  650. // archived packets may be used for diagnostics
  651. // when test is completed.
  652. archived_packets_.push_back(*it);
  653. }
  654. // get<0>() template returns sequencial index to
  655. // container.
  656. return(sent_packets_.template get<0>().erase(it));
  657. }
  658. ExchangeType xchg_type_; ///< Packet exchange type.
  659. PktList sent_packets_; ///< List of sent packets.
  660. /// Iterator pointing to the packet on sent list which will most
  661. /// likely match next received packet. This is based on the
  662. /// assumption that server responds in order to incoming packets.
  663. PktListIterator next_sent_;
  664. PktList rcvd_packets_; ///< List of received packets.
  665. /// List of archived packets. All sent packets that have
  666. /// been matched with received packet are moved to this
  667. /// list for diagnostics purposes.
  668. PktList archived_packets_;
  669. /// Indicates all packets have to be preserved after matching.
  670. /// By default this is disabled which means that when received
  671. /// packet is matched with sent packet both are deleted. This
  672. /// is important when test is executed for extended period of
  673. /// time and high memory usage might be the issue.
  674. /// When timestamps listing is specified from the command line
  675. /// (using diagnostics selector), all packets have to be preserved
  676. /// so as the printing method may read their timestamps and
  677. /// print it to user. In such usage model it will be rare to
  678. /// run test for extended period of time so it should be fine
  679. /// to keep all packets archived throughout the test.
  680. bool archive_enabled_;
  681. double min_delay_; ///< Minimum delay between sent
  682. ///< and received packets.
  683. double max_delay_; ///< Maximum delay between sent
  684. ///< and received packets.
  685. double sum_delay_; ///< Sum of delays between sent
  686. ///< and received packets.
  687. double sum_delay_squared_; ///< Squared sum of delays between
  688. ///< sent and recived packets.
  689. uint64_t orphans_; ///< Number of orphant received packets.
  690. /// Sum of unordered lookup sets. Needed to calculate mean size of
  691. /// lookup set. It is desired that number of unordered lookups is
  692. /// minimal for performance reasons. Tracking number of lookups and
  693. /// mean size of the lookup set should give idea of packets serach
  694. /// complexity.
  695. uint64_t unordered_lookup_size_sum_;
  696. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  697. ///< lookups.
  698. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  699. ///< lookups.
  700. uint64_t sent_packets_num_; ///< Total number of sent packets.
  701. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  702. };
  703. /// Pointer to ExchangeStats.
  704. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  705. /// Map containing all specified exchange types.
  706. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  707. /// Iterator poiting to \ref ExchangesMap
  708. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  709. /// Map containing custom counters.
  710. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  711. /// Iterator for \ref CustomCountersMap.
  712. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  713. /// \brief Constructor.
  714. ///
  715. /// This constructor by default disables packets archiving mode.
  716. /// In this mode all packets from the list of sent packets are
  717. /// moved to list of archived packets once they have been matched
  718. /// with received packets. This is required if it has been selected
  719. /// from the command line to print timestamps for all packets after
  720. /// the test. If this is not selected archiving should be disabled
  721. /// for performance reasons and to avoid waste of memory for storing
  722. /// large list of archived packets.
  723. ///
  724. /// \param archive_enabled true indicates that packets
  725. /// archive mode is enabled.
  726. StatsMgr(const bool archive_enabled = false) :
  727. exchanges_(),
  728. custom_counters_(),
  729. archive_enabled_(archive_enabled),
  730. boot_time_(boost::posix_time::microsec_clock::universal_time()) {
  731. }
  732. /// \brief Specify new exchange type.
  733. ///
  734. /// This method creates new \ref ExchangeStats object that will
  735. /// collect statistics data from packets exchange of the specified
  736. /// type.
  737. ///
  738. /// \param xchg_type exchange type.
  739. /// \throw isc::BadValue if exchange of specified type exists.
  740. void addExchangeStats(const ExchangeType xchg_type) {
  741. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  742. isc_throw(BadValue, "Exchange of specified type already added.");
  743. }
  744. exchanges_[xchg_type] =
  745. ExchangeStatsPtr(new ExchangeStats(xchg_type, archive_enabled_));
  746. }
  747. /// \brief Add named custom uint64 counter.
  748. ///
  749. /// Method creates new named counter and stores in counter's map under
  750. /// key specified here as short_name.
  751. ///
  752. /// \param short_name key to use to access counter in the map.
  753. /// \param long_name name of the counter presented in the log file.
  754. void addCustomCounter(const std::string& short_name,
  755. const std::string& long_name) {
  756. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  757. isc_throw(BadValue,
  758. "Custom counter " << short_name << " already added.");
  759. }
  760. custom_counters_[short_name] =
  761. CustomCounterPtr(new CustomCounter(long_name));
  762. }
  763. /// \brief Return specified counter.
  764. ///
  765. /// Method returns specified counter.
  766. ///
  767. /// \param counter_key key poiting to the counter in the counters map.
  768. /// The short counter name has to be used to access counter.
  769. /// \return pointer to specified counter object.
  770. CustomCounterPtr getCounter(const std::string& counter_key) {
  771. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  772. if (it == custom_counters_.end()) {
  773. isc_throw(BadValue,
  774. "Custom counter " << counter_key << "does not exist");
  775. }
  776. return(it->second);
  777. }
  778. /// \brief Increment specified counter.
  779. ///
  780. /// Increement counter value by one.
  781. ///
  782. /// \param counter_key key poitinh to the counter in the counters map.
  783. /// \return pointer to specified counter after incrementation.
  784. const CustomCounter& incrementCounter(const std::string& counter_key) {
  785. CustomCounterPtr counter = getCounter(counter_key);
  786. return(++(*counter));
  787. }
  788. /// \brief Adds new packet to the sent packets list.
  789. ///
  790. /// Method adds new packet to the sent packets list.
  791. /// Packets are added to the list sequentially and
  792. /// most often read sequentially.
  793. ///
  794. /// \param xchg_type exchange type.
  795. /// \param packet packet to be added to the list
  796. /// \throw isc::BadValue if invalid exchange type specified or
  797. /// packet is null.
  798. void passSentPacket(const ExchangeType xchg_type,
  799. const boost::shared_ptr<T>& packet) {
  800. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  801. xchg_stats->appendSent(packet);
  802. }
  803. /// \brief Add new received packet and match with sent packet.
  804. ///
  805. /// Method adds new packet to the list of received packets. It
  806. /// also searches for corresponding packet on the list of sent
  807. /// packets. When packets are matched the statistics counters
  808. /// are updated accordingly for the particular exchange type.
  809. ///
  810. /// \param xchg_type exchange type.
  811. /// \param packet received packet
  812. /// \throw isc::BadValue if invalid exchange type specified
  813. /// or packet is null.
  814. /// \throw isc::Unexpected if corresponding packet was not
  815. /// found on the list of sent packets.
  816. boost::shared_ptr<T>
  817. passRcvdPacket(const ExchangeType xchg_type,
  818. const boost::shared_ptr<T>& packet) {
  819. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  820. boost::shared_ptr<T> sent_packet
  821. = xchg_stats->matchPackets(packet);
  822. if (sent_packet) {
  823. xchg_stats->updateDelays(sent_packet, packet);
  824. if (archive_enabled_) {
  825. xchg_stats->appendRcvd(packet);
  826. }
  827. }
  828. return(sent_packet);
  829. }
  830. /// \brief Return minumum delay between sent and received packet.
  831. ///
  832. /// Method returns minimum delay between sent and received packet
  833. /// for specified exchange type.
  834. ///
  835. /// \param xchg_type exchange type.
  836. /// \throw isc::BadValue if invalid exchange type specified.
  837. /// \return minimum delay between packets.
  838. double getMinDelay(const ExchangeType xchg_type) const {
  839. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  840. return(xchg_stats->getMinDelay());
  841. }
  842. /// \brief Return maxmimum delay between sent and received packet.
  843. ///
  844. /// Method returns maximum delay between sent and received packet
  845. /// for specified exchange type.
  846. ///
  847. /// \param xchg_type exchange type.
  848. /// \throw isc::BadValue if invalid exchange type specified.
  849. /// \return maximum delay between packets.
  850. double getMaxDelay(const ExchangeType xchg_type) const {
  851. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  852. return(xchg_stats->getMaxDelay());
  853. }
  854. /// \brief Return avarage packet delay.
  855. ///
  856. /// Method returns average packet delay for specified
  857. /// exchange type.
  858. ///
  859. /// \return average packet delay.
  860. double getAvgDelay(const ExchangeType xchg_type) const {
  861. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  862. return(xchg_stats->getAvgDelay());
  863. }
  864. /// \brief Return standard deviation of packet delay.
  865. ///
  866. /// Method returns standard deviation of packet delay
  867. /// for specified exchange type.
  868. ///
  869. /// \return standard deviation of packet delay.
  870. double getStdDevDelay(const ExchangeType xchg_type) const {
  871. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  872. return(xchg_stats->getStdDevDelay());
  873. }
  874. /// \brief Return number of orphant packets.
  875. ///
  876. /// Method returns number of orphant packets for specified
  877. /// exchange type.
  878. ///
  879. /// \param xchg_type exchange type.
  880. /// \throw isc::BadValue if invalid exchange type specified.
  881. /// \return number of orphant packets so far.
  882. uint64_t getOrphans(const ExchangeType xchg_type) const {
  883. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  884. return(xchg_stats->getOrphans());
  885. }
  886. /// \brief Return average unordered lookup set size.
  887. ///
  888. /// Method returns average unordered lookup set size.
  889. /// This value changes every time \ref ExchangeStats::matchPackets
  890. /// function performs unordered packet lookup.
  891. ///
  892. /// \param xchg_type exchange type.
  893. /// \throw isc::BadValue if invalid exchange type specified.
  894. /// \return average unordered lookup set size.
  895. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  896. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  897. return(xchg_stats->getAvgUnorderedLookupSetSize());
  898. }
  899. /// \brief Return number of unordered sent packets lookups
  900. ///
  901. /// Method returns number of unordered sent packet lookups.
  902. /// Unordered lookup is used when received packet was sent
  903. /// out of order by server - transaction id of received
  904. /// packet does not match transaction id of next sent packet.
  905. ///
  906. /// \param xchg_type exchange type.
  907. /// \throw isc::BadValue if invalid exchange type specified.
  908. /// \return number of unordered lookups.
  909. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  910. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  911. return(xchg_stats->getUnorderedLookups());
  912. }
  913. /// \brief Return number of ordered sent packets lookups
  914. ///
  915. /// Method returns number of ordered sent packet lookups.
  916. /// Ordered lookup is used when packets are received in the
  917. /// same order as they were sent to the server.
  918. /// If packets are skipped or received out of order, lookup
  919. /// function will use unordered lookup (with hash table).
  920. ///
  921. /// \param xchg_type exchange type.
  922. /// \throw isc::BadValue if invalid exchange type specified.
  923. /// \return number of ordered lookups.
  924. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  925. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  926. return(xchg_stats->getOrderedLookups());
  927. }
  928. /// \brief Return total number of sent packets
  929. ///
  930. /// Method returns total number of sent packets for specified
  931. /// exchange type.
  932. ///
  933. /// \param xchg_type exchange type.
  934. /// \throw isc::BadValue if invalid exchange type specified.
  935. /// \return number of sent packets.
  936. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  937. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  938. return(xchg_stats->getSentPacketsNum());
  939. }
  940. /// \brief Return total number of received packets
  941. ///
  942. /// Method returns total number of received packets for specified
  943. /// exchange type.
  944. ///
  945. /// \param xchg_type exchange type.
  946. /// \throw isc::BadValue if invalid exchange type specified.
  947. /// \return number of received packets.
  948. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  949. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  950. return(xchg_stats->getRcvdPacketsNum());
  951. }
  952. /// \brief Return total number of dropped packets.
  953. ///
  954. /// Method returns total number of dropped packets for specified
  955. /// exchange type.
  956. ///
  957. /// \param xchg_type exchange type.
  958. /// \throw isc::BadValue if invalid exchange type specified.
  959. /// \return number of dropped packets.
  960. uint64_t getDroppedPacketsNum(const ExchangeType xchg_type) const {
  961. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  962. return(xchg_stats->getDroppedPacketsNum());
  963. }
  964. /// \brief Get time period since the start of test.
  965. ///
  966. /// Calculate dna return period since the test start. This
  967. /// can be specifically helpful when calculating packet
  968. /// exchange rates.
  969. ///
  970. /// \return test period so far.
  971. boost::posix_time::time_period getTestPeriod() const {
  972. using namespace boost::posix_time;
  973. time_period test_period(boot_time_,
  974. microsec_clock::universal_time());
  975. return test_period;
  976. }
  977. /// \brief Return name of the exchange.
  978. ///
  979. /// Method returns name of the specified exchange type.
  980. /// This function is mainly for logging purposes.
  981. ///
  982. /// \param xchg_type exchange type.
  983. /// \return string representing name of the exchange.
  984. std::string exchangeToString(ExchangeType xchg_type) const {
  985. switch(xchg_type) {
  986. case XCHG_DO:
  987. return("DISCOVER-OFFER");
  988. case XCHG_RA:
  989. return("REQUEST-ACK");
  990. case XCHG_SA:
  991. return("SOLICIT-ADVERTISE");
  992. case XCHG_RR:
  993. return("REQUEST-REPLY");
  994. default:
  995. return("Unknown exchange type");
  996. }
  997. }
  998. /// \brief Print statistics counters for all exchange types.
  999. ///
  1000. /// Method prints statistics for all exchange types.
  1001. /// Statistics includes:
  1002. /// - number of sent and received packets
  1003. /// - number of dropped packets and number of orphans
  1004. /// - minimum packets delay,
  1005. /// - average packets delay,
  1006. /// - maximum packets delay,
  1007. /// - standard deviation of packets delay.
  1008. ///
  1009. /// \throw isc::InvalidOperation if no exchange type added to
  1010. /// track statistics.
  1011. void printStats() const {
  1012. if (exchanges_.size() == 0) {
  1013. isc_throw(isc::InvalidOperation,
  1014. "no exchange type added for tracking");
  1015. }
  1016. for (ExchangesMapIterator it = exchanges_.begin();
  1017. it != exchanges_.end();
  1018. ++it) {
  1019. ExchangeStatsPtr xchg_stats = it->second;
  1020. std::cout << "***Statistics for: " << exchangeToString(it->first)
  1021. << "***" << std::endl;
  1022. xchg_stats->printMainStats();
  1023. std::cout << std::endl;
  1024. xchg_stats->printRTTStats();
  1025. std::cout << std::endl;
  1026. }
  1027. }
  1028. /// \brief Print intermediate statistics.
  1029. ///
  1030. /// Method prints intermediate statistics for all exchanges.
  1031. /// Statistics includes sent, received and dropped packets
  1032. /// counters.
  1033. void printIntermediateStats() const {
  1034. std::ostringstream stream_sent;
  1035. std::ostringstream stream_rcvd;
  1036. std::ostringstream stream_drops;
  1037. std::string sep("");
  1038. for (ExchangesMapIterator it = exchanges_.begin();
  1039. it != exchanges_.end(); ++it) {
  1040. if (it != exchanges_.begin()) {
  1041. sep = "/";
  1042. }
  1043. stream_sent << sep << it->second->getSentPacketsNum();
  1044. stream_rcvd << sep << it->second->getRcvdPacketsNum();
  1045. stream_drops << sep << it->second->getDroppedPacketsNum();
  1046. }
  1047. std::cout << "sent: " << stream_sent.str()
  1048. << "; received: " << stream_rcvd.str()
  1049. << "; drops: " << stream_drops.str()
  1050. << std::endl;
  1051. }
  1052. /// \brief Print timestamps of all packets.
  1053. ///
  1054. /// Method prints timestamps of all sent and received
  1055. /// packets for all defined exchange types.
  1056. ///
  1057. /// \throw isc::InvalidOperation if one of the packets has
  1058. /// no timestamp value set or if packets archive mode is
  1059. /// disabled.
  1060. ///
  1061. /// \throw isc::InvalidOperation if no exchange type added to
  1062. /// track statistics or packets archive mode is disabled.
  1063. void printTimestamps() const {
  1064. if (exchanges_.size() == 0) {
  1065. isc_throw(isc::InvalidOperation,
  1066. "no exchange type added for tracking");
  1067. }
  1068. for (ExchangesMapIterator it = exchanges_.begin();
  1069. it != exchanges_.end();
  1070. ++it) {
  1071. ExchangeStatsPtr xchg_stats = it->second;
  1072. std::cout << "***Timestamps for packets: "
  1073. << exchangeToString(it->first)
  1074. << "***" << std::endl;
  1075. xchg_stats->printTimestamps();
  1076. std::cout << std::endl;
  1077. }
  1078. }
  1079. /// \brief Print names and values of custom counters.
  1080. ///
  1081. /// Method prints names and values of custom counters. Custom counters
  1082. /// are defined by client class for tracking different statistics.
  1083. ///
  1084. /// \throw isc::InvalidOperation if no custom counters added for tracking.
  1085. void printCustomCounters() const {
  1086. if (custom_counters_.size() == 0) {
  1087. isc_throw(isc::InvalidOperation, "no custom counters specified");
  1088. }
  1089. for (CustomCountersMapIterator it = custom_counters_.begin();
  1090. it != custom_counters_.end();
  1091. ++it) {
  1092. CustomCounterPtr counter = it->second;
  1093. std::cout << counter->getName() << ": " << counter->getValue()
  1094. << std::endl;
  1095. }
  1096. }
  1097. private:
  1098. /// \brief Return exchange stats object for given exchange type
  1099. ///
  1100. /// Method returns exchange stats object for given exchange type.
  1101. ///
  1102. /// \param xchg_type exchange type.
  1103. /// \throw isc::BadValue if invalid exchange type specified.
  1104. /// \return exchange stats object.
  1105. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  1106. ExchangesMapIterator it = exchanges_.find(xchg_type);
  1107. if (it == exchanges_.end()) {
  1108. isc_throw(BadValue, "Packets exchange not specified");
  1109. }
  1110. ExchangeStatsPtr xchg_stats = it->second;
  1111. return(xchg_stats);
  1112. }
  1113. ExchangesMap exchanges_; ///< Map of exchange types.
  1114. CustomCountersMap custom_counters_; ///< Map with custom counters.
  1115. /// Indicates that packets from list of sent packets should be
  1116. /// archived (moved to list of archived packets) once they are
  1117. /// matched with received packets. This is required when it has
  1118. /// been selected from the command line to print packets'
  1119. /// timestamps after test. This may affect performance and
  1120. /// consume large amount of memory when the test is running
  1121. /// for extended period of time and many packets have to be
  1122. /// archived.
  1123. bool archive_enabled_;
  1124. boost::posix_time::ptime boot_time_; ///< Time when test is started.
  1125. };
  1126. } // namespace perfdhcp
  1127. } // namespace isc
  1128. #endif // __STATS_MGR_H