-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnprof.nelua
344 lines (284 loc) · 9.29 KB
/
nprof.nelua
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
--[[ This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/. ]]
-- this library is inspired by jprof: https://github.com/pfirsich/jprof
require 'vector'
require 'os'
require 'string'
require 'math'
require 'stringbuilder'
##[[
local NPROF = NPROF or {}
NPROF.use_colors = NPROF.use_colors or false
NPROF.scale = NPROF.scale or {x = 1, y = 24}
NPROF.draw_list = NPROF.draw_list == nil and true or NPROF.draw_list
NPROF.draw_rects = NPROF.draw_rects == nil and true or NPROF.draw_rects
NPROF.draw_impl = NPROF.draw_impl == nil and false or NPROF.draw_impl
if NPROF.draw_impl then
local di = NPROF.draw_impl
staticassert(di == 'raylib' or di == 'nene', "NPROF doesn't support "..di.." implementation")
end
]]
-- [[ cross-library abstractions [[
## local is_drawing = (NPROF.draw_list or NPROF.draw_rects) and NPROF.draw_impl
## if is_drawing then
-- require
## if NPROF.draw_impl == 'raylib' then
require 'raylib'
## elseif NPROF.draw_impl == 'nene' then
require 'nene'
## end
-- import types
## if NPROF.draw_impl == 'raylib' then
local Color = Color
local Vec2 = Vector2
## elseif NPROF.draw_impl == 'nene' then
local Color = Nene.Color
local Vec2 = Nene.Math.Vec2
## end
-- default colors
## if NPROF.draw_impl == 'raylib' then
local bg_color: Color <const> = BLACK
local default_color: Color <const> = WHITE
## elseif NPROF.draw_impl == 'nene' then
local bg_color: Color <const> = Nene.Palette.black
local default_color: Color <const> = Nene.Palette.white
## end
-- color palette
## if NPROF.use_colors then
## if NPROF.draw_impl == 'raylib' then
local colors: [15]Color <const> = {
WHITE,
ORANGE,
RED,
GOLD,
LIME,
BLUE,
LIGHTGRAY,
PINK,
YELLOW,
GREEN,
RAYWHITE,
SKYBLUE,
PURPLE,
BEIGE,
MAGENTA,
}
## elseif NPROF.draw_impl == 'nene' then
local colors: [4]Color <const> = {
Nene.Palette.white,
Nene.Palette.red,
Nene.Palette.green,
Nene.Palette.yellow,
}
## end
## end
## end
##[[
local draw_state = concept(function(state_attr)
local state_type = state_attr.type
if NPROF.draw_impl == 'nene' then
return state_type.is_nenestate, 'a Nene.CoreState value must be given'
else
return state_type.is_niltype
end
end)
]]
local function draw_rectangle(ext_state: #[draw_state]#, pos: Vec2, size: Vec2, bg_color: Color, outline_color: facultative(Color))
local px, py, sx, sy = math.ifloor(pos.x), math.ifloor(pos.y), math.ifloor(size.x), math.ifloor(size.y)
## if NPROF.draw_impl == 'raylib' then
Raylib.DrawRectangleV(pos, size, bg_color)
## if not outline_color.type.is_niltype then
Raylib.DrawRectangleLines(px, py, sx, sy, outline_color)
## end
## elseif NPROF.draw_impl == 'nene' then
ext_state:render_draw_rect({px, py, sx, sy}, false, bg_color)
## if not outline_color.type.is_niltype then
ext_state:render_draw_rect({px, py, sx, sy}, true, outline_color)
## end
## end
end
local function draw_text(ext_state: #[draw_state]#, text: string, pos: Vec2, color: Color)
local px, py = math.ifloor(pos.x), math.ifloor(pos.y)
## if NPROF.draw_impl == 'raylib' then
Raylib.DrawText(text, px, py, 10, color)
## elseif NPROF.draw_impl == 'nene' then
local text_texture = Nene.TextTexture.new(ext_state, text, color)
text_texture:draw(ext_state, pos, Nene.Palette.white)
text_texture:free()
## end
end
local function get_screen_dimensions(ext_state: #[draw_state]#): (integer, integer)
## if NPROF.draw_impl == 'raylib' then
return Raylib.GetScreenWidth(), Raylib.GetScreenHeight()
## elseif NPROF.draw_impl == 'nene' then
local screen_size = ext_state:get_render_target_dimensions()
return math.ifloor(screen_size.x), math.ifloor(screen_size.y)
## end
end
-- ]] cross-library abstractions ]]
local NProfItem = @record{
title: string,
level: uinteger,
start: number,
_end: number,
}
function NProfItem:__tostring(): string
## if PROF then
local builder: stringbuilder;
builder:write_string(tostring(self.title))
builder:write_string(' { start = ')
builder:write_string(tostring(self.start))
builder:write_string('; _end = ')
builder:write_string(tostring(self._end))
builder:write_string('; level = ')
builder:write_string(tostring(self.level))
builder:write_string('}')
local result = builder:promote()
builder:destroy()
return result
## else
return ''
## end
end
global NProf = @record{
recording_stack: vector(NProfItem),
recorded_stack: vector(NProfItem),
current_level: uinteger,
next_recording_idx: uinteger,
next_recorded_idx: uinteger,
}
function NProf:push(title: string)
## if PROF then
self.current_level = self.current_level + 1
if self.next_recording_idx == #self.recording_stack then
self.recording_stack:push({})
end
self.recording_stack[self.next_recording_idx] = {
title = title,
level = self.current_level,
start = os.clock(),
}
self.next_recording_idx = self.next_recording_idx + 1
## end
end
function NProf:pop()
## if PROF then
local previous_recording_idx = self.next_recording_idx - 1
if self.next_recorded_idx == #self.recorded_stack then
self.recorded_stack:push({})
end
self.recorded_stack[self.next_recorded_idx] = self.recording_stack[previous_recording_idx]
self.recorded_stack[self.next_recorded_idx]._end = os.clock()
self.next_recorded_idx = self.next_recorded_idx + 1
self.next_recording_idx = previous_recording_idx
self.current_level = self.current_level - 1
## end
end
function NProf:reset()
## if PROF then
self.recording_stack:destroy()
self.recorded_stack:destroy()
self.current_level = 0
self.next_recording_idx = 0
self.next_recorded_idx = 0
## end
end
function NProf:report(): string
## if PROF then
local builder: stringbuilder;
for i = 0, < self.next_recorded_idx do
builder:write_string(tostring(self.recorded_stack[i]))
builder:write_string('\n')
end
local result = builder:promote()
builder:destroy()
return result
## else
return ''
## end
end
function NProf:draw(external_state: #[draw_state]#)
## if PROF and is_drawing then
local scale_x: integer <comptime> = #[math.abs(NPROF.scale.x)]#
local scale_y: integer <comptime> = #[NPROF.scale.y]#
local rect_background = bg_color
rect_background.a = 175
local start_min = math.huge
local end_max = 0.0
for i = 0, < #self.recorded_stack do
local r = self.recorded_stack[i]
if r.start < start_min then
start_min = r.start
end
if r._end > end_max then
end_max = r._end
end
end
for i = 0, < #self.recorded_stack do
local color = default_color
## if NPROF.use_colors then
color = colors[i % #colors]
## end
local item = self.recorded_stack[i]
local indentation = string.rep(' ', item.level)
defer indentation:destroy() end
local s = (item.start - start_min) / (end_max - start_min)
local e = (item._end - start_min) / (end_max - start_min)
local p_str = tostring((e - s) * 100)
defer p_str:destroy() end
local percent_sb: stringbuilder;
percent_sb:write_string(p_str)
percent_sb:write_string('%')
local percent = percent_sb:promote()
percent_sb:destroy()
## if NPROF.draw_rects then
local screen_width, screen_height = get_screen_dimensions(external_state)
local pos_x = (item.start - start_min) * scale_x
local size_x = (item._end - item.start) * scale_x
## if NPROF.scale.x > 0 then
pos_x = s * (screen_width * scale_x)
size_x = e * (screen_width * scale_x) - pos_x
## end
local pos: Vec2 = { x = pos_x, y = screen_height - item.level * scale_y }
local size: Vec2 = { x = size_x, y = scale_y }
draw_rectangle(external_state, pos, size, rect_background, color)
draw_text(external_state, item.title, {pos.x + 2, pos.y + 2}, color)
draw_text(external_state, percent, {pos.x + 2, pos.y + scale_y // 2 + 2 }, color)
## end
-- draw text line
## if NPROF.draw_list then
local title_p_sb: stringbuilder;
title_p_sb:write_string(indentation)
title_p_sb:write_string(item.title)
title_p_sb:write_string(': ')
title_p_sb:write_string(percent)
local title_p = title_p_sb:promote()
title_p_sb:destroy()
draw_rectangle(external_state, {10, i * 13}, {title_p.size * 7, 13}, rect_background)
draw_text(external_state, title_p, {10, i * 13}, color)
## end
end
## end
end
## if TEST then
local nprof: NProf = {}
nprof:push'first for'
defer nprof:pop() end
local x: uinteger, y: uinteger = 0, 0
for i = 0, 3 do
local sb: stringbuilder;
sb:write_string('second for (')
sb:write_string(i)
sb:write_string(')')
local s = sb:promote()
sb:destroy()
nprof:push(s)
defer nprof:pop() end
x, y = x + 1, 0
for j = 0, 50000000 do
y = y + 1
end
end
print(nprof:report())
## end