Guest User

bns2_server_performance.lua

a guest
Nov 11th, 2013
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local lastFrameTime = 0
  2. local lastAvgUpdate = 0
  3. local longestFrameLength = 0
  4. local rollingAvg = 30
  5.  
  6.  
  7. local function OnUpdateServer()
  8.  
  9.     local time = Shared.GetTime()
  10.  
  11.     local frameLength = time - lastFrameTime
  12.  
  13.     if frameLength > longestFrameLength then
  14.         longestFrameLength = frameLength
  15.     end
  16.  
  17.     if time > lastAvgUpdate + 0.1 then
  18.         local longestFrameFPS = 1 / longestFrameLength
  19.  
  20.         // map lower fps to even lower fps using dark magic
  21.         // (this will become a percentage/score, this "wrong" value will not be presented)
  22.         longestFrameFPS = 30 + (25 * (-30 + longestFrameFPS) / longestFrameFPS)
  23.  
  24.         rollingAvg = (rollingAvg * 0.995) + (0.005 * longestFrameFPS)
  25.  
  26.         longestFrameLength = 0
  27.         lastAvgUpdate = time
  28.     end
  29.  
  30.     lastFrameTime = time
  31.  
  32. end
  33. Event.Hook("UpdateServer", OnUpdateServer)
  34.  
  35.  
  36. local function GetServerPerf()
  37.     local fps = 30
  38.  
  39.     if Shared.GetTime() < 60 then
  40.         fps = Server.GetFrameRate()
  41.     else
  42.         fps = rollingAvg
  43.     end
  44.  
  45.     return math.max(0, math.min(100, math.floor(((fps / 30) * 100) + 0.5)))
  46. end
  47.  
  48.  
  49. local kKeyValueUpdateRate = 5
  50. local lastKeyValueUpdateTime = 0
  51.  
  52. local function UpdateServerConfig()
  53.  
  54.     if Shared.GetSystemTime() - lastKeyValueUpdateTime >= kKeyValueUpdateRate then
  55.  
  56.         Server.SetKeyValue("performance", ToString(GetServerPerf()))
  57.  
  58.         lastKeyValueUpdateTime = Shared.GetSystemTime()
  59.  
  60.     end
  61.  
  62. end
  63. Event.Hook("UpdateServer", UpdateServerConfig)
RAW Paste Data