Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually track total class test times, not the time a thread is alive #2084

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions test/lib/tpunit++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ int tpunit::_TestFixture::tpunit_detail_do_run(const set<string>& include, const
// Capture everything by reference except threadID, because we don't want it to be incremented for the
// next thread in the loop.
thread t = thread([&, threadID]{
auto start = chrono::steady_clock::now();
chrono::steady_clock::time_point start = chrono::steady_clock::now();
chrono::steady_clock::time_point end = start;

threadInitFunction();
try {
Expand Down Expand Up @@ -254,22 +255,32 @@ int tpunit::_TestFixture::tpunit_detail_do_run(const set<string>& include, const
continue;
}

// At this point, we know this test should run.
if (!f->_multiThreaded) {
printf("--------------\n");
}
{
lock_guard<mutex> lock(currentTestNameMutex);
currentTestPtr = f;
if (f->_name) {
currentTestName = f->_name;
} else {
cout << "test has no name???" << endl;
currentTestName = "UNSPECIFIED";
}
}

tpunit_run_test_class(f);
// At this point, we know this test should run.
if (!f->_multiThreaded) {
printf("--------------\n");
}
{
lock_guard<mutex> lock(currentTestNameMutex);
currentTestPtr = f;
if (f->_name) {
currentTestName = f->_name;
} else {
cout << "test has no name???" << endl;
currentTestName = "UNSPECIFIED";
}
}

start = chrono::steady_clock::now();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hide whitespace since I fixed it off by one


tpunit_run_test_class(f);

// Do this to capture the longest test classes, not longest thread.
end = chrono::steady_clock::now();

if (currentTestName.size() && currentTestName != "UNSPECIFIED") {
lock_guard<mutex> lock(testTimeLock);
testTimes.emplace(make_pair(chrono::duration_cast<std::chrono::milliseconds>(end - start), currentTestName));
}
}
} catch (ShutdownException se) {
// This will have broken us out of our main loop, so we'll just exit. We also set the exit flag to let
Expand All @@ -278,11 +289,6 @@ int tpunit::_TestFixture::tpunit_detail_do_run(const set<string>& include, const
exitFlag = true;
printf("Thread %d caught shutdown exception, exiting.\n", threadID);
}
auto end = chrono::steady_clock::now();
if (currentTestName.size()) {
lock_guard<mutex> lock(testTimeLock);
testTimes.emplace(make_pair(chrono::duration_cast<std::chrono::milliseconds>(end - start), currentTestName));
}
});
threadList.push_back(move(t));
}
Expand Down Expand Up @@ -321,15 +327,24 @@ int tpunit::_TestFixture::tpunit_detail_do_run(const set<string>& include, const

cout << endl;
cout << "Slowest Test Classes: " << endl;

// Combine total thread time so its not obscured by multi-threaded tests.
long long totalTestTime = 0;
for (auto testTime : testTimes) {
totalTestTime += testTime.first.count();
}

auto it = testTimes.rbegin();
for (size_t i = 0; i < 10; i++) {
if (it == testTimes.rend()) {
break;
}
cout << it->first << ": " << it->second << endl;
cout << it->first << ": " << it->second << " : " << (static_cast<double>(it->first.count()) / totalTestTime) * 100.0 << "% of total test time" << endl;
it++;
}

cout << "Total test time across threads: " << totalTestTime << "ms" << endl;

return tpunit_detail_stats()._failures;
}
return 1;
Expand Down