Skip to content

Commit

Permalink
Fix PDF output when there's just one run.
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmroberts authored Nov 7, 2023
1 parent e8db6f3 commit a33bd7b
Showing 1 changed file with 75 additions and 72 deletions.
147 changes: 75 additions & 72 deletions scripts/perf/perflib/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,79 +290,82 @@ def make_tex(figs, docdir, outdirs, label, significance, secondtype=None):
nspeedup = len(df_all_good.index)
nslowdown = len(df_all_bad.index)

print(
"nspeedup (" + label[1] + " is faster): " +
" " * max(len(label[0]) - len(label[1]), 0), nspeedup)
print(
"nslowdown (" + label[0] + " is faster): " +
" " * max(len(label[1]) - len(label[0]), 0), nslowdown)

if ncompare > 0:
geometric_mean = scipy.stats.mstats.gmean(speedups)
print("geometric mean:", geometric_mean)
tex += "\\begin{table}[H]\n"
tex += "\\centering\n"
tex += "\\begin{tabular}{l|p{5cm}|p{5cm}|l|}\n"
tex += "ncompare & nspeedup " + "(" + label[
1] + " faster)" + " & nslowdown (" + label[
0] + " faster)" + " & gmean\\\\ \n"
tex += "\\hline\n"
tex += str(ncompare) + "&" + str(nspeedup) + "&" + str(
nslowdown) + "&" + '{0:.3f}'.format(geometric_mean) + "\\\\\n"
tex += "100\\%" + "&" + '{0:.3f}'.format(
100 * nspeedup / ncompare) + "\\% " + "&" + '{0:.3f}'.format(
100 * nslowdown / ncompare) + "\\% " + "&\\\\\n"
tex += "\\hline\n"
tex += "\\end{tabular}\n"
tex += "\\caption{Overall Performance Changes}\n"
tex += "\\end{table}\n"

if nspeedup + nslowdown > 0:
vals = []
for row in df_all_good.itertuples(index=False):
vals.append(100 * (row.speedup - 1))
for row in df_all_bad.itertuples(index=False):
vals.append(100 * (1 - (1 / row.speedup)))

histdatname = os.path.join(docdir, "histogram.dat")

with open(histdatname, 'w') as f:
f.write("\t".join(str(x) for x in vals))
f.write("\n")

fout = tempfile.TemporaryFile(mode="w+")
ferr = tempfile.TemporaryFile(mode="w+")

asycmd = ["asy", "-f", "pdf", "histogram.asy"]
asycmd.extend(['-u', 'filename="' + histdatname + '"'])
asycmd.extend(['-o', os.path.join(docdir, "histogram.pdf")])

asyproc = subprocess.Popen(asycmd, cwd=top, stdout=fout, stderr=ferr)
try:
asyproc.wait(timeout=20)
except subprocess.TimeoutExpired:
logging.info("asy command killed: " + sjoin(asycmd))
asyproc.kill()

if asyproc.returncode != 0:
logging.warn('ASY command failed: ' + sjoin(asycmd))
if len(outdirs) > 1:
print(
"nspeedup (" + label[1] + " is faster): " +
" " * max(len(label[0]) - len(label[1]), 0), nspeedup)
print(
"nslowdown (" + label[0] + " is faster): " +
" " * max(len(label[1]) - len(label[0]), 0), nslowdown)
if ncompare > 0:
geometric_mean = scipy.stats.mstats.gmean(speedups)
print("geometric mean:", geometric_mean)
tex += "\\begin{table}[H]\n"
tex += "\\centering\n"
tex += "\\begin{tabular}{l|p{5cm}|p{5cm}|l|}\n"
tex += "ncompare & nspeedup " + "(" + label[
1] + " faster)" + " & nslowdown (" + label[
0] + " faster)" + " & gmean\\\\ \n"
tex += "\\hline\n"
tex += str(ncompare) + "&" + str(nspeedup) + "&" + str(
nslowdown) + "&" + '{0:.3f}'.format(geometric_mean) + "\\\\\n"
tex += "100\\%" + "&" + '{0:.3f}'.format(
100 * nspeedup / ncompare) + "\\% " + "&" + '{0:.3f}'.format(
100 * nslowdown / ncompare) + "\\% " + "&\\\\\n"
tex += "\\hline\n"
tex += "\\end{tabular}\n"
tex += "\\caption{Overall Performance Changes}\n"
tex += "\\end{table}\n"

if nspeedup + nslowdown > 0:
vals = []
for row in df_all_good.itertuples(index=False):
vals.append(100 * (row.speedup - 1))
for row in df_all_bad.itertuples(index=False):
vals.append(100 * (1 - (1 / row.speedup)))

histdatname = os.path.join(docdir, "histogram.dat")

with open(histdatname, 'w') as f:
f.write("\t".join(str(x) for x in vals))
f.write("\n")

fout = tempfile.TemporaryFile(mode="w+")
ferr = tempfile.TemporaryFile(mode="w+")

asycmd = ["asy", "-f", "pdf", "histogram.asy"]
asycmd.extend(['-u', 'filename="' + histdatname + '"'])
asycmd.extend(['-o', os.path.join(docdir, "histogram.pdf")])

asyproc = subprocess.Popen(asycmd,
cwd=top,
stdout=fout,
stderr=ferr)
try:
asyproc.wait(timeout=20)
except subprocess.TimeoutExpired:
logging.info("asy command killed: " + sjoin(asycmd))
asyproc.kill()

if asyproc.returncode != 0:
logging.warn('ASY command failed: ' + sjoin(asycmd))

fout.seek(0)
ferr.seek(0)
cout = fout.read()
cerr = ferr.read()

print(cout)
print(cerr)

tex += '''\\centering
\\begin{figure}[H]
\\includegraphics[width=\\textwidth]{'''
tex += "histogram.pdf"
tex += '''}
\\caption{''' + "Histogram of performance changes" + '''}\n\\end{figure}'''

fout.seek(0)
ferr.seek(0)
cout = fout.read()
cerr = ferr.read()

print(cout)
print(cerr)

tex += '''\\centering
\\begin{figure}[H]
\\includegraphics[width=\\textwidth]{'''
tex += "histogram.pdf"
tex += '''}
\\caption{''' + "Histogram of performance changes" + '''}\n\\end{figure}'''

tex += "\\clearpage\n"
tex += "\\clearpage\n"

tex += figtex

Expand Down

0 comments on commit a33bd7b

Please sign in to comment.