Skip to content

Commit ba841ac

Browse files
committed
timer [skip ci]
1 parent 7d80f64 commit ba841ac

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ cli/stacktrace.o: cli/stacktrace.cpp cli/stacktrace.h lib/config.h lib/utils.h
722722
cli/threadexecutor.o: cli/threadexecutor.cpp cli/executor.h cli/threadexecutor.h lib/addoninfo.h lib/check.h lib/checkers.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
723723
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/threadexecutor.cpp
724724

725-
test/fixture.o: test/fixture.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h test/options.h test/redirect.h
725+
test/fixture.o: test/fixture.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h test/options.h test/redirect.h
726726
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp
727727

728728
test/helpers.o: test/helpers.cpp cli/filelister.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/checkers.h lib/config.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/helpers.h

lib/timer.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,36 @@ Timer::~Timer()
9494
stop();
9595
}
9696

97-
void Timer::stop()
97+
void Timer::restart()
98+
{
99+
mStart = Clock::now();
100+
}
101+
102+
double Timer::stop()
98103
{
99104
if (mMode == ShowTime::NONE)
100-
return;
105+
return -1.0;
101106
if (mType == Type::OVERALL && mMode != ShowTime::TOP5_SUMMARY && mMode != ShowTime::SUMMARY) {
102107
mMode = ShowTime::NONE;
103-
return;
108+
return -1.0;
104109
}
105110
if (mType == Type::FILE && mMode != ShowTime::TOP5_FILE && mMode != ShowTime::FILE && mMode != ShowTime::FILE_TOTAL) {
106111
mMode = ShowTime::NONE;
107-
return;
112+
return -1.0;
108113
}
109114
if (mStart != TimePoint{}) {
110115
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
111116
if (!mResults) {
117+
// TODO: do not print implicitly
112118
std::lock_guard<std::mutex> l(stdCoutLock);
113119
std::cout << (mType == Type::OVERALL ? "Overall time: " : "Check time: " + mName + ": ") << TimerResultsData::durationToString(diff) << std::endl;
114120
} else {
115121
mResults->addResults(mName, diff);
116122
}
123+
return diff.count();
117124
}
118125
mMode = ShowTime::NONE; // prevent multiple stops
126+
return -1.0;
119127
}
120128

121129
std::string TimerResultsData::durationToString(std::chrono::milliseconds duration)

lib/timer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class CPPCHECKLIB Timer {
8787
Timer(const Timer&) = delete;
8888
Timer& operator=(const Timer&) = delete;
8989

90-
void stop();
90+
void restart();
91+
double stop();
9192

9293
template<class TFunc>
9394
static void run(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, const TFunc& f) {

test/fixture.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "library.h"
2424
#include "options.h"
2525
#include "redirect.h"
26+
#include "timer.h"
2627

2728
#include <algorithm>
2829
#include <cstdio>
@@ -84,8 +85,11 @@ std::size_t TestFixture::fails_counter = 0;
8485
std::size_t TestFixture::todos_counter = 0;
8586
std::size_t TestFixture::succeeded_todos_counter = 0;
8687

88+
static TimerResults timerResults;
89+
8790
TestFixture::TestFixture(const char * const _name)
88-
: classname(_name)
91+
: mTimer(new Timer("", ShowTime::SUMMARY, &timerResults))
92+
, classname(_name)
8993
{}
9094

9195

@@ -105,7 +109,8 @@ bool TestFixture::prepareTest(const char testname[])
105109
std::putchar('.'); // Use putchar to write through redirection of std::cout/cerr
106110
std::fflush(stdout);
107111
} else {
108-
std::cout << classname << "::" << mTestname << std::endl;
112+
std::cout << classname << "::" << mTestname /*<< std::endl*/;
113+
mTimer->restart();
109114
}
110115
return !dry_run;
111116
}
@@ -114,6 +119,9 @@ bool TestFixture::prepareTest(const char testname[])
114119

115120
void TestFixture::teardownTest()
116121
{
122+
if (mTimer)
123+
std::cout << " - " << mTimer->stop() << "ms" << std::endl;
124+
117125
teardownTestInternal();
118126

119127
{

test/fixture.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
class options;
4444
class Tokenizer;
45+
class Timer;
4546

4647
class TestFixture : public ErrorLogger {
4748
private:
@@ -284,6 +285,8 @@ class TestFixture : public ErrorLogger {
284285
std::ostringstream mOutput;
285286
std::ostringstream mErrout;
286287

288+
std::unique_ptr<Timer> mTimer;
289+
287290
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
288291
void reportErr(const ErrorMessage &msg) override;
289292
void reportMetric(const std::string &metric) override

test/options.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ options::options(int argc, const char* const argv[])
2323
,mSummary(mWhichTests.count("-n") == 0)
2424
,mDryRun(mWhichTests.count("-d") != 0)
2525
,mExcludeTests(mWhichTests.count("-x") != 0)
26+
,mShowTime(mWhichTests.count("-t") != 0)
2627
,mExe(argv[0])
2728
{
2829
for (auto it = mWhichTests.cbegin(); it != mWhichTests.cend();) {
@@ -71,3 +72,8 @@ bool options::exclude_tests() const
7172
{
7273
return mExcludeTests;
7374
}
75+
76+
bool options::show_time() const
77+
{
78+
return mShowTime;
79+
}

test/options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class options {
3939
bool dry_run() const;
4040
/** Exclude provided lists of tests. */
4141
bool exclude_tests() const;
42+
/** Print the runtime of the tests. */
43+
bool show_time() const;
4244
/** Which test should be run. Empty string means 'all tests' */
4345
const std::set<std::string>& which_test() const;
4446

@@ -55,6 +57,7 @@ class options {
5557
const bool mSummary;
5658
const bool mDryRun;
5759
const bool mExcludeTests;
60+
const bool mShowTime;
5861
std::string mExe;
5962
};
6063

0 commit comments

Comments
 (0)