Advertisement
Belzebub

lib/tps.lua

Mar 17th, 2021 (edited)
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local Time1, Time2, TimeDiff = 0, 0, 0
  3.  
  4. local function time()
  5.     local f = io.open("/tmp/tps", "w")
  6.     f:write("tps")
  7.     f:close()
  8.     return (fs.lastModified("/tmp/tps"))
  9. end
  10.  
  11. local function cur(delay)
  12.     Time1 = time()
  13.     os.sleep(delay)
  14.     Time2 = time()
  15.     TimeDiff = Time2 - Time1
  16.     return 20000 * delay / TimeDiff, TimeDiff
  17. end
  18.  
  19. function math.median(t)
  20.     table.sort(t)
  21.  
  22.     if math.fmod(#t,2) == 0 then
  23.         return (t[#t * 0.5] + t[(#t* 0.5) + 1]) * 0.5
  24.     else
  25.         return t[math.ceil(#t * 0.5)]
  26.     end
  27. end
  28.  
  29. function math.summ(tab)
  30.     local out = 0
  31.  
  32.     for i, n in ipairs(tab) do
  33.         out = out + n
  34.     end
  35.  
  36.     return out
  37. end
  38.  
  39. function math.avg(tab)
  40.     return math.summ(tab) / #tab
  41. end
  42.  
  43. local function process(t, delay, max)
  44.     if t[max] == nil then t[max] = {} end
  45.     local t = t[max]
  46.  
  47.     if #t >= max then table.remove(t, 1) end
  48.  
  49.     t[#t + 1] = cur(delay)
  50. end
  51.  
  52. local t = {}
  53. local function avg(delay, max)
  54.     process(t, delay, max)
  55.     return math.avg(t)
  56. end
  57.  
  58. local t = {}
  59. local function median(delay, max)
  60.     process(t, delay, max)
  61.     return math.median(t)
  62. end
  63.  
  64. return setmetatable({
  65.     cur = cur,
  66.     avg = avg,
  67.     median = median
  68. }, {__call = function()
  69.     return cur()
  70. end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement