missing project/build files
[client-tools.git] / src / external / 3rd / library / soePlatform / Base / Statistics.h
blob9febe23dab2760a4fb1f76f07d7fc2d92ed65fe3
1 #ifndef STATISTICS_H
2 #define STATISTICS_H
4 #include <time.h>
5 #include "Platform.h"
7 namespace Base
9 template <class TYPE, uint32 COUNT>
10 class CStatistic
12 public:
13 CStatistic(uint32 freq=0) :
14 mSampleFrequency(freq),
15 mLastSampleTime(0),
16 mSample(0),
17 mSampleTotal(0),
18 mAggregateTotal(0),
19 mAggregateMaximum(0),
20 mAggregateMinimum(0),
21 mAverageTotal(0),
22 mAverageIndex(0),
23 mAverageCount(0)
27 bool Sample(TYPE value)
29 bool commit = false;
31 if (!mLastSampleTime)
32 mLastSampleTime = time(NULL);
34 if (!mSampleFrequency || mLastSampleTime + mSampleFrequency < (unsigned)time(NULL))
36 mSampleTotal++;
37 mAggregateTotal += value;
38 if (value > mAggregateMaximum)
39 mAggregateMaximum = value;
40 if (value < mAggregateMinimum)
41 mAggregateMinimum = value;
43 if (mAverageIndex == COUNT)
44 mAverageIndex = 0;
45 if (mAverageCount > COUNT)
46 mAverageTotal -= mAverageData[mAverageIndex];
48 mAverageData[mAverageIndex++] = value;
49 mAverageTotal += value;
51 if (mAverageCount < COUNT)
52 mAverageCount++;
54 commit = true;
55 mSample = 0;
58 mSample += value;
60 return commit;
63 TYPE GetSample(uint32 age = 0)
65 if (age > mAverageCount)
66 return 0;
68 uint32 index = mAverageIndex;
69 if (age > index)
70 index = COUNT - (age - index);
71 else
72 index -= age;
74 return mAverageData[index];
77 double GetAverage()
79 return (double)mAverageTotal/mAverageCount;
82 double GetAggregateAverage()
84 return (double)mAggregateTotal/mSampleTotal;
87 TYPE GetMaximum()
89 return mAggregateMaximum;
92 TYPE GetMinimum()
94 return mAggregateMinimum;
97 private:
98 uint32 mSampleFrequency;
99 time_t mLastSampleTime;
100 TYPE mSample;
101 uint32 mSampleTotal;
102 TYPE mAggregateTotal;
103 TYPE mAggregateMaximum;
104 TYPE mAggregateMinimum;
106 TYPE mAverageData[COUNT];
107 TYPE mAverageTotal;
108 uint32 mAverageIndex;
109 uint32 mAverageCount;
112 class CStatisticTimer
114 public:
115 CStatisticTimer(bool running = true);
117 void Start() { if (mRunning) return; mStart = getTimer(); mRunning = true; }
118 void Stop() { if (!mRunning) return; mTotal += getTimer()-mStart; mRunning = false; }
119 void Reset() { mRunning = false; mTotal = 0; }
121 double GetTime();
122 uint64 GetFraction(uint32 fraction=1000);
124 private:
125 uint64 mTotal;
126 uint64 mStart;
127 bool mRunning;
133 #endif // STATISTICS_H