-
Notifications
You must be signed in to change notification settings - Fork 5
/
profiler.gd
266 lines (216 loc) · 7.24 KB
/
profiler.gd
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
tool
class_name Profiler
extends Node
# Measures and records timings.
const DEFAULT_THREAD_ID := ""
# A mapping from thread ID to Stopwatch.
# Dictionary<String, Stopwatch>
var _stopwatches := {}
# A mapping from thread ID, to metric ID, to a list of duration values, in
# milliseconds.
# Dictionary<String, Dictionary<String, Array<float>>>
var _timings := {}
# A mapping from thread ID, to metric ID, to a count.
# Dictionary<String, Dictionary<String, int>>
var _counts := {}
# This can be useful if you want to list metrics in a report even if they
# weren't actually used.
# Array<String>
var _preregistered_metric_keys := []
func _init() -> void:
Sc.logger.on_global_init(self, "Profiler")
init_thread(DEFAULT_THREAD_ID)
func init_thread(thread_id: String) -> void:
_stopwatches[thread_id] = Stopwatch.new()
_timings[thread_id] = {}
_counts[thread_id] = {}
func start(
metric: String,
thread_id := DEFAULT_THREAD_ID) -> void:
if !Sc.metadata.is_profiler_enabled:
return
_stopwatches[thread_id].start(metric)
func stop(
metric: String,
thread_id := DEFAULT_THREAD_ID,
records := true,
additional_timings_storage = null) -> float:
if !Sc.metadata.is_profiler_enabled:
return -1.0
var duration: float = _stopwatches[thread_id].stop(metric)
if records:
var timings_for_thread: Dictionary = _timings[thread_id]
if !timings_for_thread.has(metric):
timings_for_thread[metric] = []
timings_for_thread[metric].push_back(duration)
if additional_timings_storage != null:
if !additional_timings_storage.has(metric):
additional_timings_storage[metric] = []
additional_timings_storage[metric].push_back(duration)
return duration
func stop_with_optional_metadata(
metric: String,
thread_id := DEFAULT_THREAD_ID,
records_profile_or_metadata_container = null) -> float:
if records_profile_or_metadata_container != null:
if records_profile_or_metadata_container is Object and \
records_profile_or_metadata_container.timings != null:
return stop(
metric,
thread_id,
records_profile_or_metadata_container.records_profile,
records_profile_or_metadata_container.timings)
else:
return stop(
metric,
thread_id,
records_profile_or_metadata_container)
else:
return stop(
metric,
thread_id)
func increment_count(
metric: String,
thread_id := DEFAULT_THREAD_ID,
metadata_container = null) -> int:
var counts_for_thread: Dictionary = _counts[thread_id]
if !counts_for_thread.has(metric):
counts_for_thread[metric] = 0
counts_for_thread[metric] += 1
if metadata_container != null and \
metadata_container.records_profile:
if !metadata_container.counts.has(metric):
metadata_container.counts[metric] = 0
metadata_container.counts[metric] += 1
return counts_for_thread[metric]
func get_timing(
metric: String,
metadata_container = null) -> float:
assert(Sc.metadata.is_profiler_enabled)
if metadata_container != null:
var list: Array = metadata_container.timings[metric]
assert(list.size() == 1)
return list[0]
else:
assert(_timings.has(DEFAULT_THREAD_ID))
var list: Array = _timings[DEFAULT_THREAD_ID][metric]
assert(list.size() == 1)
return list[0]
func get_timing_list(
metric: String,
metadata_container = null) -> Array:
assert(Sc.metadata.is_profiler_enabled)
if metadata_container != null:
var timings: Dictionary = metadata_container.timings
return timings[metric] if \
timings.has(metric) else \
[]
else:
var timings := []
for thread_id in _timings:
if _timings[thread_id].has(metric):
Sc.utils.concat(
timings,
_timings[thread_id][metric])
return timings
func get_mean(
metric: String,
metadata_container = null) -> float:
assert(Sc.metadata.is_profiler_enabled)
var count := get_count(
metric,
metadata_container)
if count == 0:
return INF
else:
return get_sum(
metric,
metadata_container) / count
func get_min(
metric: String,
metadata_container = null) -> float:
assert(Sc.metadata.is_profiler_enabled)
if get_count(
metric,
metadata_container) == 0:
return INF
else:
return get_timing_list(
metric,
metadata_container).min()
func get_max(
metric: String,
metadata_container = null) -> float:
assert(Sc.metadata.is_profiler_enabled)
if get_count(
metric,
metadata_container) == 0:
return INF
else:
return get_timing_list(
metric,
metadata_container).max()
func get_sum(
metric: String,
metadata_container = null) -> float:
assert(Sc.metadata.is_profiler_enabled)
var sum := 0.0
for timing in get_timing_list(
metric,
metadata_container):
sum += timing
return sum
func get_count(
metric: String,
metadata_container = null) -> int:
assert(Sc.metadata.is_profiler_enabled)
var is_timing := is_timing(
metric,
metadata_container)
var is_count := is_count(
metric,
metadata_container)
assert(!is_timing or !is_count)
if metadata_container != null:
return metadata_container.counts[metric] if \
is_count else \
get_timing_list(
metric,
metadata_container).size()
else:
if is_count:
var count := 0
for thread_id in _counts:
if _counts[thread_id].has(metric):
count += _counts[thread_id][metric]
return count
else:
return get_timing_list(metric).size()
func is_timing(
metric: String,
metadata_container = null) -> bool:
if metadata_container != null:
return metadata_container.timings.has(metric)
else:
for thread_id in _timings:
if _timings[thread_id].has(metric):
return true
return false
func is_count(
metric: String,
metadata_container = null) -> bool:
if metadata_container != null:
return metadata_container.counts.has(metric)
else:
for thread_id in _counts:
if _counts[thread_id].has(metric):
return true
return false
func get_active_metric_keys() -> Array:
var keys := _timings.keys()
Sc.utils.concat(keys, _counts.keys())
return keys
func preregister_metric_keys(metric_keys: Array) -> void:
Sc.utils.concat(_preregistered_metric_keys, metric_keys)
func get_preregistered_metric_keys() -> Array:
return _preregistered_metric_keys