stats_mgr.h 55 KB

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