Skip to content

Commit

Permalink
Dynamically sized "Group Name" column.
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalInBlue committed Oct 6, 2023
1 parent 518ce41 commit b670b52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/celero/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace celero
void initialize(std::vector<std::string> userDefinedColumns);

void Console(const std::string& x);
void TableBanner();
void TableBanner(const size_t groupNameLength = 15);
void TableRowExperimentHeader(Experiment* x);
void TableRowFailure(const std::string& msg);
void TableRowProblemSpaceHeader(std::shared_ptr<celero::ExperimentResult> x);
Expand Down
13 changes: 10 additions & 3 deletions src/Celero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,13 @@ void celero::Run(int argc, char** argv)
userDefinedFields.insert(fieldName);
}
}

return bmark->getName().length();
};

// PrintConstants::ColumnWidth
auto maxGroupNameLength = size_t(15);

if(argument.empty() == false)
{
if(celero::TestVector::Instance().containsGroup(argument))
Expand All @@ -241,7 +246,8 @@ void celero::Run(int argc, char** argv)

if(bmark != nullptr)
{
collectFromBenchmark(bmark);
const auto groupNameLength = collectFromBenchmark(bmark);
maxGroupNameLength = std::max(maxGroupNameLength, groupNameLength);
}
}
else
Expand All @@ -258,15 +264,16 @@ void celero::Run(int argc, char** argv)

if(bmark != nullptr)
{
collectFromBenchmark(bmark);
const auto groupNameLength = collectFromBenchmark(bmark);
maxGroupNameLength = std::max(maxGroupNameLength, groupNameLength);
}
}
}

std::vector<std::string> userDefinedFieldsOrder(userDefinedFields.begin(), userDefinedFields.end());

Printer::get().initialize(userDefinedFieldsOrder);
Printer::get().TableBanner();
Printer::get().TableBanner(maxGroupNameLength);

const auto startTime = celero::timer::GetSystemTime();

Expand Down
23 changes: 19 additions & 4 deletions src/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ enum PrintConstants : size_t
ColumnWidth = 15
};

// Here, we demonstrate "worst-practices" my implementing a global variable.
// This is technical debt to shoe-horn in a way to dynamically adjust the column width for the group name.
// Public shaming is acceptable.
size_t GlobalGroupNameColumnWidth(15);

///
/// http://stackoverflow.com/questions/14765155/how-can-i-easily-format-my-data-table-in-c
/// Center-aligns string within a field of width w. Pads with blank spaces to enforce alignment.
Expand Down Expand Up @@ -186,17 +191,24 @@ std::string PrintHRule(const size_t additionalColumns = 0)

std::stringstream ss;
std::string column{":"};
std::string groupColumn{":"};

while(column.length() < PrintConstants::ColumnWidth)
{
column += "-";
}

while(groupColumn.length() < GlobalGroupNameColumnWidth)
{
groupColumn += "-";
}

std::string firstColumn = column + ":|";
groupColumn = groupColumn + ":|";

column += "-:|";

ss << "|" << firstColumn;
ss << "|" << groupColumn;

for(size_t i = 0; i < PrintConstants::NumberOfColumns + additionalColumns - 1; ++i)
{
Expand All @@ -215,11 +227,14 @@ namespace celero
std::cout << "Celero: " << x << std::endl;
}

void Printer::TableBanner()
void Printer::TableBanner(const size_t groupNameLength)
{
GlobalGroupNameColumnWidth = groupNameLength;

celero::console::SetConsoleColor(celero::console::ConsoleColor::Default);

std::cout << "|" << PrintCenter("Group") << PrintCenter("Experiment") << PrintCenter("Prob. Space") << PrintCenter("Samples")
std::cout << "|" << PrintCenter("Group", GlobalGroupNameColumnWidth) << PrintCenter("Experiment") << PrintCenter("Prob. Space")
<< PrintCenter("Samples")
<< PrintCenter("Iterations") << PrintCenter("Baseline") << PrintCenter("us/Iteration") << PrintCenter("Iterations/sec")
<< PrintCenter("RAM (bytes)");

Expand All @@ -235,7 +250,7 @@ namespace celero
void Printer::TableRowExperimentHeader(Experiment* x)
{
celero::console::SetConsoleColor(celero::console::ConsoleColor::Default);
std::cout << "|" << PrintColumn(x->getBenchmark()->getName()) << PrintColumn(x->getName());
std::cout << "|" << PrintColumn(x->getBenchmark()->getName(), GlobalGroupNameColumnWidth) << PrintColumn(x->getName());
}

void Printer::TableRowFailure(const std::string& msg)
Expand Down

0 comments on commit b670b52

Please sign in to comment.