Coverage Report

Created: 2026-09-17 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea/src/lib/util/stopwatch_impl.cc
Line
Count
Source
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
95
    : started_(false),
20
95
      last_start_(getCurrentTime()),
21
95
      last_stop_(last_start_),
22
95
      cumulative_time_(microseconds(0)) {
23
95
}
24
25
95
StopwatchImpl::~StopwatchImpl() {
26
95
}
27
28
void
29
95
StopwatchImpl::start() {
30
    // If stopwatch is "stopped", start it.
31
95
    if (!started_) {
32
95
        last_start_ = getCurrentTime();
33
95
        started_ = true;
34
95
    }
35
95
}
36
37
void
38
95
StopwatchImpl::stop() {
39
    // Is stopwatch is "started", stop it.
40
95
    if (started_) {
41
95
        last_stop_ = getCurrentTime();
42
        // Update the total time with the last measured duration.
43
95
        cumulative_time_ += last_stop_ - last_start_;
44
95
        started_ = false;
45
95
    }
46
95
}
47
48
void
49
0
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
0
    last_start_ = getCurrentTime();
54
0
    last_stop_ = last_start_;
55
    // Set the total duration to 0.
56
0
    cumulative_time_ = microseconds(0);
57
0
    started_ = false;
58
0
}
59
60
time_duration
61
0
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
0
    ptime end_time = started_ ? getCurrentTime() : last_stop_;
66
0
    return (end_time - last_start_);
67
0
}
68
69
time_duration
70
0
StopwatchImpl::getTotalDuration() const {
71
    // Get the total time recorded so far.
72
0
    time_duration total_duration = cumulative_time_;
73
0
    if (started_) {
74
        // If the stopwatch is started, add the duration between the
75
        // start time and current time.
76
0
        total_duration += (getCurrentTime() - last_start_);
77
0
    }
78
0
    return (total_duration);
79
0
}
80
81
std::string
82
0
StopwatchImpl::logFormat(const boost::posix_time::time_duration& duration) {
83
0
    std::ostringstream s;
84
0
    if (duration.total_seconds() > 0) {
85
0
        s << duration.total_seconds() << "."
86
0
          << std::setfill('0') << std::setw(2) << (duration.total_milliseconds()/10 % 100)
87
0
          << " s";
88
0
    } else {
89
0
        s << duration.total_milliseconds() << ".";
90
0
        s << std::setfill('0') << std::setw(3) << (duration.total_microseconds() % 1000)
91
0
          << " ms";
92
0
    }
93
0
    return (s.str());
94
0
}
95
96
ptime
97
285
StopwatchImpl::getCurrentTime() const {
98
285
    return (microsec_clock::universal_time());
99
285
}
100
101
102
} // end of isc::util
103
} // end of isc