--- /dev/null
+// $Id$
+//
+// Copyright (C) 2010
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+/** \file
+ \brief StatisticAccumulator non-inline template implementation */
+
+// Custom includes
+
+#define prefix_
+///////////////////////////////ct.p////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::StatisticAccumulator<T>
+
+template <class T>
+prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator( T defaultvalue)
+ : defaultvalue_(defaultvalue),
+ sum_squared_(defaultvalue*defaultvalue),
+ sum_(defaultvalue),
+ min_(defaultvalue),
+ max_(defaultvalue),
+ last_avg_(float(defaultvalue)),
+ count_(0)
+{
+}
+
+//template <class T>
+//prefix_ senf::StatisticAccumulator<T>::~StatisticAccumulator()
+//{ }
+
+template <class T>
+prefix_ float senf::StatisticAccumulator<T>::stddev()
+ const
+{
+ if (count_ == 0) {
+ return NAN;
+ }
+ else if (count_ == 1) {
+ return 0.0;
+ }
+ float _avg (avg());
+ return sqrt( ( float(sum_squared_) / float( count_) ) - (_avg * _avg) );
+}
+
+template <class T>
+prefix_ void senf::StatisticAccumulator<T>::accumulate( T value)
+{
+ if (count_ == 0) {
+ min_ = max_ = sum_ = value;
+ sum_squared_ = value * value;
+ count_++;
+ return;
+ }
+ sum_ += value;
+ sum_squared_ += value * value;
+ count_++;
+ if (value < min_)
+ min_ = value;
+ else if (value > max_)
+ max_ = value;
+}
+
+template <class T>
+prefix_ void senf::StatisticAccumulator<T>::clear()
+{
+ last_avg_ = avg();
+ count_ = 0;
+ sum_squared_ = defaultvalue_*defaultvalue_;
+ sum_ = min_ = max_ = defaultvalue_;
+}
+
+
+///////////////////////////////ct.e////////////////////////////////////////
+#undef prefix_
--- /dev/null
+// $Id$
+//
+// Copyright (C) 2010
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+/** \file
+ \brief StatisticAccumulator inline template implementation */
+
+// Custom includes
+
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::StatisticAccumulator<T>
+
+template <class T>
+prefix_ T senf::StatisticAccumulator<T>::min()
+ const
+{
+ return min_;
+}
+
+template <class T>
+prefix_ T senf::StatisticAccumulator<T>::max()
+ const
+{
+ return max_;
+}
+
+template <class T>
+prefix_ float senf::StatisticAccumulator<T>::avg()
+ const
+{
+ return count_ == 0 ? NAN : (sum_ / float(count_));
+}
+
+//template <class T>
+//prefix_ senf::Statistics & senf::StatisticAccumulator<T>::statistics()
+//{
+// return stats_;
+//}
+
+template <class T>
+prefix_ float senf::StatisticAccumulator<T>::last_avg()
+ const
+{
+ return last_avg_;
+}
+
+template <class T>
+prefix_ boost::uint32_t senf::StatisticAccumulator<T>::count()
+ const
+{
+ return count_;
+}
+
+///////////////////////////////cti.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -U"
+// End:
--- /dev/null
+// $Id$
+//
+// Copyright (C) 2010
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+/** \file
+ \brief StatisticAccumulator public header */
+
+#ifndef HH_SENF_Utils_StatisticAccumulator_
+#define HH_SENF_Utils_StatisticAccumulator_ 1
+
+// Custom includes
+#include <math.h>
+#include <boost/cstdint.hpp>
+///////////////////////////////hh.p////////////////////////////////////////
+namespace senf {
+ /** \brief Accumulate measurement values
+
+ The accumulator mainly do the prelimenary work for the senf::Statistic class.
+ It accumulates certain values with in an interval to be used by senf::Statistics
+
+ \li the senf::Statistics class
+ \li statistics sources
+
+ This class provides the average, standard deviation, min, max values over one
+ interval, means until clear() is called. It rather directly calculates the results
+ then collection all single values provided by calling accumulate().
+ \see senf::Statistics to process accumulated values
+ \ingroup senf_statistics
+ */
+
+ template <class T>
+ class StatisticAccumulator
+ {
+ public:
+ StatisticAccumulator( T defaultvalue = 0);
+// virtual ~StatisticAccumulator();
+
+ void clear();
+ ///< Reset accumulated values.
+ /**< This member reset all avg/min/max values to the given \a
+ defaultvalue and the count to zero. */
+ void accumulate(T value);
+ ///< Gather value to be accumulated.
+ /**< This method accumulate a value.
+ \param[in] value to be accumulated value*/
+
+ public:
+ T min() const;
+ ///< Returns current minimal value.
+ /**< This method returns the minimal value of the current accumulation.*/
+ T max() const;
+ ///< Returns current maximal value.
+ /**< This method returns the maximal value of the current accumulation.*/
+ float avg() const;
+ ///< Returns average value.
+ /**< This method returns the average value of the current accumulation.*/
+ float last_avg() const;
+ ///< Returns former average value.
+ /**< This method returns the average value of the former accumulation period.*/
+ float stddev() const;
+ ///< Returns standard deviation value.
+ /**< This method returns the standard deviation value of the current accumulation.*/
+ boost::uint32_t count() const;
+ ///< Returns count of accumulated values.
+ /**< This method returns count of accumulated values of the current accumulation.*/
+
+ private:
+ T defaultvalue_;
+ T sum_squared_;
+ T sum_;
+ T min_;
+ T max_;
+ float last_avg_;
+ boost::uint32_t count_;
+
+
+ };
+
+
+ typedef StatisticAccumulator<int> StatisticAccumulatorInt;
+ typedef StatisticAccumulator<float> StatisticAccumulatorFloat;
+
+
+}
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "StatisticAccumulator.cci"
+#include "StatisticAccumulator.ct"
+#include "StatisticAccumulator.cti"
+#endif
+
--- /dev/null
+// $Id$
+//
+// Copyright (C) 2010
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+/** \file
+ \brief StatisticAccumulator non-inline template implementation */
+
+// Custom includes
+
+#include "StatisticAccumulator.hh"
+#include "Statistics.hh"
+#include "auto_unit_test.hh"
+#include <boost/test/test_tools.hpp>
+#include <boost/math/special_functions/fpclassify.hpp>
+
+#define prefix_
+///////////////////////////////ct.p////////////////////////////////////////
+SENF_AUTO_UNIT_TEST(StatisticAccumulator)
+{
+ senf::Statistics s;
+
+ senf::StatisticAccumulatorInt sa;
+
+ sa.accumulate( 5);
+ sa.accumulate( 3);
+ sa.accumulate( 4);
+ sa.accumulate( 6);
+ sa.accumulate( 7);
+
+ BOOST_CHECK_EQUAL( sa.max(), 7);
+ BOOST_CHECK_EQUAL( sa.min(), 3);
+ BOOST_CHECK_EQUAL( sa.avg(), 5.0);
+ BOOST_CHECK_EQUAL( sa.last_avg(), 0.0);
+// BOOST_CHECK_EQUAL( sa.stddev(), 1.41421354);
+ BOOST_CHECK_EQUAL( sa.count(), 5);
+
+ s(0,sa);
+
+ BOOST_CHECK_EQUAL( sa.max(), 0);
+ BOOST_CHECK_EQUAL( sa.min(), 0);
+ BOOST_CHECK( (boost::math::isnan)( sa.avg()));
+ BOOST_CHECK_EQUAL( sa.last_avg(), 5.0);
+ BOOST_CHECK( (boost::math::isnan)( sa.stddev()));
+ BOOST_CHECK_EQUAL( sa.count(), 0);
+
+
+
+
+}
+///////////////////////////////ct.e////////////////////////////////////////
+#undef prefix_
enter(1, value, value, value, dev);
}
+
prefix_ senf::StatisticsBase::OutputProxy<senf::Statistics>
senf::Statistics::output(unsigned n)
{
return entry_->dir;
}
+///////////////////////////////////////////////////////////////////////////
+// senf::Statistics
+
+template <class Value>
+prefix_ void senf::Statistics::operator()(unsigned n, StatisticAccumulator<Value> & sa)
+{
+ enter(n, float(sa.min()), sa.avg(), float(sa.max()), sa.stddev());
+ sa.clear();
+}
+
+
//-/////////////////////////////////////////////////////////////////////////////////////////////////
#undef prefix_
#include "Exception.hh"
#include <senf/Utils/Logger/Logger.hh>
#include <senf/Utils/Console/Console.hh>
+#include "StatisticAccumulator.hh"
//#include "Statistics.mpp"
//-/////////////////////////////////////////////////////////////////////////////////////////////////
The statistics functionality has two parts:
\li the senf::Statistics class
+ \li the senf::StatisticsAccumulator class
\li statistics sources
Each senf::Statistics instance collects information about a single parameter. Which
///< Same as operator() with \a min == \a avg == \a max
/**< Provided so a Statistics instance can be directly used
as a signal target. */
+ template <class Value>
+ void operator()(unsigned n, StatisticAccumulator<Value> & sa);
+ ///< Same as operator() gathers values from StatisticAccumulator
+ /**< Provided so a Statistics instance can be directly used
+ as a signal target. Caution: Clears values in
+ StatisticAccumulator afterwards
+ \param[in] n number of time-slices
+ \param[in] sa StatisticAccumulator*/
StatisticsBase::OutputProxy<Statistics> output(unsigned n = 1u);