initial implementation for histograms to svg,png terminals

This commit is contained in:
2026-07-14 10:29:55 +02:00
parent c1c5f78f66
commit ba8f29b49a
+53 -24
View File
@@ -1480,12 +1480,36 @@ def histogram_table(
return edges, counts
def histo_graphs(out_rows: list[OutputRow], args: argparse.Namespace):
# dumb term defaults
width = 120
height = 30
outfileroot=None
term = args.hterm
plotstyle = "set style fill transparent solid 0.35 noborder"
linestyle = "with steps lw 2"
if term in ("svg", "png"):
width, height = 900, 600
outfileroot="./histo-out"
plotstyle = ('set style fill transparent solid 0.35 noborder\n'
'set grid')
linestyle = "with boxes"
# nice, but not with logy
# plotstyle = "set style fill transparent solid 0.6 noborder"
# linestyle = "smooth kdensity with filledcurves y=0"
# ## linestyle = "smooth kdensity bandwidth .5 lw 2"
termsetup = {
"dumb": ("set encoding utf8\n"
"set colorsequence classic\n"
f"set terminal dumb ansi size {width},{height}\n"),
"png": (f"set terminal pngcairo size {width},{height}"),
"svg": (f"set terminal svg size {width},{height}")
}
colnames = args.histo.lower().split(',')
for metric in colnames:
tmpfile = tempfile.NamedTemporaryFile(mode='w+t', delete=False,
dir='.',prefix=f"tmp-seff-{metric}",
suffix=".data")
# create bins
try:
edges, counts = histogram_table([getattr(row,"vectors")[metric] for row in out_rows])
@@ -1493,9 +1517,13 @@ def histo_graphs(out_rows: list[OutputRow], args: argparse.Namespace):
sys.stderr.write(f"WARNING: failed to produce histogram for {metric}: {e}\n")
return
# currently we name the aggregations with numbers
# currently we name the aggregations by the number of the row
names = [str(x) for x in range (0, len(out_rows))]
# Write histogram data
tmpfile = tempfile.NamedTemporaryFile(mode='w+t', delete=False,
dir='.',prefix=f"tmp-seff-{metric}",
suffix=".data")
tmpfile.write(f"# bin_center {' '.join(names)}\n")
for i in range(len(edges) - 1):
left = edges[i]
@@ -1509,34 +1537,33 @@ def histo_graphs(out_rows: list[OutputRow], args: argparse.Namespace):
tmpfile.write("\n")
tmpfile.close()
# Gnuplot command
setoutput = ""
if outfileroot:
outfname = f'{outfileroot}-{metric}.{term}'
setoutput = f'set output "{outfname}"\n'
logy = ""
if args.hlogy:
logy = "set logscale y\n"
gnuplot_cmd = f"""set title "{metric} distribution" noenhanced
set xlabel "{metric}" noenhanced
set ylabel "Jobs"
set style fill transparent solid 0.35 noborder
set encoding utf8
# set terminal dumb size 120, 30
set terminal dumb enhanced size 120, 30
set colorsequence classic
set terminal dumb ansi size 120, 30
{logy}
plot_cmd = 'plot "{tmpfile.name}" \\
"""
# gnuplot_cmd += f' using 1:2 with boxes title "{names[0]}" \\\n'
# for col in range(1,len(names)):
# gnuplot_cmd += \
# f', "" using 1:{col+2} with boxes title "{names[col]}" \\\n'
gnuplot_cmd = (f'set title "{metric} distribution" noenhanced\n'
f'set xlabel "{metric}" noenhanced\n'
f'set ylabel "jobs"\n'
f'{plotstyle}\n'
f'{termsetup[term]}\n'
f'{logy}\n'
f'{setoutput}'
f'plot_cmd = \'plot "{tmpfile.name}"'
)
gnuplot_cmd += f' using 1:2 with steps lw 2 title "{names[0]}" \\\n'
gnuplot_cmd += f' using 1:2 {linestyle} title "{names[0]}" \\\n'
for col in range(1,len(names)):
gnuplot_cmd += \
f', "" using 1:{col+2} with steps lw 2 title "{names[col]}" \\\n'
f', "" using 1:{col+2} {linestyle} title "{names[col]}" \\\n'
gnuplot_cmd += "'\neval plot_cmd\n"
# print(f"DEBUG: GNUPLOT_SCRIPT:\n{gnuplot_cmd}")
#print(f"DEBUG: GNUPLOT_SCRIPT:\n{gnuplot_cmd}")
try:
run(["gnuplot"],
input=gnuplot_cmd,
@@ -1632,6 +1659,8 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
default=None)
p.add_argument("-H", "--histo", help=f"print histogram of the given metric (Supported columns: {', '.join(HISTO_COLUMNS)}).", default=None)
p.add_argument("--hlogy", action="store_true", help="use logscale for histogram y axis")
p.add_argument("--hterm", help=f"terminal type to plot histogram to.",
default="dumb", choices=("dumb", "png", "svg"))
# Options for writing/reading cache files
p.add_argument("-B", "--write-binary-cache", help="write a binary cache file in gzipped msgpack format")