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

feat: offer csv-formatted totals (by duration) output #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
78 changes: 44 additions & 34 deletions Test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "MMeter.h"

#include <fstream>
Copy link
Owner

Choose a reason for hiding this comment

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

Better not to use fstream just for the demo, iostream is sufficient

#include <iomanip>
#include <iostream>
#include <sstream>
Expand All @@ -8,63 +9,63 @@

int calcInt(int ctr)
{
MMETER_FUNC_PROFILER;

int val = 0;
for (int i = 0; i <= ctr; i++)
{
val += i;
}
return val;
MMETER_FUNC_PROFILER;

int val = 0;
for (int i = 0; i <= ctr; i++)
{
val += i;
}
return val;
}

int calcFloat(int ctr)
{
MMETER_FUNC_PROFILER;

float val = 0;
for (int i = 0; i <= ctr; i++)
{
val += i;
}
return val;
MMETER_FUNC_PROFILER;

float val = 0;
for (int i = 0; i <= ctr; i++)
{
val += i;
}
return val;
}

std::string calcString(int ctr)
{
MMETER_FUNC_PROFILER;

std::stringstream ss;
for (int i = 0; i <= ctr; i++)
{
ss << i << ' ';
}
return ss.str();
MMETER_FUNC_PROFILER;

std::stringstream ss;
for (int i = 0; i <= ctr; i++)
{
ss << i << ' ';
}
return ss.str();
}

double calcNumbers(int ctr)
{
MMETER_FUNC_PROFILER;
MMETER_FUNC_PROFILER;

return (calcInt(ctr) + calcFloat(ctr));
return (calcInt(ctr) + calcFloat(ctr));
}

double calcAll(int ctr)
{
MMETER_FUNC_PROFILER;
MMETER_FUNC_PROFILER;

calcString(ctr);
return (calcInt(ctr) + calcFloat(ctr));
calcString(ctr);
return (calcInt(ctr) + calcFloat(ctr));
}

void test()
{
MMETER_FUNC_PROFILER;
MMETER_FUNC_PROFILER;

int COUNT = 50000000;
int COUNT = 50000000;

calcNumbers(COUNT);
calcAll(COUNT);
calcNumbers(COUNT);
calcAll(COUNT);
}

int main()
Expand All @@ -75,7 +76,16 @@ int main()
});
t1.join();

std::ofstream ofs("test.csv", std::ios::out | std::ios::trunc);
Copy link
Owner

Choose a reason for hiding this comment

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

Better to output it to cout, like the other examples. This way the demo is showcase-only.

if (!ofs)
{
throw std::ios_base::failure("Failed to open the file for writing.");
}

std::cout << std::fixed << std::setprecision(6) << MMeter::getGlobalTreePtr()->totalsByDurationStr() << std::endl;
std::cout << *MMeter::getGlobalTreePtr() << std::endl;
MMeter::getGlobalTreePtr()->outputBranchPercentagesToOStream(std::cout);
}
MMeter::getGlobalTreePtr()->outputBranchDurationsToOStream(std::cout);
MMeter::getGlobalTreePtr()->outputTotalsCSVToOStream(ofs);
MMeter::getGlobalTreePtr()->outputTotalsByDurationCSVToOStream(ofs);
}
12 changes: 12 additions & 0 deletions include/MMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ class FuncProfilerTree
*/
void outputBranchPercentagesToOStream(std::ostream &out, size_t indent = 0, size_t indentSpaces = 4) const;

/**
* @brief Outputs csv-formatted totals (totalsStr) to a stream
* @param out Output stream
*/
void outputTotalsCSVToOStream(std::ostream &out) const;

/**
* @brief Outputs csv-formatted totals (totalsByDurationStr) to a stream
* @param out Output stream
*/
void outputTotalsByDurationCSVToOStream(std::ostream &out) const;

/*
Tree manipulation
*/
Expand Down
26 changes: 24 additions & 2 deletions src/MMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ String FuncProfilerTree::totalsStr(size_t indent, size_t indentSpaces) const
return ss.str();
}

void FuncProfilerTree::outputTotalsCSVToOStream(std::ostream &out) const
{
SStream ss;
out << "Function Name,Time (s),Call Number\n";
Copy link
Owner

Choose a reason for hiding this comment

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

Header should probably be optional. Add a bool parameter?


for (auto [name, result] : totals())
{
out << name << "," << result.realDuration.count() << "," << result.callCount << std::endl;
}
}

String FuncProfilerTree::totalsByDurationStr(size_t indent, size_t indentSpaces) const
{
SStream ss;
Expand All @@ -152,6 +163,16 @@ String FuncProfilerTree::totalsByDurationStr(size_t indent, size_t indentSpaces)
return ss.str();
}

void FuncProfilerTree::outputTotalsByDurationCSVToOStream(std::ostream &out) const
{
out << "Time (s),Call Number,Function Name\n";
Copy link
Owner

Choose a reason for hiding this comment

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

Also make the header optional.


for (auto [duration, result] : totalsByDuration())
{
out << duration.count() << "," << result.callCount << "," << result.branchName << std::endl;
}
}

void FuncProfilerTree::outputBranchDurationsToOStream(std::ostream &out, size_t indent, size_t indentSpaces) const
{
if (mBranches.size() > 0)
Expand All @@ -175,7 +196,8 @@ void FuncProfilerTree::outputBranchDurationsToOStream(std::ostream &out, size_t
out << durationPtrPair.first.count() << "s /#";
if (durationPtrPair.second == nullptr)
{
out << mCount << " - " << "<body>" << std::endl;
out << mCount << " - "
<< "<body>" << std::endl;
}
else
{
Expand Down Expand Up @@ -318,4 +340,4 @@ FuncProfilerTree *getThreadLocalTreePtr()
return &threadTreeWrapper.localTree;
}

} // namespace MMeter
} // namespace MMeter