Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions [web]/performancebrowser/performancebrowser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,56 @@
--
--

-- Lua time recordings config
g_LuaTimingRecordings = {
Enabled = true,
Frequency = 2000, -- in milliseconds
HistoryLength = 300, -- number of records to keep
HighCPUResourcesAmount = 10, -- percentage threshold
}

-- Global variable to store high usage resources similar to IPB
g_HighUsageResources = {}

-- Browser update
function setQuery ( counter, user, target, category, options, filter, showClients )
local viewer = getViewer(user)
return viewer:setQuery ( counter, target, category, options, filter, showClients )
end

-- Date/time formatting function
local function getDateTimeString()
local time = getRealTime()
local weekday = ({"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"})[time.weekday + 1]
-- Weekday, DD.MM.YYYY, hh:mm:ss
return ("%s, %02d.%02d.%d, %02d:%02d:%02d"):format(weekday, time.monthday, time.month + 1, time.year + 1900, time.hour, time.minute, time.second)
end

-- Save high CPU resources function (based on IPB alarm.lua)
function saveHighCPUResources()
local columns, rows = getPerformanceStats("Lua timing")

if not rows then
return
end

for index, row in pairs(rows) do
local usageText = row[2]:gsub("[^0-9%.]", "")
local usage = math.floor(tonumber(usageText) or 0)

if (usage > g_LuaTimingRecordings.HighCPUResourcesAmount) then
-- Record this high usage to table
table.insert(g_HighUsageResources, 1, {row[1], row[2], getDateTimeString()})

-- Make sure it won't get too big
if #g_HighUsageResources > g_LuaTimingRecordings.HistoryLength then
table.remove(g_HighUsageResources, g_LuaTimingRecordings.HistoryLength)
end
end
end
end

-- Start monitoring timer
if (g_LuaTimingRecordings.Enabled) then
setTimer(saveHighCPUResources, g_LuaTimingRecordings.Frequency, 0)
end
33 changes: 33 additions & 0 deletions [web]/performancebrowser/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ end
--
---------------------------------------------------------------------------
function Target:getPerformanceStats( username, queryCategoryName, queryOptionsText, queryFilterText )
-- Handle our custom Lua time recordings category
if queryCategoryName == "Lua time recordings" then
return self:getLuaTimingRecordings( queryFilterText )
end

if self.bIsServer then
local a, b = getPerformanceStats ( queryCategoryName, queryOptionsText, queryFilterText )
return a, b, true
Expand Down Expand Up @@ -155,3 +160,31 @@ addEventHandler('onNotifyStats', resourceRoot,
end
)

---------------------------------------------------------------------------
--
-- Target:getLuaTimingRecordings()
--
-- Get recorded high usage Lua timing data
--
---------------------------------------------------------------------------
function Target:getLuaTimingRecordings( queryFilterText )
local columns = {"Resource", "CPU Usage", "Recorded Time"}
local rows = {}

-- Get the global high usage resources if it exists
if g_HighUsageResources then
for i, recording in ipairs(g_HighUsageResources) do
local resourceName = recording[1]
local cpuUsage = recording[2]
local recordedTime = recording[3]

-- Apply filter if specified
if not queryFilterText or queryFilterText == "" or string.find(string.lower(resourceName), string.lower(queryFilterText), 1, true) then
table.insert(rows, {resourceName, cpuUsage, recordedTime})
end
end
end

return columns, rows, true
end

3 changes: 3 additions & 0 deletions [web]/performancebrowser/viewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ function Viewer:getCategoriesRaw ()
local categories = {}
for _,row in ipairs(rowList) do
table.insert( categories, row[1] )
if ( row[1] == "Lua timing" ) then -- Add our custom Lua time recordings category
table.insert( categories, "Lua time recordings" )
end
end
return categories
end
Expand Down