init.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. -- mods/lagcheck/init.lua
  2. -- =================
  3. -- See README.md for licensing and other information.
  4. --ABM
  5. lagcheck_abm = {}
  6. abm_count = 0
  7. abm_last = {}
  8. local old_register_abm = minetest.register_abm
  9. function minetest.register_abm(spec)
  10. abm_count = abm_count + 1
  11. if abm_last.mod ~= minetest.get_current_modname() then
  12. abm_last.mod = minetest.get_current_modname()
  13. abm_last.count = 1
  14. else
  15. abm_last.count = abm_last.count + 1
  16. end
  17. lagcheck_abm[abm_count] = {mod = (minetest.get_current_modname() or "Unknown") .." (".. abm_last.count ..")", data = {}}
  18. spec.id = abm_count
  19. spec.action2 = spec.action
  20. spec.action = function(pos, node, active_object_count, active_object_count_wider)
  21. local time1 = minetest.get_us_time()
  22. spec.action2(pos, node, active_object_count, active_object_count_wider)
  23. table.insert(lagcheck_abm[spec.id].data, minetest.get_us_time() - time1 > 0 and minetest.get_us_time() - time1 or 0)
  24. end
  25. return old_register_abm(spec)
  26. end
  27. --Globalstep
  28. lagcheck_globalstep = {}
  29. globalstep_count = 0
  30. globalstep_last = {}
  31. local old_register_globalstep = minetest.register_globalstep
  32. function minetest.register_globalstep(spec)
  33. globalstep_count = globalstep_count + 1
  34. if globalstep_last.mod ~= minetest.get_current_modname() then
  35. globalstep_last.mod = minetest.get_current_modname()
  36. globalstep_last.count = 1
  37. else
  38. globalstep_last.count = globalstep_last.count + 1
  39. end
  40. lagcheck_globalstep[globalstep_count] = {mod = (minetest.get_current_modname() or "Unknown") .." (".. globalstep_last.count ..")", data = {}}
  41. local spec_old = spec
  42. spec = function(dtime)
  43. local time1 = minetest.get_us_time()
  44. spec_old(dtime)
  45. table.insert(lagcheck_globalstep[globalstep_count].data, minetest.get_us_time() - time1 > 0 and minetest.get_us_time() - time1 or 0)
  46. end
  47. return old_register_globalstep(spec)
  48. end
  49. --Print out
  50. local function sort_tablefunc(a)
  51. local aall = 0
  52. local amin = a[1] or 0
  53. local amax = a[1] or 0
  54. for key, value in ipairs(a.data) do
  55. aall = aall + value
  56. if value > amax then
  57. amax = value
  58. end
  59. if value < amin then
  60. amin = value
  61. end
  62. end
  63. return {min = amin, max = amax, all = #a.data == 0 and -1 or aall / #a.data}
  64. end
  65. local function sort_table(a, b)
  66. a = sort_tablefunc(a).all
  67. b = sort_tablefunc(b).all
  68. if a > b then
  69. return true
  70. end
  71. return false
  72. end
  73. local formatted = string.format("%%-%ds | %%%ds | %%%ds | %%%ds", 25, 9, 9, 9)
  74. minetest.register_chatcommand("lagcheck", {
  75. description = "Writes lagcheck",
  76. privs = {privs = true},
  77. func = function(name, param)
  78. local file = io.open(minetest.get_worldpath() .."/LagCheck.txt", "w")
  79. --ABM
  80. local str = "ABM:\n\n"
  81. str = str .. string.format(formatted, "Modname", "Average", "Min", "Max") .."\n"
  82. table.sort(lagcheck_abm, function(a, b) return sort_table(a, b) end)
  83. for key, value in ipairs(lagcheck_abm) do
  84. local data = sort_tablefunc(value)
  85. str = str .. string.format(formatted, value.mod, tostring(math.floor(data.all)), tostring(math.floor(data.min)), tostring(math.floor(data.max))) .."\n"
  86. end
  87. --Globalstep
  88. str = str .."\n\nGlobalstep:\n\n"
  89. str = str .. string.format(formatted, "Modname", "Average", "Min", "Max") .."\n"
  90. table.sort(lagcheck_globalstep, function(a, b) return sort_table(a, b) end)
  91. for key, value in ipairs(lagcheck_globalstep) do
  92. local data = sort_tablefunc(value)
  93. str = str .. string.format(formatted, value.mod, tostring(math.floor(data.all)), tostring(math.floor(data.min)), tostring(math.floor(data.max))) .."\n"
  94. end
  95. file:write(str)
  96. file:close()
  97. minetest.chat_send_player(name, "Lagcheck: File saved to world folder")
  98. end})