stats_mgr.h 56 KB

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