2
0

reporter.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. --Minetest
  2. --Copyright (C) 2016 T4im
  3. --
  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.
  8. --
  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.
  13. --
  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.
  17. local S = core.get_translator("__builtin")
  18. -- Note: In this file, only messages are translated
  19. -- but not the table itself, to keep it simple.
  20. local DIR_DELIM, LINE_DELIM = DIR_DELIM, "\n"
  21. local table, unpack, string, pairs, io, os = table, unpack, string, pairs, io, os
  22. local rep, sprintf, tonumber = string.rep, string.format, tonumber
  23. local core, settings = core, core.settings
  24. local reporter = {}
  25. ---
  26. -- Shorten a string. End on an ellipsis if shortened.
  27. --
  28. local function shorten(str, length)
  29. if str and str:len() > length then
  30. return "..." .. str:sub(-(length-3))
  31. end
  32. return str
  33. end
  34. local function filter_matches(filter, text)
  35. return not filter or string.match(text, filter)
  36. end
  37. local function format_number(number, fmt)
  38. number = tonumber(number)
  39. if not number then
  40. return "N/A"
  41. end
  42. return sprintf(fmt or "%d", number)
  43. end
  44. local Formatter = {
  45. new = function(self, object)
  46. object = object or {}
  47. object.out = {} -- output buffer
  48. self.__index = self
  49. return setmetatable(object, self)
  50. end,
  51. __tostring = function (self)
  52. return table.concat(self.out, LINE_DELIM)
  53. end,
  54. print = function(self, text, ...)
  55. if (...) then
  56. text = sprintf(text, ...)
  57. end
  58. if text then
  59. -- Avoid format unicode issues.
  60. text = text:gsub("Ms", "µs")
  61. end
  62. table.insert(self.out, text or LINE_DELIM)
  63. end,
  64. flush = function(self)
  65. table.insert(self.out, LINE_DELIM)
  66. local text = table.concat(self.out, LINE_DELIM)
  67. self.out = {}
  68. return text
  69. end
  70. }
  71. local widths = { 55, 9, 9, 9, 5, 5, 5 }
  72. local txt_row_format = sprintf(" %%-%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds", unpack(widths))
  73. local HR = {}
  74. for i=1, #widths do
  75. HR[i]= rep("-", widths[i])
  76. end
  77. -- ' | ' should break less with github than '-+-', when people are pasting there
  78. HR = sprintf("-%s-", table.concat(HR, " | "))
  79. local TxtFormatter = Formatter:new {
  80. format_row = function(self, modname, instrument_name, statistics)
  81. local label
  82. if instrument_name then
  83. label = shorten(instrument_name, widths[1] - 5)
  84. label = sprintf(" - %s %s", label, rep(".", widths[1] - 5 - label:len()))
  85. else -- Print mod_stats
  86. label = shorten(modname, widths[1] - 2) .. ":"
  87. end
  88. self:print(txt_row_format, label,
  89. format_number(statistics.time_min),
  90. format_number(statistics.time_max),
  91. format_number(statistics:get_time_avg()),
  92. format_number(statistics.part_min, "%.1f"),
  93. format_number(statistics.part_max, "%.1f"),
  94. format_number(statistics:get_part_avg(), "%.1f")
  95. )
  96. end,
  97. format = function(self, filter)
  98. local profile = self.profile
  99. self:print(S("Values below show absolute/relative times spend per server step by the instrumented function."))
  100. self:print(S("A total of @1 sample(s) were taken.", profile.stats_total.samples))
  101. if filter then
  102. self:print(S("The output is limited to '@1'.", filter))
  103. end
  104. self:print()
  105. self:print(
  106. txt_row_format,
  107. "instrumentation", "min Ms", "max Ms", "avg Ms", "min %", "max %", "avg %"
  108. )
  109. self:print(HR)
  110. for modname,mod_stats in pairs(profile.stats) do
  111. if filter_matches(filter, modname) then
  112. self:format_row(modname, nil, mod_stats)
  113. if mod_stats.instruments ~= nil then
  114. for instrument_name, instrument_stats in pairs(mod_stats.instruments) do
  115. self:format_row(nil, instrument_name, instrument_stats)
  116. end
  117. end
  118. end
  119. end
  120. self:print(HR)
  121. if not filter then
  122. self:format_row("total", nil, profile.stats_total)
  123. end
  124. end
  125. }
  126. local CsvFormatter = Formatter:new {
  127. format_row = function(self, modname, instrument_name, statistics)
  128. self:print(
  129. "%q,%q,%d,%d,%d,%d,%d,%f,%f,%f",
  130. modname, instrument_name,
  131. statistics.samples,
  132. statistics.time_min,
  133. statistics.time_max,
  134. statistics:get_time_avg(),
  135. statistics.time_all,
  136. statistics.part_min,
  137. statistics.part_max,
  138. statistics:get_part_avg()
  139. )
  140. end,
  141. format = function(self, filter)
  142. self:print(
  143. "%q,%q,%q,%q,%q,%q,%q,%q,%q,%q",
  144. "modname", "instrumentation",
  145. "samples",
  146. "time min µs",
  147. "time max µs",
  148. "time avg µs",
  149. "time all µs",
  150. "part min %",
  151. "part max %",
  152. "part avg %"
  153. )
  154. for modname, mod_stats in pairs(self.profile.stats) do
  155. if filter_matches(filter, modname) then
  156. self:format_row(modname, "*", mod_stats)
  157. if mod_stats.instruments ~= nil then
  158. for instrument_name, instrument_stats in pairs(mod_stats.instruments) do
  159. self:format_row(modname, instrument_name, instrument_stats)
  160. end
  161. end
  162. end
  163. end
  164. end
  165. }
  166. local function format_statistics(profile, format, filter)
  167. local formatter
  168. if format == "csv" then
  169. formatter = CsvFormatter:new {
  170. profile = profile
  171. }
  172. else
  173. formatter = TxtFormatter:new {
  174. profile = profile
  175. }
  176. end
  177. formatter:format(filter)
  178. return formatter:flush()
  179. end
  180. ---
  181. -- Format the profile ready for display and
  182. -- @return string to be printed to the console
  183. --
  184. function reporter.print(profile, filter)
  185. if filter == "" then filter = nil end
  186. return format_statistics(profile, "txt", filter)
  187. end
  188. ---
  189. -- Serialize the profile data and
  190. -- @return serialized data to be saved to a file
  191. --
  192. local function serialize_profile(profile, format, filter)
  193. if format == "lua" or format == "json" or format == "json_pretty" then
  194. local stats = filter and {} or profile.stats
  195. if filter then
  196. for modname, mod_stats in pairs(profile.stats) do
  197. if filter_matches(filter, modname) then
  198. stats[modname] = mod_stats
  199. end
  200. end
  201. end
  202. if format == "lua" then
  203. return core.serialize(stats)
  204. elseif format == "json" then
  205. return core.write_json(stats)
  206. elseif format == "json_pretty" then
  207. return core.write_json(stats, true)
  208. end
  209. end
  210. -- Fall back to textual formats.
  211. return format_statistics(profile, format, filter)
  212. end
  213. local worldpath = core.get_worldpath()
  214. local function get_save_path(format, filter)
  215. local report_path = settings:get("profiler.report_path") or ""
  216. if report_path ~= "" then
  217. core.mkdir(sprintf("%s%s%s", worldpath, DIR_DELIM, report_path))
  218. end
  219. return (sprintf(
  220. "%s/%s/profile-%s%s.%s",
  221. worldpath,
  222. report_path,
  223. os.date("%Y%m%dT%H%M%S"),
  224. filter and ("-" .. filter) or "",
  225. format
  226. ):gsub("[/\\]+", DIR_DELIM))-- Clean up delims
  227. end
  228. ---
  229. -- Save the profile to the world path.
  230. -- @return success, log message
  231. --
  232. function reporter.save(profile, format, filter)
  233. if not format or format == "" then
  234. format = settings:get("profiler.default_report_format") or "txt"
  235. end
  236. if filter == "" then
  237. filter = nil
  238. end
  239. local path = get_save_path(format, filter)
  240. local output, io_err = io.open(path, "w")
  241. if not output then
  242. return false, S("Saving of profile failed: @1", io_err)
  243. end
  244. local content, err = serialize_profile(profile, format, filter)
  245. if not content then
  246. output:close()
  247. return false, S("Saving of profile failed: @1", err)
  248. end
  249. output:write(content)
  250. output:close()
  251. core.log("action", "Profile saved to " .. path)
  252. return true, S("Profile saved to @1", path)
  253. end
  254. return reporter