Advertisement
gugurec1000

tps

May 22nd, 2025 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local gpu = component.gpu
  4. local keyboard = require("keyboard")
  5. local term = require("term")
  6.  
  7. -- Настройки
  8. local timeConstant = 2 -- количество секунд, которое ждём между замерами
  9. local graphX = 5
  10. local graphY = 5
  11.  
  12. local tpsHistory = {}
  13. local maxSamples = 10
  14. local w, h = gpu.getResolution()
  15.  
  16. -- Очистка экрана
  17. gpu.setBackground(0x000000)
  18. gpu.setForeground(0xFFFFFF)
  19. gpu.fill(1, 1, w, h, " ")
  20. gpu.set(2, 2, "Измерение TPS... Ждите ~" .. timeConstant .. " сек между точками.")
  21. gpu.set(2, 3, "Для выхода нажмите Ctrl + W")
  22.  
  23. -- Получение серверного времени через файловую метку
  24. local function time()
  25.   local f = io.open("/tmp/tpsfile.tmp", "w")
  26.   f:write("test")
  27.   f:close()
  28.   return fs.lastModified("/tmp/tpsfile.tmp")
  29. end
  30.  
  31. -- Расчёт цвета по TPS
  32. local function getColor(tps)
  33.   local H = math.max(0, math.min(120, tps * 6)) -- от 0 до 120
  34.   local X = 1 - math.abs((H / 60) % 2 - 1)
  35.   local r, g, b = 0, 0, 0
  36.  
  37.   if H < 60 then
  38.     r, g, b = 1, X, 0
  39.   else
  40.     r, g, b = X, 1, 0
  41.   end
  42.  
  43.   return (math.floor(r * 255) << 16) + (math.floor(g * 255) << 8) + math.floor(b * 255)
  44. end
  45.  
  46. -- Отрисовка графика
  47. local function drawGraph()
  48.   gpu.setBackground(0x1E1E1E)
  49.   gpu.fill(graphX - 2, graphY - 2, 35, 14, " ")
  50.   for i = 1, #tpsHistory do
  51.     local tps = tpsHistory[i]
  52.     local height = math.floor(math.min(tps, 20) / 2 + 0.5)
  53.     local color = getColor(tps)
  54.     gpu.setBackground(color)
  55.     gpu.fill(graphX + i * 3, graphY + 10 - height, 2, height, " ")
  56.   end
  57.   gpu.setBackground(0x000000)
  58. end
  59.  
  60. -- Главный цикл
  61. while true do
  62.   -- Проверка выхода
  63.   if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  64.     gpu.set(2, h - 1, "Выход...")
  65.     os.sleep(0.5)
  66.     term.clear()
  67.     os.exit()
  68.   end
  69.  
  70.   -- Измерение TPS
  71.   local t0 = time()
  72.   os.sleep(timeConstant)
  73.   local t1 = time()
  74.   local delta = t1 - t0
  75.   local tps = (20000 * timeConstant) / math.max(delta, 1) -- мс
  76.  
  77.   table.insert(tpsHistory, tps)
  78.   if #tpsHistory > maxSamples then
  79.     table.remove(tpsHistory, 1)
  80.   end
  81.  
  82.   -- Вывод текста
  83.   gpu.setForeground(0xFFFFFF)
  84.   gpu.set(2, 5, "Текущий TPS: " .. string.format("%.2f", tps) .. "    ")
  85.   local avg = 0
  86.   for _, v in ipairs(tpsHistory) do avg = avg + v end
  87.   avg = avg / #tpsHistory
  88.   gpu.set(2, 6, "Средний TPS:  " .. string.format("%.2f", avg) .. "    ")
  89.  
  90.   -- График
  91.   drawGraph()
  92. end
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement