Advertisement
Guest User

WarmUp 4.3

a guest
Dec 3rd, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1. -- Updated WarmUp by Cybeloras of Mal'Ganis. Uses debugprofile start/stop instead of GetTime because it seems that GetTime() is not updated during loading screens anymore.
  2.  
  3. collectgarbage("stop")
  4. collectgarbage("collect")
  5. local initmem = collectgarbage("count")
  6. local longesttime, biggestmem, totalmem, totalgarbage, mostgarbage, gctime = 0, 0, 0, 0, 0, 0
  7. local totaltime = 0
  8. local eventcounts, combatframe = {}, ChatFrame2
  9. local eventargs = {}
  10. local threshtimes, threshmems = {1.0, 0.5, 0.1}, {1000, 500, 100}
  11. local threshcolors = {"|cffff0000", "|cffff8000", "|cffffff80", "|cff80ff80"}
  12. local sv, intransit, reloading, longestaddon, biggestaddon, varsloadtime, logging, mostgarbageaddon, leftworld
  13. local memstack = {initmem}
  14.  
  15. local usecombatframe = false
  16.  
  17. local timerIsLocked
  18. local function start()
  19.     if timerIsLocked then
  20.     --  WarmupChatFrame:AddMessage("ATTEMPTED TO START TIMER WHILE LOCKED")
  21.     --  WarmupChatFrame:AddMessage(debugstack())
  22.     end
  23.    
  24.     timerIsLocked = true
  25.     debugprofilestart()
  26. end
  27.  
  28. local function stop()
  29.     if not timerIsLocked then
  30.     --  WarmupChatFrame:AddMessage("ATTEMPTED TO STOP TIMER WHILE UNLOCKED")
  31.     --  WarmupChatFrame:AddMessage(debugstack())
  32.     end
  33.    
  34.     timerIsLocked = false
  35.     return debugprofilestop()
  36. end
  37.  
  38. start()
  39.  
  40.     --[[ (insert a space between the dashes and brackets)
  41.     LoadAddOn("Blizzard_DebugTools")
  42.     EventTraceFrame_HandleSlashCmd ("")
  43.     EVENT_TRACE_MAX_ENTRIES = 10000
  44.     --]]
  45.  
  46. local frame = CreateFrame("Frame", "WarmupFrame", UIParent)
  47. Warmup = {}
  48.  
  49. frame:SetScript("OnEvent", function(self, event, ...)
  50.     if eventcounts then
  51.         eventcounts[event] = (eventcounts[event] or 0) + 1
  52.         eventargs[event] = max(select("#", ...), eventargs[event] or 0)
  53.     end
  54.     if Warmup[event] then Warmup[event](Warmup, ...) end
  55. end)
  56.  
  57.  
  58. local function GetThreshColor(set, value)
  59.     local t = set == "mem" and threshmems or threshtimes
  60.     for i,v in pairs(t) do
  61.         if value >= v then return threshcolors[i] end
  62.     end
  63.     return threshcolors[4]
  64. end
  65.  
  66.  
  67. local function PutOut(txt, color, time, mem, gc)
  68.     local outstr = (time and string.format("%.3f sec | ", time) or "")..
  69.         color.. txt.. (mem and string.format(" (%d KiB", mem) or "")..
  70.         (gc and string.format(" - %d KiB)", gc) or mem and ")" or "")
  71.  
  72.     if usecombatframe then combatframe:AddMessage(outstr) end
  73.     WarmupChatFrame:AddMessage(outstr)
  74. end
  75.  
  76.  
  77. local function PutOutAO(name, time, mem, garbage)
  78.     WarmupChatFrame:AddMessage(string.format("%s%.3f sec|r | %s (%s%d KiB|r - %s%d KiB|r)", GetThreshColor("time", time), time,
  79.         name, GetThreshColor("mem", mem), mem, GetThreshColor("mem", garbage), garbage))
  80.     return string.format("%.3f sec | %s (%d KiB - %d KiB)", time, name, mem, garbage)
  81. end
  82.  
  83.  
  84.  
  85. do
  86.     local loadandpop = function(...)
  87.         local newm, newt = table.remove(memstack)
  88.         local oldm, oldt = table.remove(memstack)
  89.         local origm, origt = table.remove(memstack)
  90.         table.insert(memstack, origm + newm - oldm)
  91.         return ...
  92.     end
  93.     local lao = LoadAddOn
  94.     LoadAddOn = function (...)
  95.         if timerIsLocked then
  96.             stop() -- stop any runaway timers
  97.         end
  98.        
  99.         start()
  100.         collectgarbage("collect")
  101.         gctime = gctime + stop()/1000
  102.        
  103.         local newmem = collectgarbage("count")
  104.         table.insert(memstack, newmem)
  105.         table.insert(memstack, newmem)
  106.         start() -- start the timer for ADDON_LOADED to finish
  107.         return loadandpop(lao(...))
  108.     end
  109. end
  110.  
  111.  
  112. function Warmup:OnLoad()
  113.     table.insert(UISpecialFrames, "WarmupOutputFrame")
  114.     frame:RegisterAllEvents()
  115. end
  116.  
  117. do
  118.     for i=1,GetNumAddOns() do
  119.         if IsAddOnLoaded(i) then
  120.             if GetAddOnInfo(i) ~= "!!Warmup" then
  121.                 if usecombatframe then combatframe:AddMessage("Addon loaded before Warmup: ".. GetAddOnInfo(i)) end
  122.                 WarmupChatFrame:AddMessage("Addon loaded before Warmup: ".. GetAddOnInfo(i))
  123.             end
  124.         end
  125.     end
  126. end
  127.  
  128. function Warmup:Init()
  129.     if not WarmupSV then WarmupSV = {} end
  130.     sv = WarmupSV
  131.     sv.addoninfo = {}
  132. end
  133.  
  134.  
  135. function Warmup:DumpEvents()
  136.     local sortt = {}
  137.     for ev,val in pairs(eventcounts) do table.insert(sortt, ev) end
  138.  
  139.     table.sort(sortt)
  140.  
  141.     for i,ev in pairs(sortt) do
  142.         WarmupChatFrame:AddMessage(string.format(threshcolors[1].."%d|r (%d) | %s%s|r", eventcounts[ev], eventargs[ev], threshcolors[4], ev))
  143.     end
  144.     WarmupChatFrame:AddMessage("------------")
  145. end
  146.  
  147.  
  148. function Warmup:ADDON_LOADED(addon)
  149.     local addonmem = collectgarbage("count")
  150.     local lastmem = table.remove(memstack)
  151.     local lasttime = stop()/1000
  152.     local diff = addonmem - lastmem
  153.    
  154.     totaltime = totaltime + lasttime
  155.    
  156.     start()
  157.     collectgarbage("collect")
  158.     gctime = gctime + stop()/1000
  159.    
  160.     local gcmem = collectgarbage("count")
  161.     local garbage = addonmem - gcmem
  162.  
  163.     if not sv then self:Init() end
  164.  
  165.     table.insert(sv.addoninfo, PutOutAO(addon, lasttime, diff - garbage, garbage))
  166.  
  167.     if lasttime > longesttime then
  168.         longesttime = lasttime
  169.         longestaddon = addon
  170.     end
  171.     if (diff - garbage) > biggestmem then
  172.         biggestmem = diff - garbage
  173.         biggestaddon = addon
  174.     end
  175.     if garbage > mostgarbage then
  176.         mostgarbage = garbage
  177.         mostgarbageaddon = addon
  178.     end
  179.     totalgarbage = totalgarbage + garbage
  180.     totalmem = totalmem + diff
  181.     table.insert(memstack, gcmem)
  182.     start()
  183. end
  184.  
  185.  
  186. function Warmup:VARIABLES_LOADED()
  187.     if varsloadtime then return end
  188.     stop() -- stop the timer (it is still running from the last addon loaded
  189.    
  190.     start()
  191.     collectgarbage("collect")
  192.     gctime = gctime + stop()/1000
  193.  
  194.     local lastmem = collectgarbage("count")
  195.  
  196.     varsloadtime = GetTime()
  197.     PutOut("Addon Loadup", threshcolors[4], totaltime, lastmem - initmem, totalgarbage)
  198.     PutOut("Warmup's Garbage Collection", threshcolors[4], gctime)
  199.     PutOut("Longest addon: ".. longestaddon, threshcolors[2], longesttime)
  200.     PutOut("Biggest addon: ".. biggestaddon, threshcolors[2], nil, biggestmem)
  201.     PutOut("Most Garbage: "..mostgarbageaddon, threshcolors[2], nil, mostgarbage)
  202.  
  203.     frame:RegisterEvent("PLAYER_LOGIN")
  204.  
  205.     SlashCmdList["RELOAD"] = ReloadUI
  206.     SLASH_RELOAD1 = "/rl"
  207.  
  208.     SlashCmdList["RELOADNODISABLE"] = function()
  209.         sv.time = GetTime()
  210.         reloading = true
  211.         EnableAddOn("!!Warmup")
  212.         ReloadUI()
  213.     end
  214.     SLASH_RELOADNODISABLE1 = "/rlnd"
  215.  
  216.     SlashCmdList["WARMUP"] = function()
  217.         if WarmupOutputFrame:IsVisible() then WarmupOutputFrame:Hide()
  218.         else WarmupOutputFrame:Show() end
  219.     end
  220.  
  221.     SLASH_WARMUP1 = "/wu"
  222.     SLASH_WARMUP2 = "/warmup"
  223.  
  224.     collectgarbage("restart")
  225.     DisableAddOn("!!Warmup")
  226.     start()
  227. end
  228.  
  229.  
  230. function Warmup:PLAYER_LOGIN()
  231.     logging = true
  232.     frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  233. end
  234.  
  235.  
  236. function Warmup:PLAYER_ENTERING_WORLD()
  237.     if logging then
  238.         local entrytime = stop()/1000
  239.  
  240.         PutOut("World entry", threshcolors[4], entrytime)
  241.  
  242.         PutOut("Total time", threshcolors[4], entrytime + totaltime + gctime)
  243.  
  244.         sv.time = nil
  245.         varsloadtime = nil
  246.     elseif leftworld then
  247.         PutOut("Zoning", threshcolors[4], stop()/1000)
  248.         leftworld = nil
  249.     end
  250.    
  251.     logging = nil
  252.     frame:RegisterAllEvents()
  253.     frame:UnregisterEvent("PLAYER_LOGIN")
  254.     frame:UnregisterEvent("PLAYER_LOGOUT")
  255.     frame:UnregisterEvent("PLAYER_ENTERING_WORLD")
  256.  
  257.     self:DumpEvents()
  258.     eventcounts = nil
  259. end
  260.  
  261.  
  262. function Warmup:PLAYER_LEAVING_WORLD()
  263.     --sv.time = GetTime()
  264.     frame:RegisterAllEvents()
  265.     frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  266.     frame:RegisterEvent("PLAYER_LOGOUT")
  267.  
  268.     eventcounts = {}
  269.     if timerIsLocked then
  270.         stop() -- stop any runaway timers
  271.     end
  272.     start()
  273.     leftworld = true
  274. end
  275.  
  276.  
  277. function Warmup:PLAYER_LOGOUT()
  278.     if not reloading then sv.time = nil end
  279. end
  280.  
  281.  
  282. Warmup:OnLoad()
  283.  
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement