reporter.lua 7.4 KB

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