-
Notifications
You must be signed in to change notification settings - Fork 0
/
SummaryStats.cs
56 lines (50 loc) · 1.34 KB
/
SummaryStats.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ChaosLoad
{
public class SummaryStats : IDisposable
{
private readonly int maxAgeMs = 10000;
private object objLock = new object();
private List<DateTime> TimeStamps { get; set; } = new List<DateTime>();
private Thread PruneThread;
private bool Stopping = false;
public SummaryStats(int maxAgeMs)
{
this.maxAgeMs = maxAgeMs;
PruneThread = new Thread(() =>
{
while (!Stopping)
{
lock (objLock)
{
TimeStamps.RemoveAll(x => x < DateTime.UtcNow.AddMilliseconds(-maxAgeMs));
}
Thread.Sleep(5000);
}
});
PruneThread.Start();
}
public int Count()
{
lock (objLock)
{
return TimeStamps.Count(x => x >= DateTime.UtcNow.AddMilliseconds(-maxAgeMs));
}
}
public void Log()
{
lock (objLock)
{
TimeStamps.Add(DateTime.UtcNow);
}
}
public void Dispose()
{
Stopping = true;
PruneThread.Join();
}
}
}