Kea 2.5.9
stopwatch_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2023 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
10#include <iomanip>
11#include <sstream>
12
13namespace isc {
14namespace util {
15
16using 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
28void
30 // If stopwatch is "stopped", start it.
31 if (!started_) {
32 last_start_ = getCurrentTime();
33 started_ = true;
34 }
35}
36
37void
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
48void
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
60time_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
69time_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
81std::string
82StopwatchImpl::logFormat(const boost::posix_time::time_duration& duration) {
83 std::ostringstream s;
84 if (duration.total_seconds() > 0) {
85 s << duration.total_seconds() << "."
86 << std::setfill('0') << std::setw(2) << (duration.total_milliseconds()/10 % 100)
87 << " s";
88 } else {
89 s << duration.total_milliseconds() << ".";
90 s << std::setfill('0') << std::setw(3) << (duration.total_microseconds() % 1000)
91 << " ms";
92 }
93 return (s.str());
94}
95
96ptime
98 return (microsec_clock::universal_time());
99}
100
101
102} // end of isc::util
103} // end of isc
void start()
Starts the stopwatch.
virtual ~StopwatchImpl()
Virtual destructor.
boost::posix_time::time_duration getTotalDuration() const
Retrieves the total measured duration.
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.
void reset()
Reset the stopwatch.
virtual boost::posix_time::ptime getCurrentTime() const
Returns the current time.
void stop()
Stop the stopwatch.
boost::posix_time::time_duration getLastDuration() const
Retrieves the measured duration.
Defines the logger used by the top-level component of kea-lfc.