Advertisement
Guest User

tps

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