Skip to content

Commit 65384e7

Browse files
committed
testrunner: added option -t to print timing information about the tests
1 parent 182815a commit 65384e7

7 files changed

Lines changed: 56 additions & 5 deletions

File tree

.github/workflows/valgrind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
- name: Run valgrind
5252
run: |
5353
ec=0
54-
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 TestGarbage TestOther TestSimplifyTemplate TestRegEx 9>memcheck.log || ec=1
54+
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 TestGarbage TestOther TestSimplifyTemplate TestRegEx 9>memcheck.log || ec=1
5555
cat memcheck.log
5656
exit $ec
5757
# 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/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: 13 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,24 @@ 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+
mTimer.reset(new Timer(fullTestName, ShowTime::TOP5_SUMMARY, timerResults));
115119
return !dry_run;
116120
}
117121

118122
void TestFixture::teardownTest()
119123
{
120124
teardownTestInternal();
121125

126+
if (mTimer)
127+
mTimer->stop();
128+
122129
{
123130
const std::string s = errout_str();
124131
if (!s.empty())
@@ -385,6 +392,7 @@ void TestFixture::processOptions(const options& args)
385392
dry_run = args.dry_run();
386393
exclude_tests = args.exclude_tests();
387394
exename = args.exe();
395+
timerResults = args.timer_results();
388396
}
389397

390398
std::size_t TestFixture::runTests(const options& args)
@@ -410,7 +418,10 @@ std::size_t TestFixture::runTests(const options& args)
410418
tests = it->second;
411419
}
412420

413-
TestFixture* fixture = test->create();
421+
TestFixture* fixture;
422+
Timer::run(test->classname + " - create", ShowTime::TOP5_SUMMARY, args.timer_results(), [&](){
423+
fixture = test->create();
424+
});
414425
fixture->processOptions(args);
415426
fixture->run(tests);
416427
}

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:
@@ -62,6 +64,7 @@ class TestFixture : public ErrorLogger {
6264
bool dry_run{};
6365
bool exclude_tests{};
6466
bool mNewTemplate{};
67+
TimerResultsIntf* timerResults{};
6568

6669
virtual void run() = 0;
6770

@@ -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: 19 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
: mExe(argv[0])
2124
{
@@ -34,6 +37,8 @@ options::options(int argc, const char* const argv[])
3437
mDryRun = true;
3538
else if (arg == "-x")
3639
mExcludeTests = true;
40+
else if (arg == "-t")
41+
mTimerResults.reset(new TimerResults);
3742
else
3843
mErrors.emplace_back("unknown option '" + arg + "'");
3944
continue; // command-line switch
@@ -52,6 +57,15 @@ options::options(int argc, const char* const argv[])
5257
}
5358
}
5459

60+
options::~options()
61+
{
62+
// TODO: allow more than 5 results to be shown
63+
// TODO: provide higher resolution in output
64+
// TODO: disable the metrics
65+
if (mTimerResults)
66+
mTimerResults->showResults(ShowTime::TOP5_FILE);
67+
}
68+
5569
bool options::quiet() const
5670
{
5771
return mQuiet;
@@ -87,6 +101,11 @@ bool options::exclude_tests() const
87101
return mExcludeTests;
88102
}
89103

104+
TimerResultsIntf* options::timer_results() const
105+
{
106+
return mTimerResults.get();
107+
}
108+
90109
const std::vector<std::string>& options::errors() const
91110
{
92111
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)