2 --Copyright (C) 2016 T4im
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 local DIR_DELIM, LINE_DELIM = DIR_DELIM, "\n"
19 local table, unpack, string, pairs, io, os = table, unpack, string, pairs, io, os
20 local rep, sprintf, tonumber = string.rep, string.format, tonumber
21 local core, setting_get = core, core.setting_get
25 -- Shorten a string. End on an ellipsis if shortened.
27 local function shorten(str, length)
28 if str and str:len() > length then
29 return "..." .. str:sub(-(length-3))
34 local function filter_matches(filter, text)
35 return not filter or string.match(text, filter)
38 local function format_number(number, fmt)
39 number = tonumber(number)
43 return sprintf(fmt or "%d", number)
47 new = function(self, object)
49 object.out = {} -- output buffer
51 return setmetatable(object, self)
53 __tostring = function (self)
54 return table.concat(self.out, LINE_DELIM)
56 print = function(self, text, ...)
58 text = sprintf(text, ...)
62 -- Avoid format unicode issues.
63 text = text:gsub("Ms", "µs")
66 table.insert(self.out, text or LINE_DELIM)
68 flush = function(self)
69 table.insert(self.out, LINE_DELIM)
70 local text = table.concat(self.out, LINE_DELIM)
76 local widths = { 55, 9, 9, 9, 5, 5, 5 }
77 local txt_row_format = sprintf(" %%-%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds", unpack(widths))
81 HR[i]= rep("-", widths[i])
83 -- ' | ' should break less with github than '-+-', when people are pasting there
84 HR = sprintf("-%s-", table.concat(HR, " | "))
86 local TxtFormatter = Formatter:new {
87 format_row = function(self, modname, instrument_name, statistics)
89 if instrument_name then
90 label = shorten(instrument_name, widths[1] - 5)
91 label = sprintf(" - %s %s", label, rep(".", widths[1] - 5 - label:len()))
92 else -- Print mod_stats
93 label = shorten(modname, widths[1] - 2) .. ":"
96 self:print(txt_row_format, label,
97 format_number(statistics.time_min),
98 format_number(statistics.time_max),
99 format_number(statistics:get_time_avg()),
100 format_number(statistics.part_min, "%.1f"),
101 format_number(statistics.part_max, "%.1f"),
102 format_number(statistics:get_part_avg(), "%.1f")
105 format = function(self, filter)
106 local profile = self.profile
107 self:print("Values below show absolute/relative times spend per server step by the instrumented function.")
108 self:print("A total of %d samples were taken", profile.stats_total.samples)
111 self:print("The output is limited to '%s'", filter)
117 "instrumentation", "min Ms", "max Ms", "avg Ms", "min %", "max %", "avg %"
120 for modname,mod_stats in pairs(profile.stats) do
121 if filter_matches(filter, modname) then
122 self:format_row(modname, nil, mod_stats)
124 if mod_stats.instruments ~= nil then
125 for instrument_name, instrument_stats in pairs(mod_stats.instruments) do
126 self:format_row(nil, instrument_name, instrument_stats)
133 self:format_row("total", nil, profile.stats_total)
138 local CsvFormatter = Formatter:new {
139 format_row = function(self, modname, instrument_name, statistics)
141 "%q,%q,%d,%d,%d,%d,%d,%f,%f,%f",
142 modname, instrument_name,
146 statistics:get_time_avg(),
150 statistics:get_part_avg()
153 format = function(self, filter)
155 "%q,%q,%q,%q,%q,%q,%q,%q,%q,%q",
156 "modname", "instrumentation",
166 for modname, mod_stats in pairs(self.profile.stats) do
167 if filter_matches(filter, modname) then
168 self:format_row(modname, "*", mod_stats)
170 if mod_stats.instruments ~= nil then
171 for instrument_name, instrument_stats in pairs(mod_stats.instruments) do
172 self:format_row(modname, instrument_name, instrument_stats)
180 local function format_statistics(profile, format, filter)
182 if format == "csv" then
183 formatter = CsvFormatter:new {
187 formatter = TxtFormatter:new {
191 formatter:format(filter)
192 return formatter:flush()
196 -- Format the profile ready for display and
197 -- @return string to be printed to the console
199 function reporter.print(profile, filter)
200 if filter == "" then filter = nil end
201 return format_statistics(profile, "txt", filter)
205 -- Serialize the profile data and
206 -- @return serialized data to be saved to a file
208 local function serialize_profile(profile, format, filter)
209 if format == "lua" or format == "json" or format == "json_pretty" then
210 local stats = filter and {} or profile.stats
212 for modname, mod_stats in pairs(profile.stats) do
213 if filter_matches(filter, modname) then
214 stats[modname] = mod_stats
218 if format == "lua" then
219 return core.serialize(stats)
220 elseif format == "json" then
221 return core.write_json(stats)
222 elseif format == "json_pretty" then
223 return core.write_json(stats, true)
226 -- Fall back to textual formats.
227 return format_statistics(profile, format, filter)
230 local worldpath = core.get_worldpath()
231 local function get_save_path(format, filter)
232 local report_path = setting_get("profiler.report_path") or ""
233 if report_path ~= "" then
234 core.mkdir(sprintf("%s%s%s", worldpath, DIR_DELIM, report_path))
237 "%s/%s/profile-%s%s.%s",
240 os.date("%Y%m%dT%H%M%S"),
241 filter and ("-" .. filter) or "",
243 ):gsub("[/\\]+", DIR_DELIM))-- Clean up delims
247 -- Save the profile to the world path.
248 -- @return success, log message
250 function reporter.save(profile, format, filter)
251 if not format or format == "" then
252 format = setting_get("profiler.default_report_format") or "txt"
258 local path = get_save_path(format, filter)
260 local output, io_err = io.open(path, "w")
262 return false, "Saving of profile failed with: " .. io_err
264 local content, err = serialize_profile(profile, format, filter)
267 return false, "Saving of profile failed with: " .. err
269 output:write(content)
272 local logmessage = "Profile saved to " .. path
273 core.log("action", logmessage)
274 return true, logmessage