stats_mgr.h 55 KB

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