From 307ad244b005e60bb4099fba188f431e9290c459 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 21 Jan 2014 08:44:12 -0500 Subject: [PATCH] Use duration in insight::Event, add chrono_util --- Builds/VisualStudio2012/beast.vcxproj | 1 + Builds/VisualStudio2012/beast.vcxproj.filters | 3 + beast/Chrono.h | 1 + beast/chrono/chrono_util.h | 66 +++++++++++++++++++ beast/insight/Collector.h | 2 + beast/insight/Event.h | 19 +++--- beast/insight/EventImpl.h | 4 +- beast/insight/Meter.h | 21 ++++-- beast/insight/impl/NullCollector.cpp | 2 +- beast/insight/impl/StatsDCollector.cpp | 23 +++---- 10 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 beast/chrono/chrono_util.h diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index 8914c8f9a2..ec1388eac9 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -90,6 +90,7 @@ + diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index 0f09ebf75a..5bd137834e 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -1239,6 +1239,9 @@ beast + + beast\chrono + diff --git a/beast/Chrono.h b/beast/Chrono.h index c0ea88da49..3d9c46a8f9 100644 --- a/beast/Chrono.h +++ b/beast/Chrono.h @@ -22,6 +22,7 @@ #include "chrono/abstract_clock.h" #include "chrono/chrono_io.h" +#include "chrono/chrono_util.h" #include "chrono/manual_clock.h" #include "chrono/ratio_io.h" diff --git a/beast/chrono/chrono_util.h b/beast/chrono/chrono_util.h new file mode 100644 index 0000000000..5d62be65a5 --- /dev/null +++ b/beast/chrono/chrono_util.h @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_CHRONO_UTIL_H_INCLUDED +#define BEAST_CHRONO_UTIL_H_INCLUDED + +// From Howard Hinnant +// http://home.roadrunner.com/~hinnant/duration_io/chrono_util.html + +// round down +template +To floor(std::chrono::duration const& d) +{ + To t = std::chrono::duration_cast(d); + if (t > d) + --t; + return t; +} + +// round to nearest, to even on tie +template +To round (std::chrono::duration const& d) +{ + To t0 = std::chrono::duration_cast(d); + To t1 = t0; + ++t1; + auto diff0 = d - t0; + auto diff1 = t1 - d; + if (diff0 == diff1) + { + if (t0.count() & 1) + return t1; + return t0; + } + else if (diff0 < diff1) + return t0; + return t1; +} + +// round up +template +To ceil (std::chrono::duration const& d) +{ + To t = std::chrono::duration_cast(d); + if (t < d) + ++t; + return t; +} + +#endif diff --git a/beast/insight/Collector.h b/beast/insight/Collector.h index 9d379307cc..5595d88c05 100644 --- a/beast/insight/Collector.h +++ b/beast/insight/Collector.h @@ -44,6 +44,8 @@ namespace insight { class Collector { public: + typedef std::shared_ptr ptr; + virtual ~Collector() = 0; /** Create a hook. diff --git a/beast/insight/Event.h b/beast/insight/Event.h index 7c822fed7e..fac4be70e6 100644 --- a/beast/insight/Event.h +++ b/beast/insight/Event.h @@ -20,10 +20,13 @@ #ifndef BEAST_INSIGHT_EVENT_H_INCLUDED #define BEAST_INSIGHT_EVENT_H_INCLUDED +#include #include #include "EventImpl.h" +#include "../chrono/chrono_util.h" + namespace beast { namespace insight { @@ -45,8 +48,7 @@ public: A null metric reports no information. */ Event () - { - } + { } /** Create the metric reference the specified implementation. Normally this won't be called directly. Instead, call the appropriate @@ -55,17 +57,14 @@ public: */ explicit Event (std::shared_ptr const& impl) : m_impl (impl) - { - } + { } - /** Push an event notification. - The value specifies the elapsed time in milliseconds, or any other - domain specific value. - */ - void notify (value_type value) const + /** Push an event notification. */ + template + void notify (std::chrono::duration const& value) const { if (m_impl) - m_impl->notify (value); + m_impl->notify (ceil (value)); } private: diff --git a/beast/insight/EventImpl.h b/beast/insight/EventImpl.h index 3e84fe4260..567a1def2b 100644 --- a/beast/insight/EventImpl.h +++ b/beast/insight/EventImpl.h @@ -30,10 +30,10 @@ class Event; class EventImpl : public std::enable_shared_from_this { public: - typedef uint64 value_type; + typedef std::chrono::milliseconds value_type; virtual ~EventImpl () = 0; - virtual void notify (value_type value) = 0; + virtual void notify (value_type const& value) = 0; }; } diff --git a/beast/insight/Meter.h b/beast/insight/Meter.h index 3471fb31f5..2743e9c2cd 100644 --- a/beast/insight/Meter.h +++ b/beast/insight/Meter.h @@ -43,8 +43,7 @@ public: A null metric reports no information. */ Meter () - { - } + { } /** Create the metric reference the specified implementation. Normally this won't be called directly. Instead, call the appropriate @@ -53,8 +52,7 @@ public: */ explicit Meter (std::shared_ptr const& impl) : m_impl (impl) - { - } + { } /** Set a handler for polling. If a handler is set, it will be called once per collection interval. @@ -79,13 +77,22 @@ public: } Meter const& operator+= (value_type amount) const - { increment (amount); return *this; } + { + increment (amount); + return *this; + } Meter const& operator++ () const - { increment (1); return *this; } + { + increment (1); + return *this; + } Meter const& operator++ (int) const - { increment (1); return *this; } + { + increment (1); + return *this; + } /** @} */ private: diff --git a/beast/insight/impl/NullCollector.cpp b/beast/insight/impl/NullCollector.cpp index 0539d783a9..97b242c322 100644 --- a/beast/insight/impl/NullCollector.cpp +++ b/beast/insight/impl/NullCollector.cpp @@ -50,7 +50,7 @@ private: class NullEventImpl : public EventImpl { public: - void notify (value_type) + void notify (value_type const&) { } diff --git a/beast/insight/impl/StatsDCollector.cpp b/beast/insight/impl/StatsDCollector.cpp index 98cb84fa80..b99f52fc5b 100644 --- a/beast/insight/impl/StatsDCollector.cpp +++ b/beast/insight/impl/StatsDCollector.cpp @@ -112,9 +112,9 @@ public: ~StatsDEventImpl (); - void notify (EventImpl::value_type value); + void notify (EventImpl::value_type const& alue); - void do_notify (EventImpl::value_type value); + void do_notify (EventImpl::value_type const& value); void do_process (); private: @@ -527,7 +527,7 @@ StatsDEventImpl::~StatsDEventImpl () { } -void StatsDEventImpl::notify (EventImpl::value_type value) +void StatsDEventImpl::notify (EventImpl::value_type const& value) { m_impl->get_io_service().dispatch (std::bind ( &StatsDEventImpl::do_notify, @@ -535,13 +535,13 @@ void StatsDEventImpl::notify (EventImpl::value_type value) shared_from_this ()), value)); } -void StatsDEventImpl::do_notify (EventImpl::value_type value) +void StatsDEventImpl::do_notify (EventImpl::value_type const& value) { std::stringstream ss; ss << m_impl->prefix() << "." << m_name << ":" << - value << "|ms" << + value.count() << "|ms" << "\n"; m_impl->post_buffer (ss.str ()); } @@ -617,17 +617,18 @@ void StatsDGaugeImpl::do_increment (GaugeImpl::difference_type amount) if (amount > 0) { + GaugeImpl::value_type const d ( + static_cast (amount)); value += - (amount >= std::numeric_limits ::max() - m_value) + (d >= std::numeric_limits ::max() - m_value) ? std::numeric_limits ::max() - m_value - : GaugeImpl::value_type (amount); + : d; } else if (amount < 0) { - value -= - (std::abs (amount) >= m_value) - ? m_value - : std::abs (amount); + GaugeImpl::value_type const d ( + static_cast (-amount)); + value = (d >= value) ? 0 : value - d; } do_set (value);