Repository navigation
Expand file tree
/
Copy pathcov_map.py
More file actions
249 lines (209 loc) · 6.35 KB
/
Copy pathcov_map.py
File metadata and controls
249 lines (209 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
COV_JSON_PATH = "cov.json"
OUTPUT_PATH = "treemap"
DIFF_OUTPUT_PATH = "treemap-diff"
pl = None
px = None
def load_plot_deps():
global pl, px
if pl is None:
import polars as polars
pl = polars
if px is None:
import plotly.express as plotly_express
px = plotly_express
def load_file_level_cov(json_path):
load_plot_deps()
with open(json_path, "r") as f:
raw = json.load(f)
rows = []
for file in raw["data"][0]["files"]:
path = file["filename"]
if "summary" not in file:
continue
summary = file["summary"]
total = summary["branches"]["count"]
covered = summary["branches"]["covered"]
if total == 0:
continue
name = path.split("/")[-1]
dir_ = path.split("/")[-2] if "/" in path else ""
rows.append(
{
"dir": dir_,
"file": name,
"Covered Percentage (%)": covered / total * 100,
"branches": total,
}
)
return pl.DataFrame(rows)
def svg_comment_safe(text):
return text.replace("--", r"\u002d\u002d")
def write_svg_with_data(fig, output, df, metadata):
svg_path = Path(output + ".svg")
fig.write_image(str(svg_path))
payload = {
"metadata": metadata,
"rows": df.to_dicts(),
}
comment = (
"\n<!-- cov_map_data\n"
+ svg_comment_safe(json.dumps(payload, sort_keys=True))
+ "\n-->\n"
)
svg = svg_path.read_text()
insert_at = svg.find("?>")
if insert_at != -1:
insert_at += 2
svg = svg[:insert_at] + comment + svg[insert_at:]
else:
svg = comment + svg
svg_path.write_text(svg)
def plot_treemap(df, output="treemap"):
load_plot_deps()
fig = px.treemap(
df.to_dict(),
path=["dir", "file"],
values="branches",
color="Covered Percentage (%)",
color_continuous_scale="RdYlGn",
color_continuous_midpoint=50,
width=1000,
height=700,
)
fig.update_layout(
uniformtext=dict(minsize=12),
margin=dict(t=30, l=0, r=0, b=0),
coloraxis_colorbar_title="Branches Coverage %",
)
# fig.write_html(output + ".html")
write_svg_with_data(
fig,
output,
df,
{
"mode": "coverage",
"metric": "branch coverage percentage",
},
)
print(f"[+] Treemap saved to {output}")
def plot_treemap_diff(coverage_path, baseline_path, output="treemap-diff"):
load_plot_deps()
coverage = {
(row["dir"], row["file"]): row
for row in load_file_level_cov(coverage_path).to_dicts()
}
baseline = {
(row["dir"], row["file"]): row
for row in load_file_level_cov(baseline_path).to_dicts()
}
rows = []
for dir_, file in sorted(set(coverage) | set(baseline)):
coverage_row = coverage.get((dir_, file), {})
baseline_row = baseline.get((dir_, file), {})
branches = max(
coverage_row.get("branches", 0),
baseline_row.get("branches", 0),
)
if branches == 0:
continue
rows.append(
{
"dir": dir_,
"file": file,
"Coverage (%)": coverage_row.get("Covered Percentage (%)", 0),
"Baseline Coverage (%)": baseline_row.get(
"Covered Percentage (%)", 0
),
"Coverage Differential (%)": coverage_row.get(
"Covered Percentage (%)", 0
)
- baseline_row.get("Covered Percentage (%)", 0),
"branches": branches,
}
)
df = pl.DataFrame(rows)
if df.is_empty():
print("[!] No comparable file coverage data found")
return
better = df.filter(pl.col("Coverage Differential (%)") > 0).sort(
"Coverage Differential (%)", descending=True
)
if better.is_empty():
print("[diff] No files are better than baseline.")
else:
print("[diff] Files better than baseline:")
for row in better.to_dicts():
path = f"{row['dir']}/{row['file']}" if row["dir"] else row["file"]
print(
"[diff] "
f"{path}: +{row['Coverage Differential (%)']:.2f}% "
f"({row['Coverage (%)']:.2f}% vs "
f"{row['Baseline Coverage (%)']:.2f}%, "
f"{row['branches']} branches)"
)
fig = px.treemap(
df.to_dict(),
path=["dir", "file"],
values="branches",
color="Coverage Differential (%)",
color_continuous_scale="RdYlGn",
color_continuous_midpoint=0,
width=1000,
height=700,
)
fig.update_layout(
uniformtext=dict(minsize=12),
margin=dict(t=30, l=0, r=0, b=0),
coloraxis_colorbar_title="Branch Coverage Diff %",
)
# fig.write_html(output + ".html")
write_svg_with_data(
fig,
output,
df,
{
"mode": "diff",
"metric": "branch coverage percentage",
"operation": "coverage - baseline",
},
)
print(f"[+] Diff treemap saved to {output}.html and {output}.svg")
def parse_args():
parser = argparse.ArgumentParser(
description="Draw LLVM coverage treemaps from llvm-cov export JSON."
)
parser.add_argument(
"coverage",
nargs="?",
default=COV_JSON_PATH,
help=f"coverage JSON to plot, default: {COV_JSON_PATH}",
)
parser.add_argument(
"--diff",
metavar="BASELINE_JSON",
help="plot coverage differential as COVERAGE - BASELINE_JSON",
)
parser.add_argument(
"-o",
"--output",
help="output path without extension",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
if args.diff:
plot_treemap_diff(
args.coverage,
args.diff,
args.output if args.output else DIFF_OUTPUT_PATH,
)
else:
df = load_file_level_cov(args.coverage)
if df.is_empty():
print(f"[!] No file coverage data found in {args.coverage}")
else:
plot_treemap(df, args.output if args.output else OUTPUT_PATH)