31 namespace performanceAnalysis {
32 #ifdef XERUS_PERFORMANCE_ANALYSIS 33 const size_t startupTime =
uTime();
34 std::map<std::string, std::map<std::string, std::map<std::string, std::pair<size_t, size_t>>>> calls;
37 const size_t totalTime =
uTime() - startupTime;
38 size_t totalExplainedTime = 0;
40 std::stringstream mainStream;
41 mainStream << std::endl;
42 mainStream <<
"| ==================================================================================" << std::endl;
43 mainStream <<
"| ============================== Performance Analysis ==============================" << std::endl;
44 mainStream <<
"| ==================================================================================" << std::endl << std::endl;
46 for(
const auto& group : calls) {
47 size_t totalGroupCalls = 0, totalGroupTime = 0;
48 std::stringstream groupStream;
50 for(
const auto& call : group.second) {
51 size_t totalCallCalls = 0, totalCallTime = 0;
52 std::stringstream callStream;
54 for(
const auto& subCall : call.second) {
55 totalGroupCalls += subCall.second.first; totalCallCalls += subCall.second.first;
56 totalGroupTime += subCall.second.second; totalCallTime += subCall.second.second;
57 if(1000*subCall.second.second > totalTime) {
58 callStream <<
"| | " << std::setfill (
' ') << std::setw(10) << subCall.second.second/1000
59 <<
" ms ( " << std::setw(3) << (100*subCall.second.second)/totalTime <<
"% ) in " << std::setfill (
' ') << std::setw(10) << subCall.second.first
60 <<
" calls (" << std::setfill (
' ') << std::setw(8) << subCall.second.second/(1000*subCall.second.first)
61 <<
" ms in average) for " << subCall.first << std::endl;
64 if(1000*totalCallTime > totalTime) {
65 groupStream << std::endl;
66 groupStream <<
"| --------------------------- " << std::left << std::setfill (
' ') << std::setw(26) << call.first <<
" ---------------------------" << std::endl;
67 groupStream << callStream.str();
68 groupStream <<
"| Together " << std::setw(10) << std::right << totalCallTime/1000 <<
" ms (" << std::setw(3) << (100*totalCallTime)/totalTime <<
"% ) in " << std::setw(8) << totalCallCalls <<
" calls." << std::endl;
71 if(1000*totalGroupTime > totalTime) {
72 mainStream << std::endl;
73 mainStream <<
"| ============================== " << std::left << std::setfill (
' ') << std::setw(20) << group.first <<
" ==============================" << std::endl;
74 mainStream <<
"| ============ Total time " << std::setw(14) << std::right << totalGroupTime/1000 <<
" ms (" << std::setw(3) << (100*totalGroupTime)/totalTime <<
"% ) in " << std::setw(10) << totalGroupCalls <<
" calls ============" << std::endl;
75 mainStream << groupStream.str();
77 totalExplainedTime += totalGroupTime;
80 mainStream << std::endl <<
"| The analysed low level functions explain " << (100*totalExplainedTime)/(totalTime) <<
"% of the total time." << std::endl << std::endl;
81 return mainStream.str();
84 std::string
get_analysis() {
return "XERUS_PERFORMANCE_ANALYSIS must be set to obtain an analysis."; }
The main namespace of xerus.
size_t uTime()
: Returns the time since epoche in microseconds.