31 this->saveToCSVFile(sFil);
32 std::cout <<
"[MRPT global profiler] Write stats to: " << sFil
57 : COutputLogger(
"CTimeLogger"), m_tictac(), m_enabled(enabled), m_name(
name)
71 m_enabled(o.m_enabled),
78 COutputLogger::operator=(o);
86 m_enabled(o.m_enabled),
93 COutputLogger::operator=(o);
114 for (
size_t p = 0;
p <
s.size();
p +=
len)
117 if (
p +
len <
s.size()) ret +=
"\n";
125 for (
const auto e :
m_data)
128 cs.
min_t = e.second.min_t;
129 cs.
max_t = e.second.max_t;
131 cs.
mean_t = e.second.n_calls ? e.second.mean_t / e.second.n_calls : 0;
133 cs.
last_t = e.second.last_t;
141 std::string mrpt_string =
"MRPT CTimeLogger report ";
146 int space_to_fill = top_header.size() < column_width
147 ? (column_width - top_header.size()) / 2
150 top_header = dashes_half + top_header + dashes_half;
151 if (dashes_half.size() % 2)
158 " FUNCTION #CALLS MIN.T MEAN.T " 162 stats_text += top_header +
"\n";
163 stats_text += middle_header +
"\n";
164 stats_text += bottom_header +
"\n";
167 for (
const auto i :
m_data)
169 const string sMinT =
unitsFormat(i.second.min_t, 1,
false);
170 const string sMaxT =
unitsFormat(i.second.max_t, 1,
false);
171 const string sTotalT =
unitsFormat(i.second.mean_t, 1,
false);
173 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0, 1,
177 "%s %7u %6s%c %6s%c %6s%c %6s%c\n",
179 static_cast<unsigned int>(i.second.n_calls), sMinT.c_str(),
180 i.second.has_time_units ?
's' :
' ', sMeanT.c_str(),
181 i.second.has_time_units ?
's' :
' ', sMaxT.c_str(),
182 i.second.has_time_units ?
's' :
' ', sTotalT.c_str(),
183 i.second.has_time_units ?
's' :
' ');
187 stats_text += footer +
"\n";
195 s +=
"FUNCTION, #CALLS, LAST.T, MIN.T, MEAN.T, MAX.T, TOTAL.T\n";
196 for (
const auto& i :
m_data)
199 "\"%s\",\"%7u\",\"%e\",\"%e\",\"%e\",\"%e\",\"%e\"\n",
200 i.first.c_str(),
static_cast<unsigned int>(i.second.n_calls),
201 i.second.last_t, i.second.min_t,
202 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0,
203 i.second.max_t, i.second.mean_t);
215 const string s = func_name;
227 const string s = func_name;
254 const char* event_name,
const double value)
257 const string s = event_name;
276 : n_calls(0), min_t(0), max_t(0), mean_t(0), last_t(0), has_time_units(true)
286 return it->second.n_calls ? it->second.mean_t / it->second.n_calls : 0;
294 return it->second.last_t;
298 const CTimeLogger& logger,
const char* section_name)
299 : m_logger(const_cast<
CTimeLogger&>(logger)), m_section_name(section_name)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
std::stack< double, std::vector< double > > open_calls
void global_profiler_leave(const char *func_name) noexcept
const Scalar * const_iterator
void Tic()
Starts the stopwatch.
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...
MyGlobalProfiler global_profiler
void clear(bool deep_clear=false)
Resets all stats.
CTimeLogger & operator=(const CTimeLogger &o)
This CStream derived class allow using a file as a write-only, binary stream.
CTimeLogger(bool enabled=true, const std::string &name="")
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
double do_leave(const char *func_name)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void do_enter(const char *func_name)
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
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...
virtual ~CTimeLogger()
Default constructor.
GLsizei const GLchar ** string
CTimeLoggerEntry(const CTimeLogger &logger, const char *section_name)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void global_profiler_enter(const char *func_name) noexcept
#define MRPT_LOG_INFO_STREAM(__CONTENTS)
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'.
std::string aux_format_string_multilines(const std::string &s, const size_t len)
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...
GLuint const GLchar * name
double Tac()
Stops the stopwatch.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
double leave(const char *func_name)
End of a named section.
const char * m_section_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.
void registerUserMeasure(const char *event_name, const double value)
GLsizei const GLfloat * value
void enter(const char *func_name)
Start of a named section.
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the COutputLogger interface.
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...
mrpt::utils::CTimeLogger & global_profiler_getref() noexcept
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.