Skip to content

Commit 2986b72

Browse files
committed
refs #14599 - added OneShotTimer / moved some ShowTime logic out of Timer [skip ci]
1 parent 6073dd4 commit 2986b72

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
267267
return EXIT_SUCCESS;
268268
}
269269

270-
TimerResults overallTimerResults;
271-
Timer realTimeClock("Overall time", settings.showtime, &overallTimerResults, Timer::Type::OVERALL);
270+
std::unique_ptr<OneShotTimer> overallTimer;
271+
if (settings.showtime == ShowTime::SUMMARY || settings.showtime == ShowTime::TOP5_SUMMARY)
272+
overallTimer.reset(new OneShotTimer("Overall time", settings.showtime));
272273

273274
settings.loadSummaries();
274275

@@ -277,9 +278,6 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
277278

278279
const int ret = check_wrapper(settings, supprs);
279280

280-
realTimeClock.stop();
281-
overallTimerResults.showResults(settings.showtime, false, true);
282-
283281
return ret;
284282
}
285283

lib/cppcheck.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,9 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
923923
if (Settings::terminated())
924924
return mLogger->exitcode();
925925

926-
TimerResults checkTimeResults;
927-
Timer fileTotalTimer{"Check time: " + file.spath(), mSettings.showtime, &checkTimeResults, Timer::Type::FILE};
926+
std::unique_ptr<OneShotTimer> checkTimeTimer;
927+
if (mSettings.showtime == ShowTime::FILE || mSettings.showtime == ShowTime::FILE_TOTAL || mSettings.showtime == ShowTime::TOP5_FILE)
928+
checkTimeTimer.reset(new OneShotTimer("Check time: " + file.spath(), mSettings.showtime));
928929

929930
if (!mSettings.quiet) {
930931
std::string fixedpath = Path::toNativeSeparators(file.spath());
@@ -1296,9 +1297,6 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12961297
if (mTimerResults && (mSettings.showtime == ShowTime::FILE || mSettings.showtime == ShowTime::TOP5_FILE))
12971298
mTimerResults->showResults(mSettings.showtime);
12981299

1299-
fileTotalTimer.stop();
1300-
checkTimeResults.showResults(mSettings.showtime, false, true);
1301-
13021300
return mLogger->exitcode();
13031301
}
13041302

lib/timer.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace {
3838

3939
// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
4040
// that could also get rid of the broader locking
41-
void TimerResults::showResults(ShowTime mode, bool metrics, bool format) const
41+
void TimerResults::showResults(ShowTime mode, bool metrics) const
4242
{
4343
if (mode == ShowTime::NONE)
4444
return;
@@ -60,11 +60,7 @@ void TimerResults::showResults(ShowTime mode, bool metrics, bool format) const
6060
const double sec = iter->second.getSeconds().count();
6161
const double secAverage = sec / static_cast<double>(iter->second.mNumberOfResults);
6262
if ((mode != ShowTime::TOP5_FILE && mode != ShowTime::TOP5_SUMMARY) || (ordinal<=5)) {
63-
std::cout << iter->first << ": ";
64-
if (format)
65-
std::cout << TimerResultsData::durationToString(iter->second.mDuration);
66-
else
67-
std::cout << sec << "s";
63+
std::cout << iter->first << ": " << sec << "s";
6864
if (metrics)
6965
std::cout << " (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))";
7066
std::cout << std::endl;
@@ -87,10 +83,9 @@ void TimerResults::reset()
8783
mResults.clear();
8884
}
8985

90-
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, Type type)
86+
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults)
9187
: mName(std::move(str))
9288
, mMode(showtimeMode)
93-
, mType(type)
9489
, mStart(Clock::now())
9590
, mResults(timerResults)
9691
{}
@@ -104,14 +99,6 @@ void Timer::stop()
10499
{
105100
if (mMode == ShowTime::NONE)
106101
return;
107-
if (mType == Type::OVERALL && mMode != ShowTime::TOP5_SUMMARY && mMode != ShowTime::SUMMARY) {
108-
mMode = ShowTime::NONE;
109-
return;
110-
}
111-
if (mType == Type::FILE && mMode != ShowTime::TOP5_FILE && mMode != ShowTime::FILE && mMode != ShowTime::FILE_TOTAL) {
112-
mMode = ShowTime::NONE;
113-
return;
114-
}
115102
if (mStart != TimePoint{}) {
116103
if (!mResults) {
117104
assert(false);
@@ -124,7 +111,7 @@ void Timer::stop()
124111
mMode = ShowTime::NONE; // prevent multiple stops
125112
}
126113

127-
std::string TimerResultsData::durationToString(std::chrono::milliseconds duration)
114+
static std::string durationToString(std::chrono::milliseconds duration)
128115
{
129116
// Extract hours
130117
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
@@ -148,3 +135,22 @@ std::string TimerResultsData::durationToString(std::chrono::milliseconds duratio
148135
secondsStr.resize(pos + 4); // keep three decimal
149136
return (ellapsedTime + secondsStr + "s");
150137
}
138+
139+
OneShotTimer::OneShotTimer(std::string name, ShowTime showtime)
140+
{
141+
if (showtime == ShowTime::NONE)
142+
return;
143+
144+
class MyResults : public TimerResultsIntf
145+
{
146+
private:
147+
void addResults(const std::string &name, std::chrono::milliseconds duration) override
148+
{
149+
// TODO: do not directly use std::cout
150+
std::cout << name << ": " << durationToString(duration) << std::endl;
151+
}
152+
};
153+
154+
mResults.reset(new MyResults);
155+
mTimer.reset(new Timer(std::move(name), showtime, mResults.get()));
156+
}

lib/timer.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <chrono>
2626
#include <cstdint>
2727
#include <map>
28+
#include <memory>
2829
#include <mutex>
2930
#include <string>
3031
#include <utility>
@@ -52,15 +53,13 @@ struct TimerResultsData {
5253
std::chrono::duration<double> getSeconds() const {
5354
return std::chrono::duration_cast<std::chrono::duration<double>>(mDuration);
5455
}
55-
56-
static std::string durationToString(std::chrono::milliseconds duration);
5756
};
5857

5958
class CPPCHECKLIB WARN_UNUSED TimerResults : public TimerResultsIntf {
6059
public:
6160
TimerResults() = default;
6261

63-
void showResults(ShowTime mode, bool metrics = true, bool format = false) const;
62+
void showResults(ShowTime mode, bool metrics = true) const;
6463
void addResults(const std::string& str, std::chrono::milliseconds duration) override;
6564

6665
void reset();
@@ -75,13 +74,7 @@ class CPPCHECKLIB Timer {
7574
using Clock = std::chrono::high_resolution_clock;
7675
using TimePoint = std::chrono::time_point<Clock>;
7776

78-
enum class Type : std::uint8_t {
79-
FILE,
80-
OVERALL,
81-
OTHER
82-
};
83-
84-
Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults = nullptr, Type type = Type::OTHER);
77+
Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults = nullptr);
8578
~Timer();
8679

8780
Timer(const Timer&) = delete;
@@ -98,10 +91,18 @@ class CPPCHECKLIB Timer {
9891
private:
9992
const std::string mName;
10093
ShowTime mMode{};
101-
Type mType{};
10294
TimePoint mStart;
10395
TimerResultsIntf* mResults{};
10496
};
10597

98+
class CPPCHECKLIB OneShotTimer
99+
{
100+
public:
101+
OneShotTimer(std::string name, ShowTime showtime);
102+
private:
103+
std::unique_ptr<TimerResultsIntf> mResults;
104+
std::unique_ptr<Timer> mTimer;
105+
};
106+
106107
//---------------------------------------------------------------------------
107108
#endif // timerH

0 commit comments

Comments
 (0)