Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local gpu = component.gpu
- local keyboard = require("keyboard")
- local term = require("term")
- -- Настройки
- local timeConstant = 2 -- количество секунд, которое ждём между замерами
- local graphX = 5
- local graphY = 5
- local tpsHistory = {}
- local maxSamples = 10
- local w, h = gpu.getResolution()
- -- Очистка экрана
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1, 1, w, h, " ")
- gpu.set(2, 2, "Измерение TPS... Ждите ~" .. timeConstant .. " сек между точками.")
- gpu.set(2, 3, "Для выхода нажмите Ctrl + W")
- -- Получение серверного времени через файловую метку
- local function time()
- local f = io.open("/tmp/tpsfile.tmp", "w")
- f:write("test")
- f:close()
- return fs.lastModified("/tmp/tpsfile.tmp")
- end
- -- Расчёт цвета по TPS
- local function getColor(tps)
- local H = math.max(0, math.min(120, tps * 6)) -- от 0 до 120
- local X = 1 - math.abs((H / 60) % 2 - 1)
- local r, g, b = 0, 0, 0
- if H < 60 then
- r, g, b = 1, X, 0
- else
- r, g, b = X, 1, 0
- end
- return (math.floor(r * 255) << 16) + (math.floor(g * 255) << 8) + math.floor(b * 255)
- end
- -- Отрисовка графика
- local function drawGraph()
- gpu.setBackground(0x1E1E1E)
- gpu.fill(graphX - 2, graphY - 2, 35, 14, " ")
- for i = 1, #tpsHistory do
- local tps = tpsHistory[i]
- local height = math.floor(math.min(tps, 20) / 2 + 0.5)
- local color = getColor(tps)
- gpu.setBackground(color)
- gpu.fill(graphX + i * 3, graphY + 10 - height, 2, height, " ")
- end
- gpu.setBackground(0x000000)
- end
- -- Главный цикл
- while true do
- -- Проверка выхода
- if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
- gpu.set(2, h - 1, "Выход...")
- os.sleep(0.5)
- term.clear()
- os.exit()
- end
- -- Измерение TPS
- local t0 = time()
- os.sleep(timeConstant)
- local t1 = time()
- local delta = t1 - t0
- local tps = (20000 * timeConstant) / math.max(delta, 1) -- мс
- table.insert(tpsHistory, tps)
- if #tpsHistory > maxSamples then
- table.remove(tpsHistory, 1)
- end
- -- Вывод текста
- gpu.setForeground(0xFFFFFF)
- gpu.set(2, 5, "Текущий TPS: " .. string.format("%.2f", tps) .. " ")
- local avg = 0
- for _, v in ipairs(tpsHistory) do avg = avg + v end
- avg = avg / #tpsHistory
- gpu.set(2, 6, "Средний TPS: " .. string.format("%.2f", avg) .. " ")
- -- График
- drawGraph()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement