mrWhiskasss

Пророверка ТПС по графику [OpenComputers]

Jul 28th, 2021 (edited)
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. --pastebin run B1HXukMz
  2. local com = require "component"
  3. local fs = require "filesystem"
  4. local keyboard = require "keyboard"
  5. local gpu = com.gpu
  6.  
  7. local leftX = 5
  8. local topY = 14
  9. local timeConstant = 2 --how long it waits per measure cycle
  10.  
  11. local w, h = gpu.getResolution()
  12. gpu.setBackground(0x000000)
  13. gpu.setForeground(0xFFFFFF)
  14. gpu.fill(1, 1, w, h, " ") -- clears the screen
  15.  
  16. local function time()
  17. local f = io.open("/tmp/timeFile","w")
  18. f:write("test")
  19. f:close()
  20. return(fs.lastModified("/tmp/timeFile"))
  21. end
  22.  
  23. local realTimeOld = 0
  24. local realTimeNew = 0
  25. local realTimeDiff = 0
  26.  
  27. local TPS = {}
  28. local avgTPS = 0
  29. for tSlot=1,10 do
  30.     TPS[tSlot]=0
  31. end
  32.  
  33. local function getColor(tps) --Uses HSV to decide color
  34.     local H, rP, gP, bP, X = tps*12-120, 0, 0, 0, 0
  35.     --H is hue should range from 0 to 120
  36.     if H<0 then H=0 end --forces greater then 0 but if things get wonky lets Hue go above 120 and turn blue
  37.     X = (1-math.abs((H/60)%2-1))
  38.     if H<60 then
  39.         rP = 1
  40.         gP = X
  41.         bP = 0
  42.     elseif H<120 then
  43.         rP = X
  44.         gP = 1
  45.         bP = 0 
  46.     elseif H<180 then
  47.         rP = 0
  48.         gP = 1
  49.         bP =
  50.     elseif H<240 then
  51.         rP = 0
  52.         gP = X
  53.         bP = 1 
  54.     elseif H<300 then
  55.         rP = X
  56.         gP = 0
  57.         bP = 1
  58.     else
  59.         rP = 1
  60.         gP = 0
  61.         bP = X
  62.     end
  63.     return(math.floor((rP)*255)*65536+math.floor((gP)*255)*256+math.floor((bP)*255))
  64. end
  65.  
  66. local function histoPlot(tabVal,leftX, topY,step)
  67.     local height = math.floor(tabVal[step]/2)+1
  68.     if height>11 then height=11 end
  69.     gpu.setBackground(0xB4B4B4)
  70.     gpu.fill(3*step+leftX-2, topY-2, 2, 13," ")  --erases the old TPS bar
  71.     gpu.fill(leftX-1,topY+10,33,1," ") --erases the old blue box
  72.     gpu.setBackground(getColor(tabVal[step]))
  73.     gpu.fill(3*step+leftX-2, topY-height+10, 2, height," ") --draws the TPS bar
  74.     gpu.setBackground(0x0064FF)
  75.     gpu.fill(leftX+step*3-2,topY+10,2,1," ") --the blue box that marks where we are on the graph
  76.     gpu.setBackground(0x000000)
  77. end
  78.  
  79. gpu.setBackground(0xB4B4B4)  --draws the nice grey graph background
  80. gpu.fill(leftX-1,topY-2,33,13," ")
  81. gpu.setBackground(0x000000)
  82.  
  83. for i = 1,#TPS do --draws the first red bars to make something looking like a graph
  84.     local height = math.floor(TPS[i]/2)+1
  85.     gpu.setBackground(getColor(TPS[i]))
  86.     gpu.fill(3*i+leftX-2, topY-height+10, 2, height," ")
  87. end
  88. gpu.setBackground(0x000000)
  89. gpu.set(18,11,"T/s")
  90. gpu.set(1,12,"20")
  91. gpu.set(1,17,"10")
  92. gpu.set(1,22," 0")
  93. for i=1, math.huge do
  94.     for tSlot = 1, 10 do --main averaging loop that measures individual TPS and puts it into a cycling table location
  95.         realTimeOld = time()
  96.         os.sleep(timeConstant) --waits for an estimated ammount game seconds
  97.         realTimeNew = time()
  98.  
  99.         realTimeDiff = realTimeNew-realTimeOld
  100.        
  101.         TPS[tSlot] = 20000*timeConstant/realTimeDiff
  102.         avgTPS = (TPS[1]+TPS[2]+TPS[3]+TPS[4]+TPS[5]+TPS[6]+TPS[7]+TPS[8]+TPS[9]+TPS[10])/10
  103.         gpu.set(2,2,"Server is running at:")
  104.         gpu.set(3,3,string.sub(tostring(TPS[tSlot])..".0000",1,7).." Ticks/second                                                ")
  105.         gpu.set(2,5,"Averaged value is:")
  106.         gpu.set(3,6,string.sub(tostring(avgTPS)..".0000",1,7).." Ticks/second                                                ")
  107.         histoPlot(TPS,leftX,topY,tSlot)
  108.         if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  109.             gpu.set(2,11,"Exiting...                                                ")
  110.             os.sleep(0.5)
  111.             gpu.fill(1, 1, w, h, " ") -- clears the screen
  112.             os.exit()
  113.         end
  114.     end
  115. end
  116.  
Add Comment
Please, Sign In to add comment