Advertisement
Belzebub

euNtps_monitoring.lua

Dec 11th, 2021 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. -- демон для вывода eu/s и примерный tps на второй экран
  2. -- софт рендерит новый фрейм средне-медианное tps и eu/s каждую секунду
  3. -- credits: Beelzebub#0281 incredible-gmod.ru ??.12.2021
  4.  
  5. -- скачивание: wget https://pastebin.com/raw/guHTzhQG /home/monitoring.lua
  6. -- остановить прогу: /home/monitoring.lua stop
  7. -- автозапуск проги: добавьте путь к файлу в конец /home/.shrc
  8.  
  9. local frametime = 3 -- раз в сколько секунд будет рендериться новый кадр?
  10.  
  11. local event = require("event")
  12. local component = require("component")
  13. local fs = require("filesystem")
  14. local internet = require("internet")
  15. local energy_counter = component.energy_counter
  16. local gpu = component.gpu
  17.  
  18. local eu, last_eu = {}, 0
  19.  
  20. function math.median(t)
  21.     table.sort(t)
  22.  
  23.     if math.fmod(#t, 2) == 0 then
  24.         return (t[#t * 0.5] + t[(#t * 0.5) + 1]) * 0.5
  25.     else
  26.         return t[math.ceil(#t * 0.5)]
  27.     end
  28. end
  29.  
  30. function string.comma(num)
  31.     local formatted = tostring(math.floor(num))
  32.  
  33.     while true do  
  34.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1 %2")
  35.         if k == 0 then
  36.             break
  37.         end
  38.     end
  39.  
  40.     return formatted
  41. end
  42.  
  43. local function getenergy()
  44.     if energy_counter == nil then return 0 end
  45.  
  46.     local cur = energy_counter.getCount()
  47.    
  48.     if #eu >= 10 then table.remove(eu, 1) end
  49.     eu[#eu + 1] = cur - last_eu
  50.     last_eu = cur
  51.  
  52.     return #eu == 1 and eu[1] or math.median(eu)
  53. end
  54.  
  55. local function getSecondaryComponent(component_name, primary_adress)
  56.     local list = component.list(component_name)
  57.     local secondary = list()
  58.  
  59.     if secondary == primary_adress then
  60.         secondary = list()
  61.     end
  62.  
  63.     return secondary
  64. end
  65.  
  66. local function time()
  67.     local f = io.open("/tmp/tps", "w")
  68.     f:write("test")
  69.     f:close()
  70.     return (fs.lastModified("/tmp/tps"))
  71. end
  72.  
  73. local lastTime = 0
  74. local tps = {}
  75. local function getTps()
  76.     local curTime = time()
  77.     local time_diff = curTime - lastTime
  78.    
  79.     if #tps >= 10 then table.remove(tps, 1) end
  80.     tps[#tps + 1] = 20000 / time_diff
  81.     lastTime = curTime
  82.  
  83.     return math.median(tps), time_diff
  84. end
  85.  
  86. local function draw()
  87.     local tps, time_diff = getTps()
  88.     if tps > 20 then tps = 20 end -- мерило крайне неточное, приходиться работать с тем что имеем
  89.     local tps_str = math.floor(tps) .." tps"
  90.  
  91.     local eups_str = string.comma(getenergy() * time_diff / 1000 / frametime) .." eu/s"
  92.  
  93.     local wide = math.max(#tps_str, #eups_str)
  94.  
  95.     gpu.setResolution(wide, 2)
  96.    
  97.     gpu.setBackground(0x000000)
  98.     gpu.setForeground(0xFFFFFF)
  99.     gpu.fill(1, 1, wide, 2, " ")
  100.  
  101.     gpu.set(wide * 0.5 - #tps_str * 0.5, 1, tps_str)
  102.     gpu.set(wide * 0.5 - #eups_str * 0.5, 2, eups_str)
  103. end
  104.  
  105. function stop()
  106.     if _G.MonitoringTimerID == nil then return end
  107.  
  108.     event.cancel(_G.MonitoringTimerID)
  109.     _G.MonitoringTimerID = nil
  110. end
  111.  
  112. function start()
  113.     local screen_primary = require("term").screen()
  114.     local screen_secondary = getSecondaryComponent("screen", screen_primary)
  115.  
  116.     if screen_primary == screen_secondary then return print("К компьютеру не подключен второй экран!") end
  117.  
  118.     lastTime = time()
  119.  
  120.     _G.MonitoringTimerID = event.timer(frametime, function()
  121.         local w, h = gpu.getResolution()
  122.         gpu.bind(screen_secondary, false)
  123.             local succ, err = pcall(draw)
  124.             if err then
  125.                 gpu.setResolution(gpu.maxResolution())
  126.                 print(err)
  127.             end
  128.         gpu.bind(screen_primary, false)
  129.         gpu.setResolution(w, h)
  130.     end, math.huge)
  131. end
  132.  
  133. local arg = ...
  134. if arg == "stop" then return stop() end
  135.  
  136. local succ, err = pcall(start)
  137. if err then
  138.     print(err)
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement