Module: Polyrun::Timing::Summary

Defined in:
lib/polyrun/timing/summary.rb

Overview

Human-readable slow-file list from merged timing JSON (per-file cost).

Constant Summary collapse

CSV_HEADERS =
%w[rank path last_seconds min max mean p95 runs failures timeouts].freeze
MARKDOWN_HEADERS =
%w[rank path seconds].freeze

Class Method Summary collapse

Class Method Details

.format_csv(merged, top: 30) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/polyrun/timing/summary.rb', line 33

def format_csv(merged, top: 30)
  rows = ranked_entries(merged, top: top).each_with_index.map do |(path, entry), index|
    [
      index + 1,
      path,
      entry["last_seconds"],
      entry["min"],
      entry["max"],
      entry["mean"],
      entry["p95"],
      entry["runs"],
      entry["failures"],
      entry["timeouts"]
    ]
  end
  Export::Csv.generate(CSV_HEADERS, rows)
end

.format_markdown(merged, top: 30, title: "Polyrun slowest files") ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/polyrun/timing/summary.rb', line 51

def format_markdown(merged, top: 30, title: "Polyrun slowest files")
  rows = ranked_entries(merged, top: top).each_with_index.map do |(path, entry), index|
    [index + 1, path, format("%.4f", Stats.binpack_weight(entry).to_f)]
  end
  Export::Markdown.document(
    title,
    [{heading: "Slow files", headers: MARKDOWN_HEADERS, rows: rows}]
  )
end

.format_slow_files(merged, top: 30, title: "Polyrun slowest files (by wall time, seconds)") ⇒ Object

merged is path (String) => seconds (Float) or stats Hash, as produced by Timing::Merge.merge_files.



23
24
25
26
27
28
29
30
31
# File 'lib/polyrun/timing/summary.rb', line 23

def format_slow_files(merged, top: 30, title: "Polyrun slowest files (by wall time, seconds)")
  return "#{title}\n  (no data)\n" if merged.nil? || merged.empty?

  lines = [title, ""]
  ranked_entries(merged, top: top).each_with_index do |(path, entry), index|
    lines << format("  %2d. %s  %.4f", index + 1, path, Stats.binpack_weight(entry).to_f)
  end
  lines.join("\n") + "\n"
end

.ranked_entries(merged, top: 30) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/polyrun/timing/summary.rb', line 14

def ranked_entries(merged, top: 30)
  return [] if merged.nil? || merged.empty?

  merged.map { |path, entry| [path, Stats.normalize_entry(entry)] }
    .sort_by { |(_, entry)| -Stats.binpack_weight(entry) }
    .first(Integer(top))
end

.render(merged, format: "text", **kwargs) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/polyrun/timing/summary.rb', line 61

def render(merged, format: "text", **kwargs)
  case format.to_s.downcase
  when "text", "console", "txt" then format_slow_files(merged, **kwargs)
  when "csv" then format_csv(merged, **kwargs)
  when "markdown", "md" then format_markdown(merged, **kwargs)
  else
    raise Polyrun::Error, "report-timing: unknown format #{format.inspect} (use text, csv, or markdown)"
  end
end

.write_file(merged, path, **kwargs) ⇒ Object



71
72
73
74
75
# File 'lib/polyrun/timing/summary.rb', line 71

def write_file(merged, path, **kwargs)
  format = kwargs.delete(:format) || "text"
  File.write(path, render(merged, format: format, **kwargs))
  path
end