stats_mgr.h 52 KB

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