| Kea 3.0.2-git

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
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
18 StopwatchImpl::StopwatchImpl()
19 : started_(false),
20 last_start_(getCurrentTime()),
21 last_stop_(last_start_),
22 cumulative_time_(microseconds(0)) {
23}
24
25 StopwatchImpl::~StopwatchImpl() {
26}
27
28 void
29 StopwatchImpl::start() {
30 // If stopwatch is "stopped", start it.
31 if (!started_) {
32 last_start_ = getCurrentTime();
33 started_ = true;
34 }
35}
36
37 void
38 StopwatchImpl::stop() {
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
49 StopwatchImpl::reset() {
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
61 StopwatchImpl::getLastDuration() const {
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
70 StopwatchImpl::getTotalDuration() const {
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
82 StopwatchImpl::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
97 StopwatchImpl::getCurrentTime() const {
98 return (microsec_clock::universal_time());
99}
100
101
102} // end of isc::util
103} // end of isc
isc::util::StopwatchImpl::start
void start()
Starts the stopwatch.
Definition stopwatch_impl.cc:29
isc::util::StopwatchImpl::~StopwatchImpl
virtual ~StopwatchImpl()
Virtual destructor.
Definition stopwatch_impl.cc:25
isc::util::StopwatchImpl::getTotalDuration
boost::posix_time::time_duration getTotalDuration() const
Retrieves the total measured duration.
Definition stopwatch_impl.cc:70
isc::util::StopwatchImpl::logFormat
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.
Definition stopwatch_impl.cc:82
isc::util::StopwatchImpl::StopwatchImpl
StopwatchImpl()
Constructor.
Definition stopwatch_impl.cc:18
isc::util::StopwatchImpl::reset
void reset()
Reset the stopwatch.
Definition stopwatch_impl.cc:49
isc::util::StopwatchImpl::getCurrentTime
virtual boost::posix_time::ptime getCurrentTime() const
Returns the current time.
Definition stopwatch_impl.cc:97
isc::util::StopwatchImpl::stop
void stop()
Stop the stopwatch.
Definition stopwatch_impl.cc:38
isc::util::StopwatchImpl::getLastDuration
boost::posix_time::time_duration getLastDuration() const
Retrieves the measured duration.
Definition stopwatch_impl.cc:61
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
stopwatch_impl.h