Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- демон для вывода eu/s и примерный tps на второй экран
- -- софт рендерит новый фрейм средне-медианное tps и eu/s каждую секунду
- -- credits: Beelzebub#0281 incredible-gmod.ru ??.12.2021
- -- скачивание: wget https://pastebin.com/raw/guHTzhQG /home/monitoring.lua
- -- остановить прогу: /home/monitoring.lua stop
- -- автозапуск проги: добавьте путь к файлу в конец /home/.shrc
- local frametime = 3 -- раз в сколько секунд будет рендериться новый кадр?
- local event = require("event")
- local component = require("component")
- local fs = require("filesystem")
- local internet = require("internet")
- local energy_counter = component.energy_counter
- local gpu = component.gpu
- local eu, last_eu = {}, 0
- function math.median(t)
- table.sort(t)
- if math.fmod(#t, 2) == 0 then
- return (t[#t * 0.5] + t[(#t * 0.5) + 1]) * 0.5
- else
- return t[math.ceil(#t * 0.5)]
- end
- end
- function string.comma(num)
- local formatted = tostring(math.floor(num))
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1 %2")
- if k == 0 then
- break
- end
- end
- return formatted
- end
- local function getenergy()
- if energy_counter == nil then return 0 end
- local cur = energy_counter.getCount()
- if #eu >= 10 then table.remove(eu, 1) end
- eu[#eu + 1] = cur - last_eu
- last_eu = cur
- return #eu == 1 and eu[1] or math.median(eu)
- end
- local function getSecondaryComponent(component_name, primary_adress)
- local list = component.list(component_name)
- local secondary = list()
- if secondary == primary_adress then
- secondary = list()
- end
- return secondary
- end
- local function time()
- local f = io.open("/tmp/tps", "w")
- f:write("test")
- f:close()
- return (fs.lastModified("/tmp/tps"))
- end
- local lastTime = 0
- local tps = {}
- local function getTps()
- local curTime = time()
- local time_diff = curTime - lastTime
- if #tps >= 10 then table.remove(tps, 1) end
- tps[#tps + 1] = 20000 / time_diff
- lastTime = curTime
- return math.median(tps), time_diff
- end
- local function draw()
- local tps, time_diff = getTps()
- if tps > 20 then tps = 20 end -- мерило крайне неточное, приходиться работать с тем что имеем
- local tps_str = math.floor(tps) .." tps"
- local eups_str = string.comma(getenergy() * time_diff / 1000 / frametime) .." eu/s"
- local wide = math.max(#tps_str, #eups_str)
- gpu.setResolution(wide, 2)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1, 1, wide, 2, " ")
- gpu.set(wide * 0.5 - #tps_str * 0.5, 1, tps_str)
- gpu.set(wide * 0.5 - #eups_str * 0.5, 2, eups_str)
- end
- function stop()
- if _G.MonitoringTimerID == nil then return end
- event.cancel(_G.MonitoringTimerID)
- _G.MonitoringTimerID = nil
- end
- function start()
- local screen_primary = require("term").screen()
- local screen_secondary = getSecondaryComponent("screen", screen_primary)
- if screen_primary == screen_secondary then return print("К компьютеру не подключен второй экран!") end
- lastTime = time()
- _G.MonitoringTimerID = event.timer(frametime, function()
- local w, h = gpu.getResolution()
- gpu.bind(screen_secondary, false)
- local succ, err = pcall(draw)
- if err then
- gpu.setResolution(gpu.maxResolution())
- print(err)
- end
- gpu.bind(screen_primary, false)
- gpu.setResolution(w, h)
- end, math.huge)
- end
- local arg = ...
- if arg == "stop" then return stop() end
- local succ, err = pcall(start)
- if err then
- print(err)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement