33 this->saveToCSVFile(sFil);
34 std::cout <<
"[MRPT global profiler] Write stats to: " << sFil
64 m_keep_whole_history(keep_whole_history)
90 for (
size_t p = 0;
p <
s.size();
p +=
len)
93 if (
p +
len <
s.size()) ret +=
"\n";
101 for (
const auto& e :
m_data)
104 cs.
min_t = e.second.min_t;
105 cs.
max_t = e.second.max_t;
107 cs.
mean_t = e.second.n_calls ? e.second.mean_t / e.second.n_calls : 0;
109 cs.
last_t = e.second.last_t;
120 string mrpt_string =
"MRPT CTimeLogger report "s;
122 string top_header(name_tmp + mrpt_string);
125 const auto space_to_fill = top_header.size() < column_width
126 ? (column_width - top_header.size()) / 2
129 top_header = dashes_half + top_header + dashes_half;
130 if (dashes_half.size() % 2)
137 " FUNCTION #CALLS MIN.T MEAN.T " 141 stats_text += top_header +
"\n"s;
142 stats_text += middle_header +
"\n"s;
143 stats_text += bottom_header +
"\n"s;
146 using NameAndCallData = std::map<std::string_view, TCallData>;
147 NameAndCallData stat_strs;
148 for (
const auto& i :
m_data) stat_strs[i.first] = i.second;
163 for (
const auto& i : stat_strs)
165 string line =
string(i.first);
167 const auto dot_pos = line.find(
".");
168 if (dot_pos == std::string::npos)
174 const auto parent_pos = line.find(last_parent);
175 if (parent_pos != std::string::npos)
177 line =
"+-> "s + line.substr(dot_pos);
181 const string sMinT =
unitsFormat(i.second.min_t, 1,
false);
182 const string sMaxT =
unitsFormat(i.second.max_t, 1,
false);
183 const string sTotalT =
unitsFormat(i.second.mean_t, 1,
false);
185 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0, 1,
189 "%s %7u %6s%c %6s%c %6s%c %6s%c\n",
191 static_cast<unsigned int>(i.second.n_calls), sMinT.c_str(),
192 i.second.has_time_units ?
's' :
' ', sMeanT.c_str(),
193 i.second.has_time_units ?
's' :
' ', sMaxT.c_str(),
194 i.second.has_time_units ?
's' :
' ', sTotalT.c_str(),
195 i.second.has_time_units ?
's' :
' ');
199 stats_text += footer +
"\n";
207 s +=
"FUNCTION, #CALLS, LAST.T, MIN.T, MEAN.T, MAX.T, TOTAL.T [, " 209 for (
const auto& i :
m_data)
212 "\"%.*s\",%7u,%e,%e,%e,%e,%e", static_cast<int>(i.first.size()),
213 i.first.data(),
static_cast<unsigned int>(i.second.n_calls),
214 i.second.last_t, i.second.min_t,
215 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0,
216 i.second.max_t, i.second.mean_t);
218 if (i.second.whole_history)
220 const auto& wh = i.second.whole_history.value();
221 for (
const double v : wh)
229 std::ofstream(csv_file) <<
s;
241 "s.whole = struct();\n\n"s;
249 for (
const auto& i :
m_data)
251 s_names +=
"'"s + i.first +
"',"s;
257 if (i.second.whole_history)
261 std::replace(clean_name.begin(), clean_name.end(),
'.',
'_');
262 std::replace(clean_name.begin(), clean_name.end(),
'-',
'_');
263 std::replace(clean_name.begin(), clean_name.end(),
'+',
'_');
264 std::replace(clean_name.begin(), clean_name.end(),
'*',
'_');
266 s +=
"s.whole."s + clean_name +
"=[";
267 const auto& wh = i.second.whole_history.value();
285 std::ofstream(file) <<
s;
340 const std::string_view& event_name,
const double value,
const bool is_time)
372 TDataMap::const_iterator it =
m_data.find(
name);
376 return it->second.n_calls ? it->second.mean_t / it->second.n_calls : 0;
380 TDataMap::const_iterator it =
m_data.find(
name);
384 return it->second.last_t;
388 const CTimeLogger& logger,
const std::string_view& section_name)
389 : m_logger(const_cast<
CTimeLogger&>(logger)), m_section_name(section_name)
412 LVL_INFO,
"[CTimeLoggerSaveAtDtor] Saving stats to: `"s +
name +
"`"s);
double Tac() noexcept
Stops the stopwatch.
const std::string & getName() const
void clear(bool deep_clear=false)
Resets all stats.
mrpt::system::CTimeLogger & m_tm
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the COutputLogger interface.
std::string fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores ('_') or any other user-given char. ...
~MyGlobalProfiler() override
mrpt::system::CTimeLogger & global_profiler_getref() noexcept
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
double getLastTime(const std::string &name) const
Return the last execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
static time_point now() noexcept
Returns the current time, with the highest resolution available.
void registerUserMeasure(const std::string_view &event_name, const double value, const bool is_time=false)
void stop()
for correct use, see docs for CTimeLoggerEntry
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
static MyGlobalProfiler global_profiler
std::stack< double, std::vector< double > > open_calls
void global_profiler_enter(const char *func_name) noexcept
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void saveToMFile(const std::string &m_file) const
Dump all stats to a Matlab/Octave (.m) file.
std::optional< std::deque< double > > whole_history
Versatile class for consistent logging and management of output messages.
bool m_keep_whole_history
const std::string m_section_name
double do_leave(const std::string_view &func_name)
CTimeLogger(bool enabled=true, const std::string &name="", const bool keep_whole_history=false)
mrpt::Clock::time_point m_entry
GLsizei const GLchar ** string
double getMeanTime(const std::string &name) const
Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
#define MRPT_LOG_INFO_STREAM(__CONTENTS)
void global_profiler_leave(const char *func_name) noexcept
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string unitsFormat(const double val, int nDecimalDigits=2, bool middle_space=true)
This function implements formatting with the appropriate SI metric unit prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K', 1e6->'M', 1e9->'G', 1e12->'T'.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
void deep_clear(CONTAINER &c)
Deep clear for a std vector container.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
std::string aux_format_string_multilines(const std::string &s, const size_t len)
GLuint const GLchar * name
std::string rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger=false)
Enlarge the string with spaces up to the given length.
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
GLsizei const GLfloat * value
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds...
void Tic() noexcept
Starts the stopwatch.
void do_enter(const std::string_view &func_name)
std::string extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
void logStr(const VerbosityLevel level, const std::string &msg_str) const
Main method to add the specified message string to the logger.
void enter(const std::string_view &func_name)
Start of a named section.
CTimeLoggerEntry(const CTimeLogger &logger, const std::string_view §ion_name)
double leave(const std::string_view &func_name)
End of a named section.
~CTimeLogger() override
Destructor.