Skip to content

Commit bf42abd

Browse files
authored
testrunner: added option -t to print timing information about the tests (danmar#8336)
1 parent 6bbfee2 commit bf42abd

File tree

9 files changed

+63
-7
lines changed

9 files changed

+63
-7
lines changed

.github/workflows/CI-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ jobs:
219219

220220
- name: Run Debug test
221221
if: matrix.config == 'debug'
222-
run: .\bin\debug\testrunner.exe || exit /b !errorlevel!
222+
run: .\bin\debug\testrunner.exe -t || exit /b !errorlevel!
223223

224224
- name: Build CLI release configuration using MSBuild
225225
if: matrix.config == 'release'

.github/workflows/sanitizers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114
cmake --build cmake.output --target gui-tests -- -j $(nproc)
115115
116116
- name: Run tests
117-
run: ./cmake.output/bin/testrunner
117+
run: ./cmake.output/bin/testrunner -t
118118

119119
- name: Run cfg tests
120120
run: |

.github/workflows/valgrind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
TestProcessExecutorFiles \
5858
TestSuppressions::suppressionsSettingsProcessesFiles \
5959
TestSuppressions::suppressionsSettingsProcessesFS"
60-
valgrind --error-limit=yes --leak-check=full --num-callers=50 --show-reachable=yes --track-origins=yes --suppressions=valgrind/testrunner.supp --gen-suppressions=all -s --log-fd=9 --error-exitcode=42 ./testrunner -x $excluded_tests 9>memcheck.log || ec=1
60+
valgrind --error-limit=yes --leak-check=full --num-callers=50 --show-reachable=yes --track-origins=yes --suppressions=valgrind/testrunner.supp --gen-suppressions=all -s --log-fd=9 --error-exitcode=42 ./testrunner -t -x $excluded_tests 9>memcheck.log || ec=1
6161
cat memcheck.log
6262
exit $ec
6363
# TODO: debuginfod.ubuntu.com is currently not responding to any requests causing it to run into a 40(!) minute timeout

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/regex.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/regex.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/regex.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/regex.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/regex.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/regex.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: 19 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>
@@ -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
{
@@ -106,19 +108,25 @@ bool TestFixture::prepareTest(const char testname[])
106108
// Tests will be executed - prepare them
107109
mTestname = testname;
108110
++countTests;
111+
std::string fullTestName = classname + "::" + mTestname;
109112
if (quiet_tests) {
110113
std::putchar('.'); // Use putchar to write through redirection of std::cout/cerr
111114
std::fflush(stdout);
112115
} else {
113-
std::cout << classname << "::" << mTestname << std::endl;
116+
std::cout << fullTestName << std::endl;
114117
}
118+
if (timer_results)
119+
mTimer.reset(new Timer(fullTestName, ShowTime::TOP5_SUMMARY, timer_results));
115120
return !dry_run;
116121
}
117122

118123
void TestFixture::teardownTest()
119124
{
120125
teardownTestInternal();
121126

127+
if (mTimer)
128+
mTimer->stop();
129+
122130
{
123131
const std::string s = errout_str();
124132
if (!s.empty())
@@ -385,6 +393,7 @@ void TestFixture::processOptions(const options& args)
385393
dry_run = args.dry_run();
386394
exclude_tests = args.exclude_tests();
387395
exename = args.exe();
396+
timer_results = args.timer_results();
388397
}
389398

390399
std::size_t TestFixture::runTests(const options& args)
@@ -410,7 +419,15 @@ std::size_t TestFixture::runTests(const options& args)
410419
tests = it->second;
411420
}
412421

413-
TestFixture* fixture = test->create();
422+
TestFixture* fixture;
423+
const auto f = [&](){
424+
fixture = test->create();
425+
};
426+
// TODO: Timer::run() needs proper handling if no results should be collected
427+
if (args.timer_results())
428+
Timer::run(test->classname + " - create", ShowTime::TOP5_SUMMARY, args.timer_results(), f);
429+
else
430+
f();
414431
fixture->processOptions(args);
415432
fixture->run(tests);
416433
}

test/fixture.h

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

4444
class options;
4545
class Tokenizer;
46+
class Timer;
47+
class TimerResultsIntf;
4648

4749
class TestFixture : public ErrorLogger {
4850
private:
@@ -61,6 +63,7 @@ class TestFixture : public ErrorLogger {
6163
bool quiet_tests{};
6264
bool dry_run{};
6365
bool exclude_tests{};
66+
TimerResultsIntf* timer_results{};
6467
bool mNewTemplate{};
6568

6669
virtual void run() = 0;
@@ -286,6 +289,8 @@ class TestFixture : public ErrorLogger {
286289
std::ostringstream mOutput;
287290
std::ostringstream mErrout;
288291

292+
std::unique_ptr<Timer> mTimer;
293+
289294
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
290295
void reportErr(const ErrorMessage &msg) override;
291296
void reportMetric(const std::string &metric) override
@@ -299,6 +304,7 @@ class TestFixture : public ErrorLogger {
299304
const std::string classname;
300305

301306
explicit TestFixture(const char * _name);
307+
~TestFixture() override;
302308

303309
static std::size_t runTests(const options& args);
304310
};

test/options.cpp

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

1717
#include "options.h"
1818

19+
#include "timer.h"
20+
1921
options::options(int argc, const char* const argv[])
2022
: mExe(argv[0])
2123
{
@@ -34,6 +36,8 @@ options::options(int argc, const char* const argv[])
3436
mDryRun = true;
3537
else if (arg == "-x")
3638
mExcludeTests = true;
39+
else if (arg == "-t")
40+
mTimerResults.reset(new TimerResults);
3741
else
3842
mErrors.emplace_back("unknown option '" + arg + "'");
3943
continue; // command-line switch
@@ -52,6 +56,15 @@ options::options(int argc, const char* const argv[])
5256
}
5357
}
5458

59+
options::~options()
60+
{
61+
// TODO: allow more than 5 results to be shown
62+
// TODO: provide higher resolution in output
63+
// TODO: disable the metrics
64+
if (mTimerResults)
65+
mTimerResults->showResults(ShowTime::TOP5_FILE);
66+
}
67+
5568
bool options::quiet() const
5669
{
5770
return mQuiet;
@@ -87,6 +100,11 @@ bool options::exclude_tests() const
87100
return mExcludeTests;
88101
}
89102

103+
TimerResultsIntf* options::timer_results() const
104+
{
105+
return mTimerResults.get();
106+
}
107+
90108
const std::vector<std::string>& options::errors() const
91109
{
92110
return mErrors;

test/options.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
#define OPTIONS_H
1919

2020
#include <map>
21+
#include <memory>
2122
#include <set>
2223
#include <string>
2324
#include <vector>
2425

26+
class TimerResultsIntf;
27+
class TimerResults;
28+
2529
/**
2630
* @brief Class to parse command-line parameters for ./testrunner .
2731
* Has getters for available switches and parameters.
@@ -31,6 +35,7 @@ class options {
3135
public:
3236
/** Call from main() to populate object */
3337
options(int argc, const char* const argv[]);
38+
~options();
3439
/** Don't print the name of each method being tested. */
3540
bool quiet() const;
3641
/** Print help. */
@@ -41,6 +46,8 @@ class options {
4146
bool dry_run() const;
4247
/** Exclude provided lists of tests. */
4348
bool exclude_tests() const;
49+
/** The timer results. */
50+
TimerResultsIntf* timer_results() const;
4451
/** Which tests should be run. */
4552
const std::map<std::string, std::set<std::string>>& which_tests() const;
4653

@@ -61,6 +68,7 @@ class options {
6168
bool mSummary{true};
6269
bool mDryRun{};
6370
bool mExcludeTests{};
71+
std::unique_ptr<TimerResults> mTimerResults;
6472
std::string mExe;
6573
};
6674

test/testoptions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestOptions : public TestFixture {
4545
TEST_CASE(summary);
4646
TEST_CASE(dry_run);
4747
TEST_CASE(exclude_tests);
48+
TEST_CASE(timer_results);
4849
}
4950

5051

@@ -167,6 +168,12 @@ class TestOptions : public TestFixture {
167168
ASSERT_EQUALS(true, args.exclude_tests());
168169
ASSERT(args.errors().empty());
169170
}
171+
172+
void timer_results() const {
173+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-t"};
174+
options args(getArrayLength(argv), argv);
175+
ASSERT(!!args.timer_results());
176+
}
170177
};
171178

172179
REGISTER_TEST(TestOptions)

0 commit comments

Comments
 (0)