Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
## Unreleased

### Changed
- Performance: the HTML report emits each page's code table in one awk pass and stops forking `cat` for every markup block β€” 9.5s to 4.5s for 128 files, and 58.7s to 4.5s together with the escaping fix (#1098)
- Performance: `--coverage-report-html` no longer forks twice per source line to escape it β€” 58.7s to 9.5s for 128 files here, with the escaping done once per file and the per-page `wc`, `basename` and `pwd` calls replaced by parameter expansions (#1096)
- Performance: the coverage report picks its colours without a subshell per file and per function β€” the text report over 128 files went from 387ms to 318ms, and 4042ms to 3116ms with `BASHUNIT_COVERAGE_SHOW_FUNCTIONS` on (#1092)

Expand Down
227 changes: 133 additions & 94 deletions src/coverage/html_file.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,116 @@

# HTML coverage report: the per-file page.

# The code table of one page, in one awk pass.
#
# The Bash loop that did this classified, looked up and echoed per source line:
# 8116ms for 128 pages over 22,405 lines, with no forks left in it -- Bash is
# simply the wrong tool for emitting 7MB of markup (#1098). Every input it
# needs is already a file: the aggregated hits, the per-line test list and the
# source itself.
#
# Composed with the classifier rules, which are included ahead of it.
# shellcheck disable=SC2016
_BASHUNIT_COVERAGE_AWK_HTML_ROWS='
FILENAME == hitsfile {
hits[$1 + 0] = $2 + 0
next
}

FILENAME == testsfile {
# "<lineno>|<test_file>:<test_fn>", deduplicated, first-seen order kept.
p = index($0, "|")
if (p == 0) { next }
tln = substr($0, 1, p - 1) + 0
info = substr($0, p + 1)
key = tln SUBSEP info
if (key in seen) { next }
seen[key] = 1
tests[tln] = (tln in tests) ? tests[tln] "\n" info : info
next
}

{
total++
sl[total] = $0
}

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

END {
# The DEBUG trap attributes a multi-line statement to its starting line, so
# the count carries forward across the backslash chain (#722).
carry = 0
for (ln = 1; ln <= total; ln++) {
h = (ln in hits) ? hits[ln] : 0
if (carry > 0 && h < carry) { h = carry; hits[ln] = h }
if (h > 0 && bu_ends_with_continuation(sl[ln])) { carry = h } else { carry = 0 }
}

for (ln = 1; ln <= total; ln++) {
row_class = ""
hits_display = ""

if (bu_is_executable(sl[ln])) {
h = (ln in hits) ? hits[ln] : 0
if (h > 0) {
row_class = "covered"
if (ln in tests) {
tooltip = "<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
n = split(tests[ln], entries, "\n")
for (e = 1; e <= n; e++) {
if (entries[e] == "") { continue }
c = index(entries[e], ":")
if (c == 0) { tfile = entries[e]; tfn = "" } else { tfile = substr(entries[e], 1, c - 1); tfn = substr(entries[e], c + 1) }
sub(/^.*\//, "", tfile)
tooltip = tooltip "<li><span class=\"hits-tooltip-file\">" tfile "</span>:<span class=\"hits-tooltip-fn\">" tfn "</span></li>"
}
tooltip = tooltip "</ul></div>"
hits_display = "<span class=\"hits-badge has-tooltip\">" h times tooltip "</span>"
} else {
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
} else {
row_class = "uncovered"
hits_display = "<span class=\"hits-badge\">" h times "</span>"
}
}

printf " <tr id=\"line-%s\" class=\"%s line-anchor\">\n", ln, row_class
printf " <td class=\"line-num\">%s</td>\n", ln
printf " <td class=\"hits\">%s</td>\n", hits_display
printf " <td class=\"code\">%s</td>\n", escape(sl[ln])
printf " </tr>\n"
}
}
'

##
# Emits the code-table rows of one page.
# Arguments: $1 - source file, $2 - file holding its per-line test list
##
function bashunit::coverage::html_code_rows() {
local file="$1" tests_file="$2"

bashunit::coverage::ensure_hits_aggregated
bashunit::coverage::hits_file_for "$file"
local hits_file="$_BASHUNIT_COVERAGE_HITS_FILE_OUT"
if [ -z "$hits_file" ] || [ ! -f "$hits_file" ]; then
hits_file="/dev/null"
fi

# The multiplication sign comes in as a value, not as an awk escape: `\x` is
# not POSIX awk, so the byte sequence stays on the shell side.
env LC_ALL=C "$AWK" -v hitsfile="$hits_file" -v testsfile="$tests_file" -v times="Γ—" \
"${_BASHUNIT_COVERAGE_AWK_RULES}${_BASHUNIT_COVERAGE_AWK_HTML_ROWS}" \
"$hits_file" "$tests_file" "$file"
}

function bashunit::coverage::generate_file_html() {
local file="$1"
local output_file="$2"
Expand All@@ -27,55 +137,28 @@ function bashunit::coverage::generate_file_html() {
((++_fli))
done <"$file"

# And their escaped form, in ONE awk pass for the whole file. Escaping per
# line cost a command substitution and a sed each -- about 22,000 processes
# for this repo, 58.7s of HTML report (#1096).
local -a escaped_lines=()
local _eli=0 _el
while IFS= read -r _el || [ -n "$_el" ]; do
escaped_lines[_eli]="$_el"
((++_eli))
done < <(bashunit::coverage::html_escape_file "$file")

# Pre-load test hits data into indexed array (for tooltips)
# Index: line number, Value: newline-separated list of "test_file:test_function"
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
local -a tests_by_line=()
local _line_and_test
while IFS= read -r _line_and_test; do
[ -z "$_line_and_test" ] && continue
local _tln="${_line_and_test%%|*}"
local _tinfo="${_line_and_test#*|}"
if [ -n "${tests_by_line[_tln]:-}" ]; then
# Append only if not already present (avoid duplicates)
# Use newline boundaries to prevent false positives (e.g., test_foo matching test_foo_bar)
case $'\n'"${tests_by_line[_tln]}"$'\n' in
*$'\n'"$_tinfo"$'\n'*)
# already present, skip
;;
*)
tests_by_line[_tln]="${tests_by_line[_tln]}"$'\n'"${_tinfo}"
;;
esac
else
tests_by_line[_tln]="$_tinfo"
fi
done < <(bashunit::coverage::get_all_line_tests "$file")
# The per-line test list, for the tooltips. It goes to a file because the row
# emitter below is one awk pass that reads it alongside the hits and the
# source (#1098).
local tests_file="${_BASHUNIT_COVERAGE_DATA_FILE%/*}/page-tests"
if ! bashunit::coverage::get_all_line_tests "$file" >"$tests_file" 2>/dev/null; then
: >"$tests_file"
fi

# Count total lines and functions
local total_lines="${#file_lines[@]}"
local non_executable=$((total_lines - executable))

{
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EOF
echo " <title>${display_file##*/} | Coverage Report</title>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<style>
:root {
--primary: #6366f1; --primary-dark: #4f46e5; --primary-light: #818cf8;
Expand DownExpand Up@@ -214,20 +297,20 @@ EOF
<div class="file-title">
EOF
echo " <span class=\"file-name\">${display_file##*/}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="stats-section">
<div class="stat-item">
EOF
echo " <span class=\"stat-badge coverage $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Coverage</span>
</div>
<div class="stat-item">
EOF
echo " <span class=\"stat-badge lines\">${hit}/${executable}</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<span class="stat-label">Lines</span>
</div>
</div>
Expand All@@ -240,32 +323,32 @@ EOF
<span class="progress-label">Line Coverage Progress</span>
EOF
echo " <span class=\"progress-percent $class\">${pct}%</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="progress-bar">
EOF
echo " <div class=\"progress-fill $class\" style=\"width: ${pct}%;\"></div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-color covered"></span>
EOF
echo " <span>${hit} lines covered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color uncovered"></span>
EOF
echo " <span>${uncovered} lines uncovered</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="legend-item">
<span class="legend-color neutral"></span>
EOF
echo " <span>${non_executable} non-executable</span>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
</div>
</div>
Expand All@@ -277,7 +360,7 @@ EOF
functions_data=$(bashunit::coverage::extract_functions "$file")

if [ -n "$functions_data" ]; then
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="function-summary">
<table class="function-table">
<thead>
Expand DownExpand Up@@ -339,14 +422,14 @@ EOF
echo " </tr>"
done <<<"$functions_data"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</tbody>
</table>
</div>
EOF
fi

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
<div class="code-container">
<div class="code-wrapper">
<div class="code-header">
Expand All@@ -355,59 +438,15 @@ EOF
echo " <div class=\"code-stats\">"
echo " <span>${total_lines} total lines</span>"
echo " </div>"
cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</div>
<div class="code-body">
<table class="code-table">
EOF

local lineno=0
local line
for line in "${file_lines[@]}"; do
((++lineno))

local escaped_line="${escaped_lines[$((lineno - 1))]:-}"

local row_class=""
local hits_display=""

if bashunit::coverage::is_executable_line "$line" "$lineno"; then
# O(1) lookup from pre-loaded array
local hits=${_BASHUNIT_COVERAGE_HITS_BY_LINE[$lineno]:-0}

if [ "$hits" -gt 0 ]; then
row_class="covered"

# Check if we have test info for this line
local test_info="${tests_by_line[$lineno]:-}"
if [ -n "$test_info" ]; then
# Build tooltip with test information
local tooltip_html="<div class=\"hits-tooltip\"><div class=\"hits-tooltip-title\">Tests hitting this line</div><ul class=\"hits-tooltip-list\">"
local test_file test_fn
while IFS=':' read -r test_file test_fn; do
[ -z "$test_file" ] && continue
local short_file="${test_file##*/}"
tooltip_html="$tooltip_html<li><span class=\"hits-tooltip-file\">${short_file}</span>:<span class=\"hits-tooltip-fn\">${test_fn}</span></li>"
done <<<"$test_info"
tooltip_html="$tooltip_html</ul></div>"
hits_display="<span class=\"hits-badge has-tooltip\">${hits}Γ—${tooltip_html}</span>"
else
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
else
row_class="uncovered"
hits_display="<span class=\"hits-badge\">${hits}Γ—</span>"
fi
fi

echo " <tr id=\"line-${lineno}\" class=\"$row_class line-anchor\">"
echo " <td class=\"line-num\">$lineno</td>"
echo " <td class=\"hits\">$hits_display</td>"
echo " <td class=\"code\">$escaped_line</td>"
echo " </tr>"
done
bashunit::coverage::html_code_rows "$file" "$tests_file"

cat <<'EOF'
bashunit::coverage::emit_block <<'EOF'
</table>
</div>
</div>
Expand Down
Loading
Loading