-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfTimer.cs
More file actions
153 lines (143 loc) · 4.74 KB
/
PerfTimer.cs
File metadata and controls
153 lines (143 loc) · 4.74 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JLGraphics
{
public static class PerfTimer
{
class Watcher
{
public string name { get; } = "";
public Watcher(string name)
{
this.name = name;
}
public Stopwatch stopwatch = new Stopwatch();
public double totalMs = 0;
public int samples = 0;
public int calls = 0;
public bool sampleflag = true;
public int QueryID;
public void Reset()
{
samples = 0;
totalMs = 0;
calls = 0;
QueryID = 0;
sampleflag = true;
stopwatch.Stop();
stopwatch.Reset();
}
}
static public uint Intervals
{
get => interval;
set
{
if(value == 0)
{
value = 1;
}
interval = value;
}
}
static uint interval = 500;
static uint timer = 0;
static Dictionary<string, Watcher> timerDic = new Dictionary<string, Watcher>();
static List<Watcher> watchers = new List<Watcher>();
static Stack<Watcher> startedWatcher = new Stack<Watcher>();
static public bool Enable { get; set; } = true;
public static void Start(string name)
{
if (!Enable) return;
GL.Finish();
if (timerDic.TryGetValue(name, out var watch))
{
if (watch.stopwatch.IsRunning)
{
throw new Exception("PerfTimer for: " + name + " is already running");
}
//var query = GL.GenQuery();
//GL.BeginQuery(QueryTarget.TimeElapsed, query);
//watch.QueryID = query;
watch.stopwatch.Start();
startedWatcher.Push(watch);
}
else
{
var watcher = new Watcher(name);
//var query = GL.GenQuery();
//GL.BeginQuery(QueryTarget.TimeElapsed, query);
//watcher.QueryID = query;
watcher.stopwatch.Start();
watchers.Add(watcher);
timerDic.Add(name, watcher);
startedWatcher.Push(watcher);
}
}
public static void Stop()
{
if (!Enable) return;
GL.Finish();
var startedWatcher = PerfTimer.startedWatcher.Pop();
//GL.EndQuery(QueryTarget.TimeElapsed);
//bool available = false;
//while (!available)
//{
// GL.GetQueryObject(startedWatcher.QueryID, GetQueryObjectParam.QueryResultAvailable, out int res);
// available = res == 1;
//}
//long queryTime = 0;
//GL.GetQueryObject(startedWatcher.QueryID, GetQueryObjectParam.QueryResult, out queryTime);
//GL.DeleteQuery(startedWatcher.QueryID);
startedWatcher.stopwatch.Stop();
startedWatcher.totalMs += startedWatcher.stopwatch.Elapsed.TotalMilliseconds;
startedWatcher.stopwatch.Reset();
startedWatcher.calls++;
if (startedWatcher.sampleflag == true)
{
startedWatcher.samples++;
startedWatcher.sampleflag = false;
}
}
internal static void ResetTimers(bool printResults)
{
if (!Enable) return;
timer++;
bool skip = true;
if (timer >= Intervals)
{
timer = 0;
skip = false;
JLUtility.Debug.Log("--------------------------");
}
for (int i = 0; i < watchers.Count; i++)
{
watchers[i].sampleflag = true;
if (skip)
{
continue;
}
if (watchers[i].samples == 0)
continue;
if (printResults)
{
float time = (float)(watchers[i].totalMs / watchers[i].samples);
float calls = (float)(watchers[i].calls / watchers[i].samples);
JLUtility.Debug.Log(calls + " " + watchers[i].name + ": " + time + "ms");
}
watchers[i].Reset();
}
}
internal static void Clear()
{
timerDic.Clear();
watchers.Clear();
startedWatcher.Clear();
}
}
}