stopwatch_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2015 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 STOPWATCH_IMPL_H
  15. #define STOPWATCH_IMPL_H
  16. #include <boost/date_time/posix_time/posix_time.hpp>
  17. #include <boost/scoped_ptr.hpp>
  18. namespace isc {
  19. namespace util {
  20. /// @brief @c Stopwatch class implementation.
  21. ///
  22. /// The @c Stopwatch class uses the plimpl idiom to make it easier to unit
  23. /// test behavior of the @c Stopwatch class without a need to rely on the system
  24. /// clock. The @c StopwatchImpl API allows for overriding the @c getCurrentTime
  25. /// method to return the arbitrary time value as current time to various
  26. /// methods. By setting the current time to arbitrary values the test can expect
  27. /// arbitrary values being returned by the class methods.
  28. ///
  29. /// Also, by using the pimpl idiom the @c Stopwatch class hides its implementation
  30. /// details and leaves the header with only the pointer to the @c StopwatchImpl
  31. /// class.
  32. class StopwatchImpl {
  33. public:
  34. /// @brief Constructor.
  35. ///
  36. /// Initializes the internally used timestamps. It also sets the state of
  37. /// the stopwatch to "stopped".
  38. StopwatchImpl();
  39. /// @brief Virtual destructor.
  40. ///
  41. /// This destructor is virtual because the @c StopwatchImpl::getCurrentTime
  42. /// is virtual.
  43. virtual ~StopwatchImpl();
  44. /// @brief Starts the stopwatch.
  45. ///
  46. /// Sets the stopwatch to the "started" state. It also records the time when
  47. /// the stopwatch is started. This method is no-op if the stopwatch is
  48. /// already in the "started" state.
  49. ///
  50. /// Also see the @c Stopwatch::start for details.
  51. void start();
  52. /// @brief Stop the stopwatch.
  53. ///
  54. /// Sets the stopwatch to the "stopped" state. The stop time is recorded and
  55. /// the cumulative time is updated to include the duration between the most
  56. /// recent start and stop. This method is no-op if the stopwatch is already
  57. /// in the "stopped" state.
  58. ///
  59. /// Also see the @c Stopwatch::stop for details.
  60. void stop();
  61. /// @brief Reset the stopwatch.
  62. ///
  63. /// Also see the @c Stopwatch::reset for details.
  64. void reset();
  65. /// @brief Retrieves the measured duration.
  66. ///
  67. /// Also see the @c Stopwatch::getLastDuration for details.
  68. boost::posix_time::time_duration getLastDuration() const;
  69. /// @brief Retrieves the total measured duration.
  70. ///
  71. /// Also see the @c Stopwatch::getTotalDuration for details.
  72. boost::posix_time::time_duration getTotalDuration() const;
  73. protected:
  74. /// @brief Returns the current time.
  75. ///
  76. /// This method is used internally by the @c StopwatchImpl class and
  77. /// its derivations. This class simply returns the value of
  78. /// @c boost::posix_time::micrisec_clock::univeral_time(), which is
  79. /// a current timestamp. The derivations may replace it with the
  80. /// custom implementations. The typical use case is for the unit tests
  81. /// to customize the behavior of this function to return well known
  82. /// (deterministic) values. As a result, it is possible to influence
  83. /// the "measured" values returned by accessors of this class, which
  84. /// can be compared against some exact values.
  85. virtual boost::posix_time::ptime getCurrentTime() const;
  86. private:
  87. /// @brief Holds the state of the stopwatch.
  88. bool started_;
  89. /// @brief Holds the timestamp when the stopwatch has been last started.
  90. boost::posix_time::ptime last_start_;
  91. /// @brief Holds the timestamp when the stopwatch has been last stopped.
  92. boost::posix_time::ptime last_stop_;
  93. /// @brief Holds the total measured time since the stopwatch has been
  94. /// first started after creation or reset.
  95. boost::posix_time::time_duration cumulative_time_;
  96. };
  97. }
  98. }
  99. #endif // STOPWATCH_H