PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llsimplestat.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 152 lines | 68 code | 18 blank | 66 comment | 4 complexity | 5bc697beca1d1c678c5d3e814fb2a431 MD5 | raw file
  1/** 
  2 * @file llsimplestat.h
  3 * @brief Runtime statistics accumulation.
  4 *
  5 * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_SIMPLESTAT_H
 28#define LL_SIMPLESTAT_H
 29
 30// History
 31//
 32// The original source for this code is the server repositories'
 33// llcommon/llstat.h file.  This particular code was added after the
 34// viewer/server code schism but before the effort to convert common
 35// code to libraries was complete.  Rather than add to merge issues,
 36// the needed code was cut'n'pasted into this new header as it isn't
 37// too awful a burden.  Post-modularization, we can look at removing
 38// this redundancy.
 39
 40
 41/**
 42 * @class LLSimpleStatCounter
 43 * @brief Just counts events.
 44 *
 45 * Really not needed but have a pattern in mind in the future.
 46 * Interface limits what can be done at that's just fine.
 47 *
 48 * *TODO:  Update/transfer unit tests
 49 * Unit tests:  indra/test/llcommon_llstat_tut.cpp
 50 */
 51class LLSimpleStatCounter
 52{
 53public:
 54	inline LLSimpleStatCounter()		{ reset(); }
 55	// Default destructor and assignment operator are valid
 56
 57	inline void reset()					{ mCount = 0; }
 58
 59	inline void merge(const LLSimpleStatCounter & src)
 60										{ mCount += src.mCount; }
 61	
 62	inline U32 operator++()				{ return ++mCount; }
 63
 64	inline U32 getCount() const			{ return mCount; }
 65		
 66protected:
 67	U32			mCount;
 68};
 69
 70
 71/**
 72 * @class LLSimpleStatMMM
 73 * @brief Templated collector of min, max and mean data for stats.
 74 *
 75 * Fed a stream of data samples, keeps a running account of the
 76 * min, max and mean seen since construction or the last reset()
 77 * call.  A freshly-constructed or reset instance returns counts
 78 * and values of zero.
 79 *
 80 * Overflows and underflows (integer, inf or -inf) and NaN's
 81 * are the caller's problem.  As is loss of precision when
 82 * the running sum's exponent (when parameterized by a floating
 83 * point of some type) differs from a given data sample's.
 84 *
 85 * Unit tests:  indra/test/llcommon_llstat_tut.cpp
 86 */
 87template <typename VALUE_T = F32>
 88class LLSimpleStatMMM
 89{
 90public:
 91	typedef VALUE_T Value;
 92	
 93public:
 94	LLSimpleStatMMM()				{ reset(); }
 95	// Default destructor and assignment operator are valid
 96
 97	/**
 98	 * Resets the object returning all counts and derived
 99	 * values back to zero.
100	 */
101	void reset()
102		{
103			mCount = 0;
104			mMin = Value(0);
105			mMax = Value(0);
106			mTotal = Value(0);
107		}
108
109	void record(Value v)
110		{
111			if (mCount)
112			{
113				mMin = llmin(mMin, v);
114				mMax = llmax(mMax, v);
115			}
116			else
117			{
118				mMin = v;
119				mMax = v;
120			}
121			mTotal += v;
122			++mCount;
123		}
124
125	void merge(const LLSimpleStatMMM<VALUE_T> & src)
126		{
127			if (! mCount)
128			{
129				*this = src;
130			}
131			else if (src.mCount)
132			{
133				mMin = llmin(mMin, src.mMin);
134				mMax = llmax(mMax, src.mMax);
135				mCount += src.mCount;
136				mTotal += src.mTotal;
137			}
138		}
139	
140	inline U32 getCount() const		{ return mCount; }
141	inline Value getMin() const		{ return mMin; }
142	inline Value getMax() const		{ return mMax; }
143	inline Value getMean() const	{ return mCount ? mTotal / mCount : mTotal; }
144		
145protected:
146	U32			mCount;
147	Value		mMin;
148	Value		mMax;
149	Value		mTotal;
150};
151
152#endif // LL_SIMPLESTAT_H