Module: Polyrun::Benchmark::Report
- Defined in:
- lib/polyrun/benchmark/report.rb
Overview
Formats benchmark profile JSON for humans and downstream tools.
Constant Summary collapse
- METRIC_HEADERS =
%w[section name value unit].freeze
- LINE_HEADERS =
%w[index text].freeze
Class Method Summary collapse
- .format_csv(data) ⇒ Object
- .format_markdown(data) ⇒ Object
- .format_text(data) ⇒ Object
- .load(path) ⇒ Object
- .render(data, format: "text") ⇒ Object
Class Method Details
.format_csv(data) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/polyrun/benchmark/report.rb', line 47 def format_csv(data) metrics = Array(data["metrics"]) if metrics.any? rows = metrics.map { |metric| [metric["section"], metric["name"], metric["value"], metric["unit"]] } return Export::Csv.generate(METRIC_HEADERS, rows) end rows = Array(data["lines"]).each_with_index.map { |line, index| [index + 1, line] } Export::Csv.generate(LINE_HEADERS, rows) end |
.format_markdown(data) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/polyrun/benchmark/report.rb', line 58 def format_markdown(data) = data["meta"] || {} sections = [ { heading: "Metadata", headers: %w[key value], rows: .sort_by { |key, _| key.to_s } } ] metrics = Array(data["metrics"]) if metrics.any? sections << { heading: "Metrics", headers: METRIC_HEADERS, rows: metrics.map { |metric| [metric["section"], metric["name"], metric["value"], metric["unit"]] } } end line_rows = Array(data["lines"]).reject(&:empty?).zip sections << { heading: "Log", headers: %w[line], rows: line_rows.empty? ? [["(empty)"]] : line_rows } Export::Markdown.document("Polyrun benchmark profile", sections) end |
.format_text(data) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/polyrun/benchmark/report.rb', line 35 def format_text(data) = data["meta"] || {} header = [ "Benchmark profile", "commit: #{["commit"]}", "recorded_at: #{["recorded_at"]}", "ruby: #{["ruby"]}", "" ] (header + Array(data["lines"])).join("\n") + "\n" end |
.load(path) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/polyrun/benchmark/report.rb', line 15 def load(path) ext = File.extname(path).downcase if ext == ".json" JSON.parse(File.read(path)) else {"meta" => {}, "lines" => File.read(path).lines.map(&:chomp), "metrics" => []} end end |
.render(data, format: "text") ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/polyrun/benchmark/report.rb', line 24 def render(data, format: "text") case format.to_s.downcase when "text", "console", "txt" then format_text(data) when "json" then JSON.pretty_generate(data) when "csv" then format_csv(data) when "markdown", "md" then format_markdown(data) else raise Polyrun::Error, "report-benchmark: unknown format #{format.inspect} (use text, json, csv, or markdown)" end end |