Skip to content

Commit caff3d4

Browse files
authored
refs #14599 - do not implicitly print in TimerResults::stop() (#8400)
1 parent 830ef47 commit caff3d4

File tree

8 files changed

+33
-24
lines changed

8 files changed

+33
-24
lines changed

cli/cppcheckexecutor.cpp

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

270-
Timer realTimeClock("", settings.showtime, nullptr, Timer::Type::OVERALL);
270+
TimerResults overallTimerResults;
271+
Timer realTimeClock("Overall time", settings.showtime, &overallTimerResults, Timer::Type::OVERALL);
271272

272273
settings.loadSummaries();
273274

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

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

280+
realTimeClock.stop();
281+
overallTimerResults.showResults(settings.showtime, false, true);
282+
279283
return ret;
280284
}
281285

lib/cppcheck.cpp

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

926-
const Timer fileTotalTimer{file.spath(), mSettings.showtime, nullptr, Timer::Type::FILE};
926+
TimerResults checkTimeResults;
927+
Timer fileTotalTimer{"Check time: " + file.spath(), mSettings.showtime, &checkTimeResults, Timer::Type::FILE};
927928

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

1299+
fileTotalTimer.stop();
1300+
checkTimeResults.showResults(mSettings.showtime, false, true);
1301+
12981302
return mLogger->exitcode();
12991303
}
13001304

lib/timer.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "timer.h"
2020

2121
#include <algorithm>
22+
#include <cassert>
2223
#include <cstddef>
2324
#include <iostream>
2425
#include <utility>
@@ -37,9 +38,9 @@ namespace {
3738

3839
// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
3940
// that could also get rid of the broader locking
40-
void TimerResults::showResults(ShowTime mode) const
41+
void TimerResults::showResults(ShowTime mode, bool metrics, bool format) const
4142
{
42-
if (mode == ShowTime::NONE || mode == ShowTime::FILE_TOTAL)
43+
if (mode == ShowTime::NONE)
4344
return;
4445
std::vector<dataElementType> data;
4546

@@ -54,14 +55,19 @@ void TimerResults::showResults(ShowTime mode) const
5455
// lock the whole logging operation to avoid multiple threads printing their results at the same time
5556
std::lock_guard<std::mutex> l(stdCoutLock);
5657

57-
std::cout << std::endl;
58-
5958
size_t ordinal = 1; // maybe it would be nice to have an ordinal in output later!
6059
for (auto iter=data.cbegin(); iter!=data.cend(); ++iter) {
6160
const double sec = iter->second.getSeconds().count();
6261
const double secAverage = sec / static_cast<double>(iter->second.mNumberOfResults);
6362
if ((mode != ShowTime::TOP5_FILE && mode != ShowTime::TOP5_SUMMARY) || (ordinal<=5)) {
64-
std::cout << iter->first << ": " << sec << "s (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))" << std::endl;
63+
std::cout << iter->first << ": ";
64+
if (format)
65+
std::cout << TimerResultsData::durationToString(iter->second.mDuration);
66+
else
67+
std::cout << sec << "s";
68+
if (metrics)
69+
std::cout << " (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))";
70+
std::cout << std::endl;
6571
}
6672
++ordinal;
6773
}
@@ -107,12 +113,11 @@ void Timer::stop()
107113
return;
108114
}
109115
if (mStart != TimePoint{}) {
110-
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
111116
if (!mResults) {
112-
// TODO: do not print implicitly
113-
std::lock_guard<std::mutex> l(stdCoutLock);
114-
std::cout << (mType == Type::OVERALL ? "Overall time: " : "Check time: " + mName + ": ") << TimerResultsData::durationToString(diff) << std::endl;
115-
} else {
117+
assert(false);
118+
}
119+
else {
120+
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
116121
mResults->addResults(mName, diff);
117122
}
118123
}

lib/timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class CPPCHECKLIB WARN_UNUSED TimerResults : public TimerResultsIntf {
6060
public:
6161
TimerResults() = default;
6262

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

6666
void reset();

test/cli/other_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,17 +983,14 @@ def __test_showtime(tmp_path, showtime, exp_res, exp_last, extra_args=None):
983983
assert exitcode == 0
984984
lines = stdout.splitlines()
985985
exp_len = exp_res
986-
if exp_res:
987-
exp_len += 1 # empty line at the beginning - only added when individual results exist
988986
if 'cppcheck internal API usage' in stdout:
989987
exp_len += 1
990988
exp_len += 1 # last line
991989
assert len(lines) == exp_len
992-
if exp_res:
993-
assert lines[0] == ''
994990
for i in range(1, exp_res):
995991
assert 'avg.' in lines[i]
996992
assert lines[exp_len-1].startswith(exp_last)
993+
assert not 'avg.' in lines[exp_len-1]
997994
assert stderr == ''
998995

999996

test/testprocessexecutor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,9 @@ class TestProcessExecutorBase : public TestFixture {
247247
"int main() {}",
248248
dinit(CheckOptions,
249249
$.showtime = ShowTime::TOP5_FILE));
250-
// for each file: top5 results + overall + empty line
251250
const std::string output_s = GET_REDIRECT_OUTPUT;
252-
// for each file: top5 results + overall + empty line
253-
TODO_ASSERT_EQUALS(static_cast<long long>(5 + 1 + 1) * 2, 0, cppcheck::count_all_of(output_s, '\n'));
251+
// for each file: top5 results + check time
252+
TODO_ASSERT_EQUALS(static_cast<long long>(5 + 1) * 2, 0, cppcheck::count_all_of(output_s, '\n'));
254253
}
255254

256255
void showtime_file() {

test/testsingleexecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ class TestSingleExecutorBase : public TestFixture {
241241
dinit(CheckOptions,
242242
$.showtime = ShowTime::TOP5_FILE));
243243
const std::string output_s = GET_REDIRECT_OUTPUT;
244-
// for each file: top5 results + overall + total
245-
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
244+
// for each file: top5 results + check time
245+
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
246246
}
247247

248248
void showtime_file() {

test/testthreadexecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ class TestThreadExecutorBase : public TestFixture {
249249
dinit(CheckOptions,
250250
$.showtime = ShowTime::TOP5_FILE));
251251
const std::string output_s = GET_REDIRECT_OUTPUT;
252-
// for each file: top5 results + newline + overall
253-
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
252+
// for each file: top5 results + check time
253+
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
254254
}
255255

256256
void showtime_file() {

0 commit comments

Comments
 (0)