aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--init.lua83
1 files changed, 69 insertions, 14 deletions
diff --git a/init.lua b/init.lua
index 5bfecce..d137ecf 100644
--- a/init.lua
+++ b/init.lua
@@ -7,6 +7,9 @@ advprofiler = {}
local WINDOW_SIZE = 1024
+local report_to_csv = true
+local report_file = minetest.get_worldpath()..DIR_DELIM.."advprofiler.csv"
+
--[[
Unit table:
@@ -46,14 +49,19 @@ end
local clock = os.clock
local function accumulate(list, count)
- local tot, max = 0, nil
+ local tot, max = 0, 0
+ local act_cnt = 0
local li
for i=1,count do
li = list[i]
- tot = tot + li
- max = math.max((max or li), li)
+ if li then
+ tot = tot + li
+ max = math.max(max, li)
+ act_cnt = act_cnt + 1
+ end
end
- local avg = tot/count
+ if act_cnt==0 then return nil,nil,nil end
+ local avg = tot/act_cnt
return tot, avg, max
end
@@ -126,8 +134,24 @@ function profiler:end_step()
end
end
-function profiler:end_window(print_table)
+local function nformat_csv(n)
+ if not n then return "--" end
+ return string.format("%d", n)
+end
+
+local function tformat_csv(n)
+ if not n then return "--" end
+ return string.format("%d", n*1000)
+end
+
+
+function profiler:end_window(print_table, csv_file)
table.insert(print_table, "=== "..self.name.." ===")
+
+ if csv_file then
+ csv_file:write("### "..self.name.."\n")
+ end
+
for unitn, unit in pairs(self.units) do
if unit.enter_ts then
warning(self,unitn,"Unit still entered at end of step! (discarding)")
@@ -138,13 +162,22 @@ function profiler:end_window(print_table)
profiler = self,
unit = unitn,
}
- rep.count_tot, rep.count_avg, rep.count_max = accumulate(unit.st_counts, sc)
- rep.time_tot, rep.time_avg, rep.time_max = accumulate(unit.st_times_tot, sc)
+
+ rep.count_tot, rep.count_avg, rep.count_max = accumulate(unit.st_counts, sc)
+ rep.time_tot, rep.time_avg, rep.time_max = accumulate(unit.st_times_tot, sc)
+ _ , rep.per_call_avg, _ = accumulate(unit.st_times_avg, sc)
+ _ , _ , rep.per_call_max = accumulate(unit.st_times_max, sc)
+
local report_f = unit.report or advprofiler.report
table.insert(print_table, unitn..":\t"..report_f(rep))
+ if csv_file then
+ local colt = advprofiler.get_report_columns(rep)
+ csv_file:write(table.concat(colt, "\t").."\n")
+ end
+
unit.st_stepcount = 0
end
end
@@ -164,25 +197,40 @@ function advprofiler.new_profiler(name)
return new
end
-
-
function advprofiler.step()
for _,prof in ipairs(_profs) do
prof:end_step()
end
end
+
function advprofiler.end_window_and_report(cyc)
local print_table = {
- "--- Profiling report for cycle "..cyc.." ---"
+ "--- Profiling report for cycle "..cyc.." ---",
+ "Unit\tcount_tot\tcount_avg\tcount_max\ttime_tot\ttime_avg\ttime_max\tper_call_avg\tper_call_max"
}
+
+ local csv_file
+
+ if report_to_csv then
+ csv_file = io.open(report_file, "w")
+ end
+
+ if csv_file then
+ csv_file:write("Unit\tcount_tot\tcount_avg\tcount_max\ttime_tot\ttime_avg\ttime_max\tper_call_avg\tper_call_max\n")
+ end
+
for _,prof in ipairs(_profs) do
- prof:end_window(print_table)
+ prof:end_window(print_table, csv_file)
end
for _,l in ipairs(print_table) do
logs(l)
end
+
+ if csv_file then
+ csv_file:close()
+ end
end
@@ -193,11 +241,18 @@ rep.count_tot, rep.count_avg, rep.count_max
rep.time_tot, rep.time_avg, rep.time_max
]]
+function advprofiler.get_report_columns(rep)
+ return {
+ rep.unit,
+ nformat_csv(rep.count_tot), nformat_csv(rep.count_avg), nformat_csv(rep.count_max),
+ tformat_csv(rep.time_tot), tformat_csv(rep.time_avg), tformat_csv(rep.time_max),
+ tformat_csv(rep.per_call_avg), tformat_csv(rep.per_call_max)
+ }
+end
function advprofiler.report(rep)
- return string.format("Count avg=%d\tmax=%d\ttotal=%d\tTime avg=%dms\tmax=%dms\ttotal=%dms",
- rep.count_avg, rep.count_max, rep.count_tot,
- rep.time_avg*1000, rep.time_max*1000, rep.time_tot*1000)
+ local colt = advprofiler.get_report_columns(rep)
+ return table.concat(colt, "\t")
end