RingBuffer: Add CalculateRate()

refs #5750
This commit is contained in:
Noah Hilverling 2017-11-14 11:03:05 +01:00
parent 054faa54d5
commit c10f0a639b
2 changed files with 18 additions and 2 deletions

View File

@ -20,11 +20,12 @@
#include "base/ringbuffer.hpp" #include "base/ringbuffer.hpp"
#include "base/objectlock.hpp" #include "base/objectlock.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
#include <algorithm>
using namespace icinga; using namespace icinga;
RingBuffer::RingBuffer(RingBuffer::SizeType slots) RingBuffer::RingBuffer(RingBuffer::SizeType slots)
: Object(), m_Slots(slots, 0), m_TimeValue(0) : Object(), m_Slots(slots, 0), m_TimeValue(0), m_InsertedValues(0)
{ } { }
RingBuffer::SizeType RingBuffer::GetLength(void) const RingBuffer::SizeType RingBuffer::GetLength(void) const
@ -40,6 +41,9 @@ void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num)
RingBuffer::SizeType offsetTarget = tv % m_Slots.size(); RingBuffer::SizeType offsetTarget = tv % m_Slots.size();
if (m_TimeValue == 0)
m_InsertedValues = 1;
if (tv > m_TimeValue) { if (tv > m_TimeValue) {
RingBuffer::SizeType offset = m_TimeValue % m_Slots.size(); RingBuffer::SizeType offset = m_TimeValue % m_Slots.size();
@ -51,6 +55,9 @@ void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num)
offset = 0; offset = 0;
m_Slots[offset] = 0; m_Slots[offset] = 0;
if (m_TimeValue != 0 && m_InsertedValues < m_Slots.size())
m_InsertedValues++;
} }
m_TimeValue = tv; m_TimeValue = tv;
@ -68,7 +75,7 @@ int RingBuffer::UpdateAndGetValues(RingBuffer::SizeType tv, RingBuffer::SizeType
if (span > m_Slots.size()) if (span > m_Slots.size())
span = m_Slots.size(); span = m_Slots.size();
int off = m_TimeValue % m_Slots.size();; int off = m_TimeValue % m_Slots.size();
int sum = 0; int sum = 0;
while (span > 0) { while (span > 0) {
sum += m_Slots[off]; sum += m_Slots[off];
@ -82,3 +89,10 @@ int RingBuffer::UpdateAndGetValues(RingBuffer::SizeType tv, RingBuffer::SizeType
return sum; return sum;
} }
double RingBuffer::CalculateRate(RingBuffer::SizeType tv, RingBuffer::SizeType span)
{
ObjectLock olock(this);
int sum = UpdateAndGetValues(tv, span);
return sum / static_cast<double>(std::min(span, m_InsertedValues));
}

View File

@ -44,10 +44,12 @@ public:
SizeType GetLength(void) const; SizeType GetLength(void) const;
void InsertValue(SizeType tv, int num); void InsertValue(SizeType tv, int num);
int UpdateAndGetValues(SizeType tv, SizeType span); int UpdateAndGetValues(SizeType tv, SizeType span);
double CalculateRate(SizeType tv, SizeType span);
private: private:
std::vector<int> m_Slots; std::vector<int> m_Slots;
SizeType m_TimeValue; SizeType m_TimeValue;
SizeType m_InsertedValues;
}; };
} }