Clean timer code

pull/1/head
Bram van den Heuvel 2024-04-29 18:08:37 +02:00
parent 851cb19cd2
commit 588ed956b1
1 changed files with 60 additions and 18 deletions

View File

@ -39,7 +39,7 @@ function timer.stop()
internal.stats[name] = stop - start internal.stats[name] = stop - start
end end
name_len = math.max(name_len, name) name_len = math.max(name_len, string.len(name))
end end
local h1 = "Task" local h1 = "Task"
@ -60,29 +60,22 @@ function timer.stop()
) )
) )
internal.log(string.rep("-", name_len + 3 + 8 + 3 + 8)) internal.log_hr(name_len)
for _, t in ipairs(internal.waypoints) do for _, t in ipairs(internal.waypoints) do
local name = t[1] local name = t[1]
local duration = tostring(math.round(1e3 * (t[3] - t[2])))
local avg_duration = tostring(math.round(1e3 * internal.stats[name] / internal.sessions))
internal.log( internal.log_time(
table.concat( name,
{ name internal.to_ms(t[3] - t[2]),
, string.rep(" ", name_len - string.len(name)) internal.to_ms(internal.stats[name] / internal.sessions),
, " | " name_len
, string.rep(" ", 5 - string.len(duration))
, duration
, " ms | "
, string.rep(" ", 5 - string.len(avg_duration))
, avg_duration
, " ms"
}
, ""
)
) )
end end
internal.log_hr(name_len)
internal.log_time("Total", internal.sum(), internal.sum_avg(), name_len)
end end
-- Log text to the Minetest chat -- Log text to the Minetest chat
@ -90,8 +83,57 @@ function internal.log(text)
minetest.chat_send_all(os.time() .. " - " .. text) minetest.chat_send_all(os.time() .. " - " .. text)
end end
function internal.log_hr(name_len)
internal.log(string.rep("-", name_len + 3 + 8 + 3 + 8))
end
function internal.log_time(header, time, time_avg, header_max_len)
local duration = tostring(time)
local duration_avg = tostring(time_avg)
internal.log(
table.concat(
{ header
, string.rep(" ", header_max_len - string.len(header))
, " | "
, string.rep(" ", 5 - string.len(duration))
, duration
, " ms | "
, string.rep(" ", 5 - string.len(duration_avg))
, duration_avg
, " ms"
}
, ""
)
)
end
function internal.now() function internal.now()
internal.last = os.clock() internal.last = os.clock()
end end
function internal.sum()
local s = 0
for _, t in ipairs(internal.waypoints) do
s = s + (t[3] - t[2])
end
return internal.to_ms(s)
end
function internal.sum_avg()
local s = 0
for _, t in pairs(internal.stats) do
s = s + t
end
return internal.to_ms(s / internal.sessions)
end
function internal.to_ms(s)
return math.round(s * 1e3)
end
internal.now() internal.now()