Use duration in insight::Event, add chrono_util

This commit is contained in:
Vinnie Falco
2014-01-21 08:44:12 -05:00
parent 0c0fa877cf
commit 307ad244b0
10 changed files with 111 additions and 31 deletions

View File

@@ -90,6 +90,7 @@
<ClInclude Include="..\..\beast\boost\ErrorCode.h" />
<ClInclude Include="..\..\beast\ByteOrder.h" />
<ClInclude Include="..\..\beast\chrono\chrono_io.h" />
<ClInclude Include="..\..\beast\chrono\chrono_util.h" />
<ClInclude Include="..\..\beast\chrono\CPUMeter.h" />
<ClInclude Include="..\..\beast\chrono\abstract_clock.h" />
<ClInclude Include="..\..\beast\chrono\manual_clock.h" />

View File

@@ -1239,6 +1239,9 @@
<ClInclude Include="..\..\beast\make_unique.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\chrono\chrono_util.h">
<Filter>beast\chrono</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\DynamicObject.cpp">

View File

@@ -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"

View File

@@ -0,0 +1,66 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <class To, class Rep, class Period>
To floor(std::chrono::duration <Rep, Period> const& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t > d)
--t;
return t;
}
// round to nearest, to even on tie
template <class To, class Rep, class Period>
To round (std::chrono::duration <Rep, Period> const& d)
{
To t0 = std::chrono::duration_cast<To>(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 <class To, class Rep, class Period>
To ceil (std::chrono::duration <Rep, Period> const& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t < d)
++t;
return t;
}
#endif

View File

@@ -44,6 +44,8 @@ namespace insight {
class Collector
{
public:
typedef std::shared_ptr <Collector> ptr;
virtual ~Collector() = 0;
/** Create a hook.

View File

@@ -20,10 +20,13 @@
#ifndef BEAST_INSIGHT_EVENT_H_INCLUDED
#define BEAST_INSIGHT_EVENT_H_INCLUDED
#include <chrono>
#include <memory>
#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 <EventImpl> 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 <class Rep, class Period>
void notify (std::chrono::duration <Rep, Period> const& value) const
{
if (m_impl)
m_impl->notify (value);
m_impl->notify (ceil <value_type> (value));
}
private:

View File

@@ -30,10 +30,10 @@ class Event;
class EventImpl : public std::enable_shared_from_this <EventImpl>
{
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;
};
}

View File

@@ -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 <MeterImpl> 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:

View File

@@ -50,7 +50,7 @@ private:
class NullEventImpl : public EventImpl
{
public:
void notify (value_type)
void notify (value_type const&)
{
}

View File

@@ -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 <GaugeImpl::value_type> (amount));
value +=
(amount >= std::numeric_limits <GaugeImpl::value_type>::max() - m_value)
(d >= std::numeric_limits <GaugeImpl::value_type>::max() - m_value)
? std::numeric_limits <GaugeImpl::value_type>::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 <GaugeImpl::value_type> (-amount));
value = (d >= value) ? 0 : value - d;
}
do_set (value);