Skip to content

Commit 63f4b30

Browse files
committed
add total to statistics
1 parent 69d7436 commit 63f4b30

File tree

2 files changed

+100
-50
lines changed

2 files changed

+100
-50
lines changed

server/src/printers/CoverageAndResultsStatisticsPrinter.cpp

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,101 @@
55
#include "utils/CollectionUtils.h"
66
#include "Paths.h"
77

8-
void printer::CoverageAndResultsStatisticsPrinter::write(const utbot::ProjectContext &projectContext,
9-
const Coverage::TestResultMap &testsResultMap,
10-
const Coverage::CoverageMap &coverageMap) {
11-
for (auto const &[testPath, testsResult]: testsResultMap) {
12-
fs::path sourcePath = Paths::testPathToSourcePath(projectContext, testPath);
13-
Coverage::FileCoverage fileCoverage = CollectionUtils::getOrDefault(coverageMap,
14-
sourcePath,
15-
Coverage::FileCoverage());
16-
insert({sourcePath, FileCoverageAndResultsStatistics(testsResult, fileCoverage)});
17-
}
18-
std::vector<std::string> metricNames = {
19-
"filename", "executionTime (ms)",
20-
"totalTestsNumber", "passedTestsNumber", "failedTestsNumber", "deathTestsNumber", "interruptedTestsNumber",
21-
"totalLinesNumber", "coveredLinesNumber", "lineCoverageRatio (%)"
22-
};
23-
std::string header = StringUtils::joinWith(metricNames, ",");
24-
std::stringstream ss;
25-
ss << header << '\n';
26-
for (auto const &[sourcePath, statistics]: *this) {
27-
ss << fs::relative(sourcePath, projectContext.projectPath) << ",";
28-
ss << statistics.totalExecutionTimeMs << ',';
29-
ss << statistics.totalTestsNum << ",";
30-
ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_PASSED, 0u)
8+
namespace printer {
9+
std::ostream &operator<<(std::ostream &os, const FileCoverageAndResultsStatistics &statistics) {
10+
// total tests execution file
11+
os << statistics.totalExecutionTimeMs << ',';
12+
// total number of tests
13+
os << statistics.totalTestsNum << ",";
14+
// number of passed tests
15+
os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_PASSED, 0u)
3116
<< ',';
32-
ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_FAILED, 0u)
17+
// number of failed tests
18+
os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_FAILED, 0u)
3319
<< ',';
34-
ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_DEATH, 0u) << ',';
35-
ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_INTERRUPTED, 0u)
20+
// number of death tests
21+
os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_DEATH, 0u) << ',';
22+
// number of interrupted tests
23+
os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_INTERRUPTED, 0u)
3624
<< ',';
3725

38-
uint32_t coveredLinesNum =
39-
statistics.coverage.fullCoverageLines.size() + statistics.coverage.partialCoverageLines.size();
40-
uint32_t totalLinesNum = coveredLinesNum + statistics.coverage.noCoverageLines.size();
26+
uint32_t coveredLinesNum = statistics.fullCoverageLinesNum + statistics.partialCoverageLinesNum;
27+
uint32_t totalLinesNum = coveredLinesNum + statistics.noCoverageLinesNum;
4128
double lineCoverageRatio = 0;
4229
if (totalLinesNum != 0) {
4330
lineCoverageRatio = 100.0 * coveredLinesNum / totalLinesNum;
4431
}
45-
ss << totalLinesNum << ',' << coveredLinesNum << ',';
46-
ss << std::fixed << lineCoverageRatio << '\n';
32+
// total number of lines and number of covered lines
33+
os << totalLinesNum << ',' << coveredLinesNum << ',';
34+
// line coverage ratio
35+
os << std::fixed << lineCoverageRatio;
36+
return os;
37+
}
38+
39+
void CoverageAndResultsStatisticsPrinter::write(const utbot::ProjectContext &projectContext,
40+
const Coverage::TestResultMap &testsResultMap,
41+
const Coverage::CoverageMap &coverageMap) {
42+
for (auto const &[testPath, testsResult]: testsResultMap) {
43+
fs::path sourcePath = Paths::testPathToSourcePath(projectContext, testPath);
44+
Coverage::FileCoverage fileCoverage = CollectionUtils::getOrDefault(coverageMap,
45+
sourcePath,
46+
Coverage::FileCoverage());
47+
insert({sourcePath, FileCoverageAndResultsStatistics(testsResult, fileCoverage)});
48+
}
49+
std::vector<std::string> metricNames = {
50+
"filename", "executionTime (ms)",
51+
"totalTestsNumber", "passedTestsNumber", "failedTestsNumber", "deathTestsNumber",
52+
"interruptedTestsNumber",
53+
"totalLinesNumber", "coveredLinesNumber", "lineCoverageRatio (%)"
54+
};
55+
std::string header = StringUtils::joinWith(metricNames, ",");
56+
std::stringstream ss;
57+
ss << header << '\n';
58+
59+
FileCoverageAndResultsStatistics total;
60+
for (auto const &[sourcePath, statistics]: *this) {
61+
total += statistics;
62+
ss << fs::relative(sourcePath, projectContext.projectPath).string() << ",";
63+
ss << statistics << '\n';
64+
}
65+
ss << "Total,";
66+
ss << total << '\n';
67+
fs::path resultsFilePath = resultsDirectory / "coverage-and-results-stats.csv";
68+
FileSystemUtils::writeToFile(resultsFilePath, ss.str());
69+
LOG_S(INFO) << StringUtils::stringFormat("See statistics info here: %s", resultsFilePath);
70+
}
71+
72+
FileCoverageAndResultsStatistics::FileCoverageAndResultsStatistics(
73+
const Coverage::FileTestsResult &testsResult,
74+
const Coverage::FileCoverage &fileCoverage) {
75+
totalTestsNum = 0;
76+
totalExecutionTimeMs = 0;
77+
for (const auto &[_, testResult]: testsResult) {
78+
totalTestsNum++;
79+
testsWithStatusNum[testResult.status()]++;
80+
totalExecutionTimeMs += testResult.executiontimems();
81+
}
82+
fullCoverageLinesNum = fileCoverage.fullCoverageLines.size();
83+
partialCoverageLinesNum = fileCoverage.partialCoverageLines.size();
84+
noCoverageLinesNum = fileCoverage.noCoverageLines.size();
85+
}
86+
87+
FileCoverageAndResultsStatistics &
88+
FileCoverageAndResultsStatistics::operator+=(const FileCoverageAndResultsStatistics &other) {
89+
totalExecutionTimeMs += other.totalExecutionTimeMs;
90+
totalTestsNum += other.totalTestsNum;
91+
for (auto &[status, testsNum]: testsWithStatusNum) {
92+
testsNum += CollectionUtils::getOrDefault(other.testsWithStatusNum, status, 0u);
93+
}
94+
fullCoverageLinesNum += other.fullCoverageLinesNum;
95+
partialCoverageLinesNum += other.partialCoverageLinesNum;
96+
noCoverageLinesNum += other.noCoverageLinesNum;
97+
return *this;
4798
}
48-
fs::path resultsFilePath = resultsDirectory / (TimeUtils::getDate() + "-stats.csv");
49-
FileSystemUtils::writeToFile(resultsFilePath, ss.str());
50-
LOG_S(INFO) << StringUtils::stringFormat("See statistics info here: %s", resultsFilePath);
51-
}
5299

53-
printer::FileCoverageAndResultsStatistics::FileCoverageAndResultsStatistics(
54-
const Coverage::FileTestsResult &testsResult,
55-
Coverage::FileCoverage fileCoverage) {
56-
coverage = std::move(fileCoverage);
57-
totalTestsNum = 0;
58-
totalExecutionTimeMs = 0;
59-
for (const auto &[_, testResult]: testsResult) {
60-
totalTestsNum++;
61-
testsWithStatusNum[testResult.status()]++;
62-
totalExecutionTimeMs += testResult.executiontimems();
100+
FileCoverageAndResultsStatistics
101+
FileCoverageAndResultsStatistics::operator+(FileCoverageAndResultsStatistics other) const {
102+
other += *this;
103+
return other;
63104
}
64105
}

server/src/printers/CoverageAndResultsStatisticsPrinter.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@
1212
namespace printer {
1313
class FileCoverageAndResultsStatistics {
1414
public:
15-
FileCoverageAndResultsStatistics(const Coverage::FileTestsResult &testsResult, Coverage::FileCoverage coverage);
15+
FileCoverageAndResultsStatistics() = default;
16+
FileCoverageAndResultsStatistics(const Coverage::FileTestsResult &testsResult, const Coverage::FileCoverage &coverage);
1617

1718
// Time statistics
18-
uint32_t totalExecutionTimeMs;
19+
uint32_t totalExecutionTimeMs = 0;
1920

2021
// Test runs statistics
21-
uint32_t totalTestsNum;
22-
std::unordered_map<testsgen::TestStatus, uint32_t> testsWithStatusNum;
22+
uint32_t totalTestsNum = 0;
23+
std::unordered_map<testsgen::TestStatus, uint32_t> testsWithStatusNum = {};
2324

2425
// Coverage
25-
Coverage::FileCoverage coverage;
26+
uint32_t fullCoverageLinesNum = 0;
27+
uint32_t partialCoverageLinesNum = 0;
28+
uint32_t noCoverageLinesNum = 0;
29+
30+
friend std::ostream& operator<<(std::ostream &os, const FileCoverageAndResultsStatistics &statistics);
31+
FileCoverageAndResultsStatistics& operator+=(const FileCoverageAndResultsStatistics &other);
32+
FileCoverageAndResultsStatistics operator+(FileCoverageAndResultsStatistics other) const;
2633
};
2734

35+
std::ostream& operator<<(std::ostream &os, const FileCoverageAndResultsStatistics &statistics);
36+
2837
class CoverageAndResultsStatisticsPrinter : CollectionUtils::MapFileTo<FileCoverageAndResultsStatistics> {
2938
public:
3039
explicit CoverageAndResultsStatisticsPrinter(fs::path resultsDirectory) : resultsDirectory(

0 commit comments

Comments
 (0)