mirror of
https://github.com/matrix-construct/construct
synced 2024-11-15 22:41:12 +01:00
ircd::util: Improvements to timer tool; fix accumulator semantics; comments.
This commit is contained in:
parent
f51ffb1530
commit
407202f85f
2 changed files with 82 additions and 30 deletions
|
@ -17,19 +17,46 @@ inline namespace util
|
||||||
struct timer;
|
struct timer;
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
/// Stopwatch w/ accumulation.
|
||||||
|
///
|
||||||
|
/// This utility can be used to gauge the duration of something. The clock
|
||||||
|
/// is started by default upon construction. Users can query the running time
|
||||||
|
/// using the at(), or using the pretty() convenience.
|
||||||
|
///
|
||||||
|
/// This timer features an accumulator state so the clock can be stopped and
|
||||||
|
/// continued, and each time the accumulator is updated. The accumulator
|
||||||
|
/// value is always added into results: when the timer is stopped, the result
|
||||||
|
/// is simply the accumulator value; when the clock is running, the result is
|
||||||
|
/// the accumulator value added to the difference of the current time and the
|
||||||
|
/// last start time.
|
||||||
|
///
|
||||||
|
/// One can instantiate the timer without starting the clock with the `nostart`
|
||||||
|
/// overload. Additionally, a lambda constructor will accumulate the time it
|
||||||
|
/// takes to execute the provided function.
|
||||||
struct ircd::util::timer
|
struct ircd::util::timer
|
||||||
{
|
{
|
||||||
IRCD_OVERLOAD(nostart)
|
|
||||||
using clock = std::chrono::steady_clock;
|
using clock = std::chrono::steady_clock;
|
||||||
|
IRCD_OVERLOAD(nostart)
|
||||||
|
|
||||||
nanoseconds accumulator {0ns};
|
nanoseconds accumulator
|
||||||
clock::time_point start {clock::now()};
|
{
|
||||||
|
0ns
|
||||||
|
};
|
||||||
|
|
||||||
|
clock::time_point start
|
||||||
|
{
|
||||||
|
clock::now()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Observers
|
||||||
bool stopped() const;
|
bool stopped() const;
|
||||||
template<class duration = std::chrono::seconds> duration get() const;
|
|
||||||
template<class duration = std::chrono::seconds> duration at() const;
|
template<class duration = std::chrono::seconds> duration at() const;
|
||||||
|
|
||||||
|
// Convenience wrapper for util::pretty()
|
||||||
string_view pretty(const mutable_buffer &out, const int &fmt = 0) const;
|
string_view pretty(const mutable_buffer &out, const int &fmt = 0) const;
|
||||||
std::string pretty(const int &fmt = 0) const;
|
std::string pretty(const int &fmt = 0) const;
|
||||||
|
|
||||||
|
// Control
|
||||||
void cont();
|
void cont();
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
|
@ -40,23 +67,40 @@ struct ircd::util::timer
|
||||||
|
|
||||||
inline
|
inline
|
||||||
ircd::util::timer::timer(nostart_t)
|
ircd::util::timer::timer(nostart_t)
|
||||||
:start{clock::time_point::min()}
|
:start
|
||||||
{
|
{
|
||||||
|
clock::time_point::min()
|
||||||
}
|
}
|
||||||
|
{}
|
||||||
|
|
||||||
template<class duration>
|
template<class duration>
|
||||||
duration
|
inline duration
|
||||||
ircd::util::timer::at()
|
ircd::util::timer::at()
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
const auto now(clock::now());
|
const auto now
|
||||||
return duration_cast<duration>(now - start);
|
{
|
||||||
|
!stopped()?
|
||||||
|
clock::now():
|
||||||
|
start
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto elapsed
|
||||||
|
{
|
||||||
|
duration_cast<decltype(accumulator)>(now - start)
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto accumulator
|
||||||
|
{
|
||||||
|
this->accumulator + elapsed
|
||||||
|
};
|
||||||
|
|
||||||
|
return duration_cast<duration>(accumulator);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class duration>
|
inline bool
|
||||||
duration
|
ircd::util::timer::stopped()
|
||||||
ircd::util::timer::get()
|
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return std::chrono::duration_cast<duration>(accumulator);
|
return start == clock::time_point::min();
|
||||||
}
|
}
|
||||||
|
|
44
ircd/util.cc
44
ircd/util.cc
|
@ -283,31 +283,46 @@ ircd::util::timer::timer(const std::function<void ()> &func)
|
||||||
void
|
void
|
||||||
ircd::util::timer::stop()
|
ircd::util::timer::stop()
|
||||||
{
|
{
|
||||||
if(stopped())
|
const auto now
|
||||||
return;
|
{
|
||||||
|
!stopped()?
|
||||||
|
clock::now():
|
||||||
|
start
|
||||||
|
};
|
||||||
|
|
||||||
const auto now(clock::now());
|
const auto elapsed
|
||||||
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
|
{
|
||||||
|
duration_cast<decltype(accumulator)>(now - start)
|
||||||
|
};
|
||||||
|
|
||||||
|
accumulator += elapsed;
|
||||||
start = clock::time_point::min();
|
start = clock::time_point::min();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ircd::util::timer::cont()
|
ircd::util::timer::cont()
|
||||||
{
|
{
|
||||||
if(!stopped())
|
const auto now
|
||||||
{
|
{
|
||||||
const auto now(clock::now());
|
clock::now()
|
||||||
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
start = clock::now();
|
const auto elapsed
|
||||||
|
{
|
||||||
|
!stopped()?
|
||||||
|
duration_cast<decltype(accumulator)>(now - start):
|
||||||
|
decltype(accumulator)(0)
|
||||||
|
};
|
||||||
|
|
||||||
|
accumulator += elapsed;
|
||||||
|
start = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
ircd::util::timer::pretty(const int &fmt)
|
ircd::util::timer::pretty(const int &fmt)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return util::pretty(at(), fmt);
|
return util::pretty(at<nanoseconds>(), fmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
ircd::string_view
|
ircd::string_view
|
||||||
|
@ -315,14 +330,7 @@ ircd::util::timer::pretty(const mutable_buffer &out,
|
||||||
const int &fmt)
|
const int &fmt)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return util::pretty(out, at(), fmt);
|
return util::pretty(out, at<nanoseconds>(), fmt);
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
ircd::util::timer::stopped()
|
|
||||||
const
|
|
||||||
{
|
|
||||||
return start == clock::time_point::min();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
Loading…
Reference in a new issue