Skip to content

Commit f2304f5

Browse files
committed
Add "Lua time recordings" to web performancebrowser
1 parent 27ead87 commit f2304f5

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

[web]/performancebrowser/performancebrowser.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,56 @@
44
--
55
--
66

7+
-- Lua time recordings config
8+
g_LuaTimingRecordings = {
9+
Enabled = true,
10+
Frequency = 2000, -- in milliseconds
11+
HistoryLength = 300, -- number of records to keep
12+
HighCPUResourcesAmount = 10, -- percentage threshold
13+
}
14+
15+
-- Global variable to store high usage resources similar to IPB
16+
g_HighUsageResources = {}
17+
718
-- Browser update
819
function setQuery ( counter, user, target, category, options, filter, showClients )
920
local viewer = getViewer(user)
1021
return viewer:setQuery ( counter, target, category, options, filter, showClients )
1122
end
23+
24+
-- Date/time formatting function
25+
local function getDateTimeString()
26+
local time = getRealTime()
27+
local weekday = ({"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"})[time.weekday + 1]
28+
-- Weekday, DD.MM.YYYY, hh:mm:ss
29+
return ("%s, %02d.%02d.%d, %02d:%02d:%02d"):format(weekday, time.monthday, time.month + 1, time.year + 1900, time.hour, time.minute, time.second)
30+
end
31+
32+
-- Save high CPU resources function (based on IPB alarm.lua)
33+
function saveHighCPUResources()
34+
local columns, rows = getPerformanceStats("Lua timing")
35+
36+
if not rows then
37+
return
38+
end
39+
40+
for index, row in pairs(rows) do
41+
local usageText = row[2]:gsub("[^0-9%.]", "")
42+
local usage = math.floor(tonumber(usageText) or 0)
43+
44+
if (usage > g_LuaTimingRecordings.HighCPUResourcesAmount) then
45+
-- Record this high usage to table
46+
table.insert(g_HighUsageResources, 1, {row[1], row[2], getDateTimeString()})
47+
48+
-- Make sure it won't get too big
49+
if #g_HighUsageResources > g_LuaTimingRecordings.HistoryLength then
50+
table.remove(g_HighUsageResources, g_LuaTimingRecordings.HistoryLength)
51+
end
52+
end
53+
end
54+
end
55+
56+
-- Start monitoring timer
57+
if (g_LuaTimingRecordings.Enabled) then
58+
setTimer(saveHighCPUResources, g_LuaTimingRecordings.Frequency, 0)
59+
end

[web]/performancebrowser/target.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ end
6969
--
7070
---------------------------------------------------------------------------
7171
function Target:getPerformanceStats( username, queryCategoryName, queryOptionsText, queryFilterText )
72+
-- Handle our custom Lua time recordings category
73+
if queryCategoryName == "Lua time recordings" then
74+
return self:getLuaTimingRecordings( queryFilterText )
75+
end
76+
7277
if self.bIsServer then
7378
local a, b = getPerformanceStats ( queryCategoryName, queryOptionsText, queryFilterText )
7479
return a, b, true
@@ -155,3 +160,32 @@ addEventHandler('onNotifyStats', resourceRoot,
155160
end
156161
)
157162

163+
---------------------------------------------------------------------------
164+
--
165+
-- Target:getLuaTimingRecordings()
166+
--
167+
-- Get recorded high usage Lua timing data
168+
--
169+
---------------------------------------------------------------------------
170+
function Target:getLuaTimingRecordings( queryFilterText )
171+
local columns = {"Resource", "CPU Usage", "Recorded Time"}
172+
local rows = {}
173+
174+
-- Get the global high usage resources if it exists
175+
if g_HighUsageResources then
176+
for i, recording in ipairs(g_HighUsageResources) do
177+
local resourceName = recording[1]
178+
local cpuUsage = recording[2]
179+
local recordedTime = recording[3]
180+
181+
-- Apply filter if specified
182+
if not queryFilterText or queryFilterText == "" or
183+
string.find(string.lower(resourceName), string.lower(queryFilterText), 1, true) then
184+
table.insert(rows, {resourceName, cpuUsage, recordedTime})
185+
end
186+
end
187+
end
188+
189+
return columns, rows, true
190+
end
191+

[web]/performancebrowser/viewer.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ function Viewer:getCategoriesRaw ()
155155
local categories = {}
156156
for _,row in ipairs(rowList) do
157157
table.insert( categories, row[1] )
158+
if ( row[1] == "Lua timing" ) then -- Add our custom Lua time recordings category
159+
table.insert( categories, "Lua time recordings" )
160+
end
158161
end
159162
return categories
160163
end

0 commit comments

Comments
 (0)