stats_mgr.h 56 KB

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