2 using System.Diagnostics;
4 namespace OpenSim.Framework
12 : base(windowSize, numBuckets)
16 protected override long GetZero() {
return 0; }
18 protected override long Add(
long a,
long b) {
return a + b; }
28 : base(windowSize, numBuckets)
34 long ticks = timer.ElapsedTicks;
41 return TimeSpan.FromMilliseconds((GetSum() * 1000) / Stopwatch.Frequency);
46 struct MetricsBucket<T>
64 public abstract class MetricsCollector<T>
66 private int bucketSize;
70 private int NumBuckets {
get {
return buckets.Length; } }
85 protected abstract T GetZero();
90 protected abstract T Add(T a, T b);
101 bucketSize = windowSize / numBuckets;
108 ZeroBuckets(0, NumBuckets);
109 curBucketGlobal = GetNow() / bucketSize;
110 totalSum = GetZero();
118 int curBucket = (int)(curBucketGlobal % NumBuckets);
119 buckets[curBucket].value = Add(buckets[curBucket].value, sample);
120 buckets[curBucket].count++;
122 totalSum = Add(totalSum, sample);
140 private long GetNow()
142 return DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
148 private void ZeroBuckets(
int offset,
int num)
150 for (
int i = 0; i < num; i++)
152 buckets[offset + i].value = GetZero();
153 buckets[offset + i].count = 0;
165 private void MoveWindow()
167 long newBucketGlobal = GetNow() / bucketSize;
168 long bucketsDistance = newBucketGlobal - curBucketGlobal;
170 if (bucketsDistance == 0)
176 if (bucketsDistance >= NumBuckets)
183 int curBucket = (int)(curBucketGlobal % NumBuckets);
184 int newBucket = (int)(newBucketGlobal % NumBuckets);
188 int numToClear = (int)bucketsDistance;
190 if (curBucket < NumBuckets - 1)
193 int num = Math.Min((int)bucketsDistance, NumBuckets - (curBucket + 1));
194 ZeroBuckets(curBucket + 1, num);
201 ZeroBuckets(0, numToClear);
205 curBucketGlobal = newBucketGlobal;
210 private void RecalcTotal()
212 totalSum = GetZero();
215 for (
int i = 0; i < NumBuckets; i++)
217 totalSum = Add(totalSum, buckets[i].value);
218 totalCount += buckets[i].count;
A MetricsCollector for 'long' values.
void AddSample(Stopwatch timer)
MetricsCollectorLong(int windowSize, int numBuckets)
T GetSum()
Returns the total values in the collection window.
A MetricsCollector for time spans.
MetricsCollector(int windowSize, int numBuckets)
Creates a MetricsCollector.
override long Add(long a, long b)
override long GetZero()
Returns the default (zero) value.
MetricsCollectorTime(int windowSize, int numBuckets)