Skip to content

Commit 89c39ec

Browse files
committed
testrunner: added option -t to print timing information about the tests [skip ci]
1 parent c331d49 commit 89c39ec

5 files changed

Lines changed: 43 additions & 3 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
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
@@ -731,7 +731,7 @@ test/helpers.o: test/helpers.cpp cli/filelister.h externals/simplecpp/simplecpp.
731731
test/main.o: test/main.cpp externals/simplecpp/simplecpp.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/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/utils.h test/fixture.h test/options.h
732732
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/main.cpp
733733

734-
test/options.o: test/options.cpp test/options.h
734+
test/options.o: test/options.cpp lib/config.h lib/timer.h test/options.h
735735
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/options.cpp
736736

737737
test/test64bit.o: test/test64bit.cpp lib/addoninfo.h lib/check.h lib/check64bit.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 test/fixture.h test/helpers.h

test/fixture.cpp

Lines changed: 10 additions & 1 deletion
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>
@@ -88,6 +89,7 @@ TestFixture::TestFixture(const char * const _name)
8889
: classname(_name)
8990
{}
9091

92+
TestFixture::~TestFixture() = default;
9193

9294
bool TestFixture::prepareTest(const char testname[])
9395
{
@@ -101,12 +103,14 @@ bool TestFixture::prepareTest(const char testname[])
101103
// Tests will be executed - prepare them
102104
mTestname = testname;
103105
++countTests;
106+
std::string fullTestName = classname + "::" + mTestname;
104107
if (quiet_tests) {
105108
std::putchar('.'); // Use putchar to write through redirection of std::cout/cerr
106109
std::fflush(stdout);
107110
} else {
108-
std::cout << classname << "::" << mTestname << std::endl;
111+
std::cout << fullTestName << std::endl;
109112
}
113+
mTimer.reset(new Timer(fullTestName, ShowTime::TOP5_SUMMARY, timerResults));
110114
return !dry_run;
111115
}
112116
return false;
@@ -116,6 +120,10 @@ void TestFixture::teardownTest()
116120
{
117121
teardownTestInternal();
118122

123+
// TODO: print more detailed data
124+
if (mTimer)
125+
mTimer->stop();
126+
119127
{
120128
const std::string s = errout_str();
121129
if (!s.empty())
@@ -381,6 +389,7 @@ void TestFixture::processOptions(const options& args)
381389
quiet_tests = args.quiet();
382390
dry_run = args.dry_run();
383391
exename = args.exe();
392+
timerResults = args.timer_results();
384393
}
385394

386395
std::size_t TestFixture::runTests(const options& args)

test/fixture.h

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

4343
class options;
4444
class Tokenizer;
45+
class Timer;
46+
class TimerResultsIntf;
4547

4648
class TestFixture : public ErrorLogger {
4749
private:
@@ -60,6 +62,7 @@ class TestFixture : public ErrorLogger {
6062
bool quiet_tests{};
6163
bool dry_run{};
6264
bool mNewTemplate{};
65+
TimerResultsIntf* timerResults{};
6366

6467
virtual void run() = 0;
6568

@@ -284,6 +287,8 @@ class TestFixture : public ErrorLogger {
284287
std::ostringstream mOutput;
285288
std::ostringstream mErrout;
286289

290+
std::unique_ptr<Timer> mTimer;
291+
287292
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
288293
void reportErr(const ErrorMessage &msg) override;
289294
void reportMetric(const std::string &metric) override
@@ -297,6 +302,7 @@ class TestFixture : public ErrorLogger {
297302
const std::string classname;
298303

299304
explicit TestFixture(const char * _name);
305+
~TestFixture() override;
300306

301307
static std::size_t runTests(const options& args);
302308
};

test/options.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include "options.h"
1818

19+
#include "timer.h"
20+
21+
// TODO: bailout on unknown arguments
1922
options::options(int argc, const char* const argv[])
2023
: mWhichTests(argv + 1, argv + argc)
2124
,mQuiet(mWhichTests.count("-q") != 0)
@@ -25,6 +28,9 @@ options::options(int argc, const char* const argv[])
2528
,mExcludeTests(mWhichTests.count("-x") != 0)
2629
,mExe(argv[0])
2730
{
31+
if (mArgs.count("-t") != 0)
32+
mTimerResults.reset(new TimerResults);
33+
2834
for (auto it = mWhichTests.cbegin(); it != mWhichTests.cend();) {
2935
if (!it->empty() && (((*it)[0] == '-') || (it->find("::") != std::string::npos && mWhichTests.count(it->substr(0, it->find("::"))))))
3036
it = mWhichTests.erase(it);
@@ -37,6 +43,12 @@ options::options(int argc, const char* const argv[])
3743
}
3844
}
3945

46+
options::~options()
47+
{
48+
if (mTimerResults)
49+
mTimerResults->showResults(ShowTime::TOP5_FILE);
50+
}
51+
4052
bool options::quiet() const
4153
{
4254
return mQuiet;
@@ -71,3 +83,8 @@ bool options::exclude_tests() const
7183
{
7284
return mExcludeTests;
7385
}
86+
87+
TimerResultsIntf* options::timer_results() const
88+
{
89+
return mTimerResults.get();
90+
}

test/options.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
#ifndef OPTIONS_H
1818
#define OPTIONS_H
1919

20+
#include <memory>
2021
#include <set>
2122
#include <string>
2223

24+
class TimerResultsIntf;
25+
class TimerResults;
26+
2327
/**
2428
* @brief Class to parse command-line parameters for ./testrunner .
2529
* Has getters for available switches and parameters.
@@ -29,6 +33,7 @@ class options {
2933
public:
3034
/** Call from main() to populate object */
3135
options(int argc, const char* const argv[]);
36+
~options();
3237
/** Don't print the name of each method being tested. */
3338
bool quiet() const;
3439
/** Print help. */
@@ -39,6 +44,8 @@ class options {
3944
bool dry_run() const;
4045
/** Exclude provided lists of tests. */
4146
bool exclude_tests() const;
47+
/** The timer results. */
48+
TimerResultsIntf* timer_results() const;
4249
/** Which test should be run. Empty string means 'all tests' */
4350
const std::set<std::string>& which_test() const;
4451

@@ -55,6 +62,7 @@ class options {
5562
const bool mSummary;
5663
const bool mDryRun;
5764
const bool mExcludeTests;
65+
std::unique_ptr<TimerResults> mTimerResults;
5866
std::string mExe;
5967
};
6068

0 commit comments

Comments
 (0)