-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include "MMeter.h" | ||
|
||
#include <fstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <sstream> | ||
|
@@ -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() | ||
|
@@ -75,7 +76,16 @@ int main() | |
}); | ||
t1.join(); | ||
|
||
std::ofstream ofs("test.csv", std::ios::out | std::ios::trunc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 | ||
{ | ||
|
@@ -318,4 +340,4 @@ FuncProfilerTree *getThreadLocalTreePtr() | |
return &threadTreeWrapper.localTree; | ||
} | ||
|
||
} // namespace MMeter | ||
} // namespace MMeter |
There was a problem hiding this comment.
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