stats_mgr.h 55 KB

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