stats_mgr.h 55 KB

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