Client LuaCsForBarotrauma
PerformanceCounter.cs
1 using System.Collections.Generic;
2 using System.Diagnostics;
3 using System.Linq;
4 
5 namespace Barotrauma
6 {
7  public class PerformanceCounter
8  {
9  private readonly object mutex = new object();
10 
11  public double AverageFramesPerSecond { get; private set; }
12  public double CurrentFramesPerSecond { get; private set; }
13 
14  public const int MaximumSamples = 10;
15 
16  private readonly Queue<double> sampleBuffer = new Queue<double>();
17 
18  public class TickInfo
19  {
20  public Queue<long> ElapsedTicks { get; set; } = new Queue<long>();
21  public long AvgTicksPerFrame { get; set; }
22  }
23 
24  private readonly Dictionary<string, Queue<long>> elapsedTicks = new Dictionary<string, Queue<long>>();
25  private readonly Dictionary<string, long> avgTicksPerFrame = new Dictionary<string, long>();
26 
27 #if CLIENT
28  internal Graph UpdateTimeGraph = new Graph(500), DrawTimeGraph = new Graph(500);
29 #endif
30 
31  private readonly List<string> tempSavedIdentifiers = new List<string>();
32  public IReadOnlyList<string> GetSavedIdentifiers
33  {
34  get
35  {
36  lock (mutex)
37  {
38  tempSavedIdentifiers.Clear();
39  tempSavedIdentifiers.AddRange(avgTicksPerFrame.Keys);
40  }
41  return tempSavedIdentifiers;
42  }
43  }
44 
45  public void AddElapsedTicks(string identifier, long ticks)
46  {
47  lock (mutex)
48  {
49  if (!elapsedTicks.ContainsKey(identifier)) { elapsedTicks.Add(identifier, new Queue<long>()); }
50  elapsedTicks[identifier].Enqueue(ticks);
51 
52  if (elapsedTicks[identifier].Count > MaximumSamples)
53  {
54  elapsedTicks[identifier].Dequeue();
55  avgTicksPerFrame[identifier] = (long)elapsedTicks[identifier].Average(i => i);
56  }
57  }
58  }
59 
60  public float GetAverageElapsedMillisecs(string identifier)
61  {
62  long ticksPerFrame = 0;
63  lock (mutex)
64  {
65  avgTicksPerFrame.TryGetValue(identifier, out ticksPerFrame);
66  }
67  return ticksPerFrame * 1000.0f / Stopwatch.Frequency;
68  }
69 
70  public bool Update(double deltaTime)
71  {
72  if (deltaTime == 0.0f) { return false; }
73 
74  CurrentFramesPerSecond = 1.0 / deltaTime;
75 
76  sampleBuffer.Enqueue(CurrentFramesPerSecond);
77 
78  if (sampleBuffer.Count > MaximumSamples)
79  {
80  sampleBuffer.Dequeue();
81  AverageFramesPerSecond = sampleBuffer.Average(i => i);
82  }
83  else
84  {
86  }
87 
88  return true;
89  }
90  }
91 }
float GetAverageElapsedMillisecs(string identifier)
IReadOnlyList< string > GetSavedIdentifiers
void AddElapsedTicks(string identifier, long ticks)
bool Update(double deltaTime)