aboutsummaryrefslogtreecommitdiff
path: root/scripts/lib/render-hotspots.awk
blob: 0eb2b6d94117a7feebf70bad3c9e6190386699ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# scripts/lib/render-hotspots.awk
# Render a per-line hotspot HTML report from sorted TSV rows.
# Input TSV (sorted slowest-first): "<total_ms>\t<hits>\t<file>\t<line>".
# Variables:
#   -v TITLE="..."
#   -v SRC=<path>   source file for the primary target, used to show line text.

function esc(s) {
  gsub(/&/, "\\&amp;", s); gsub(/</, "\\&lt;", s); gsub(/>/, "\\&gt;", s)
  return s
}

BEGIN {
  FS = "\t"; n = 0
  # Load source lines for text display (best effort).
  if (SRC != "") {
    i = 0
    while ((getline ln < SRC) > 0) { i++; srctext[i] = ln }
    close(SRC)
    srcbase = SRC; sub(/.*\//, "", srcbase); sub(/.*\\/, "", srcbase)
  }
}

{
  n++
  ms[n]   = $1 + 0
  hits[n] = $2 + 0
  file[n] = $3
  line[n] = $4 + 0
  if (n == 1) maxv = $1 + 0
}

END {
  print "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\">"
  print "<title>" esc(TITLE) "</title>"
  print "<style>body{font:14px/1.5 sans-serif;margin:0;padding:1.5rem;color:#111}"
  print "h1{font-size:18px}.meta{color:#555;margin-bottom:1rem}"
  print "table{border-collapse:collapse;width:100%}"
  print "th,td{padding:.3rem .5rem;text-align:left;border-bottom:1px solid #eee}"
  print "td.num{text-align:right;font-variant-numeric:tabular-nums}"
  print "code{font:12px/1.4 monospace;white-space:pre}"
  print ".bar{height:.7em;background:#e8703a;border-radius:2px}"
  print "</style></head><body>"
  print "<h1>" esc(TITLE) "</h1>"
  print "<div class=\"meta\">cumulative self-time per source line, slowest first</div>"
  print "<table><thead><tr><th>#</th><th>ms</th><th>hits</th><th>file:line</th><th>source</th><th></th></tr></thead><tbody>"
  for (i = 1; i <= n; i++) {
    w = (maxv > 0 ? (ms[i] * 100.0 / maxv) : 0)
    txt = ""
    if (file[i] == srcbase && (line[i] in srctext)) txt = srctext[line[i]]
    printf "<tr><td class=\"num\">%d</td><td class=\"num\">%.1f</td><td class=\"num\">%d</td>", i, ms[i], hits[i]
    printf "<td>%s:%d</td><td><code>%s</code></td>", esc(file[i]), line[i], esc(txt)
    printf "<td><div class=\"bar\" style=\"width:%.1f%%\"></div></td></tr>\n", w
  }
  print "</tbody></table></body></html>"
}