Kea  1.9.9-git
stopwatch_impl.cc
Go to the documentation of this file.
1 // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <util/stopwatch_impl.h>
10 #include <iomanip>
11 #include <sstream>
12 
13 namespace isc {
14 namespace util {
15 
16 using namespace boost::posix_time;
17 
19  : started_(false),
20  last_start_(getCurrentTime()),
21  last_stop_(last_start_),
22  cumulative_time_(microseconds(0)) {
23 }
24 
26 }
27 
28 void
30  // If stopwatch is "stopped", start it.
31  if (!started_) {
32  last_start_ = getCurrentTime();
33  started_ = true;
34  }
35 }
36 
37 void
39  // Is stopwatch is "started", stop it.
40  if (started_) {
41  last_stop_ = getCurrentTime();
42  // Update the total time with the last measured duration.
43  cumulative_time_ += last_stop_ - last_start_;
44  started_ = false;
45  }
46 }
47 
48 void
50  // Set last start and stop values to the current time. This is the
51  // same as in the constructor. As a result the last duration will
52  // be 0.
53  last_start_ = getCurrentTime();
54  last_stop_ = last_start_;
55  // Set the total duration to 0.
56  cumulative_time_ = microseconds(0);
57  started_ = false;
58 }
59 
60 time_duration
62  // If the stopwatch is started, the time measured is between the
63  // start time and the current time. Otherwise, it is between the
64  // start time and last stop.
65  ptime end_time = started_ ? getCurrentTime() : last_stop_;
66  return (end_time - last_start_);
67 }
68 
69 time_duration
71  // Get the total time recorded so far.
72  time_duration total_duration = cumulative_time_;
73  if (started_) {
74  // If the stopwatch is started, add the duration between the
75  // start time and current time.
76  total_duration += (getCurrentTime() - last_start_);
77  }
78  return (total_duration);
79 }
80 
81 std::string
82 StopwatchImpl::logFormat(const boost::posix_time::time_duration& duration) {
83  std::ostringstream s;
84  s << duration.total_milliseconds() << ".";
85  s << std::setfill('0') << std::setw(3) << (duration.total_microseconds() % 1000)
86  << " ms";
87  return (s.str());
88 }
89 
90 ptime
92  return (microsec_clock::universal_time());
93 }
94 
95 
96 } // end of isc::util
97 } // end of isc
virtual ~StopwatchImpl()
Virtual destructor.
boost::posix_time::time_duration getLastDuration() const
Retrieves the measured duration.
boost::posix_time::time_duration getTotalDuration() const
Retrieves the total measured duration.
void reset()
Reset the stopwatch.
void stop()
Stop the stopwatch.
void start()
Starts the stopwatch.
virtual boost::posix_time::ptime getCurrentTime() const
Returns the current time.
Defines the logger used by the top-level component of kea-dhcp-ddns.
static std::string logFormat(const boost::posix_time::time_duration &duration)
Returns the duration in the textual format which can be directly used in log messages.
StopwatchImpl()
Constructor.