From 9c7800659d96c9fd4c905e821a1ba4fcb56f7cd7 Mon Sep 17 00:00:00 2001 From: goksu <25721443+goeksu@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:16:41 +0300 Subject: [PATCH] python format --- src/tools/ftbench/src/tohtml.py | 172 ++++++++++++++++++++++---------- 1 file changed, 122 insertions(+), 50 deletions(-) diff --git a/src/tools/ftbench/src/tohtml.py b/src/tools/ftbench/src/tohtml.py index 7a67597f2..80ad6f525 100644 --- a/src/tools/ftbench/src/tohtml.py +++ b/src/tools/ftbench/src/tohtml.py @@ -1,13 +1,14 @@ +"""This module converts the benchmark results into a HTML file.""" import os import re import sys # Create the HTML file -project_root = sys.argv[1] -benchmark_html = os.path.join(project_root, 'benchmark.html') +PROJECT_ROOT = sys.argv[1] +BENCHMARK_HTML = os.path.join(PROJECT_ROOT, "benchmark.html") # GitLab URL -gitlab_url = 'https://gitlab.freedesktop.org/freetype/freetype/-/commit/' +GITLAB_URL = "https://gitlab.freedesktop.org/freetype/freetype/-/commit/" # CSS style CSS_STYLE = """ @@ -37,84 +38,155 @@ CSS_STYLE = """ """ # Directories -baseline_dir = os.path.join(project_root, 'baseline') -benchmark_dir = os.path.join(project_root, 'benchmark') +BASELINE_DIR = os.path.join(PROJECT_ROOT, "baseline") +BENCHMARK_DIR = os.path.join(PROJECT_ROOT, "benchmark") -# Open HTML file for writing -with open(benchmark_html, 'w') as html_file: - html_file.write(f'\n\nBenchmark Results\n{CSS_STYLE}\n\n

Benchmark Results

\n') +# Open HTML file for writing and write the header +with open(BENCHMARK_HTML, "w") as html_file: + html_file.write( + f"\n\ + \n\ + Benchmark Results\n\ + {CSS_STYLE}\ + \n\ + \n\ +

Benchmark Results

\n" + ) # If it's the info file, we want to handle it differently - with open(os.path.join(baseline_dir, "info.txt"), 'r') as f: + with open(os.path.join(BASELINE_DIR, "info.txt"), "r") as f: baseline_info = f.readlines() - with open(os.path.join(benchmark_dir, "info.txt"), 'r') as f: + with open(os.path.join(BENCHMARK_DIR, "info.txt"), "r") as f: benchmark_info = f.readlines() # Check if commit ids are the same if baseline_info[1].strip() == benchmark_info[1].strip(): - html_file.write('

Warning: Baseline and Benchmark have the same commit ID

\n') - - baseline_info[1] = '{}\n'.format(gitlab_url, baseline_info[1].strip(), baseline_info[1][:8]) - - benchmark_info[1] = '{}\n'.format(gitlab_url, benchmark_info[1].strip(), benchmark_info[1][:8]) + html_file.write( + '

Warning: Baseline and Benchmark have the same commit ID

\n' + ) - # Write info to HTML - html_file.write('

Info

\n') + baseline_info[1] = '{}\n'.format( + GITLAB_URL, baseline_info[1].strip(), baseline_info[1][:8] + ) + + benchmark_info[1] = '{}\n'.format( + GITLAB_URL, benchmark_info[1].strip(), benchmark_info[1][:8] + ) + + # Write info table + html_file.write("

Info

\n") html_file.write('\n') - html_file.write('\n') - info_list = ['Parameters', 'Commit ID', 'Commit Date', 'Branch'] - for info, baseline_line, benchmark_line in zip(info_list, baseline_info, benchmark_info): - html_file.write('\n'.format(info, baseline_line.strip(), benchmark_line.strip())) - html_file.write('
InfoBaselineBenchmark
{}{}{}

') - html_file.write('*Smaller values mean faster operation
\n') + html_file.write("InfoBaselineBenchmark\n") + info_list = ["Parameters", "Commit ID", "Commit Date", "Branch"] + for info, baseline_line, benchmark_line in zip( + info_list, baseline_info, benchmark_info + ): + html_file.write( + '{}{}{}\n'.format( + info, baseline_line.strip(), benchmark_line.strip() + ) + ) + html_file.write("
") + html_file.write("*Smaller values mean faster operation
\n") # Traverse through the 'baseline' directory - - for filename in os.listdir(baseline_dir): - if filename != 'info.txt': - - with open(os.path.join(baseline_dir, filename), 'r') as f: + for filename in os.listdir(BASELINE_DIR): + if filename != "info.txt": + with open(os.path.join(BASELINE_DIR, filename), "r") as f: baseline_results = f.readlines() - with open(os.path.join(benchmark_dir, filename), 'r') as f: + with open(os.path.join(BENCHMARK_DIR, filename), "r") as f: benchmark_results = f.readlines() - + # Get font name from within the .txt file for line in baseline_results: if line.startswith("ftbench results for font"): - fontname = line.split('/')[-1].strip("'")[:-2] + fontname = line.split("/")[-1]\ + .strip("'")[:-2] - - # Write results to HTML - html_file.write('

Results for {}

\n'.format(fontname)) + # Write test column headers + html_file.write("

Results for {}

\n".format(fontname)) html_file.write('\n') - html_file.write('\n'.format(os.path.join(baseline_dir,fontname[:-4]),os.path.join(benchmark_dir,fontname[:-4]))) - - for baseline_line, benchmark_line in zip(baseline_results, benchmark_results): - if baseline_line.startswith(' '): - baseline_match = re.match(r'\s+(.*?)\s+(\d+\.\d+)\s+ms\s+(\d+)\s', baseline_line) + html_file.write( + '\ + \ + \ + \ + \ + \ + \n'.format( + os.path.join(BASELINE_DIR, fontname[:-4]), + os.path.join(BENCHMARK_DIR, fontname[:-4]), + ) + ) - benchmark_match = re.match(r'\s+(.*?)\s+(\d+\.\d+)\s+ms\s+(\d+)\s', benchmark_line) + # Write test results + for baseline_line, benchmark_line in zip( + baseline_results, benchmark_results + ): + # Check if line is a test result + # Get results by Regex + if baseline_line.startswith(" "): + baseline_match = re.match( + r"\s+(.*?)\s+(\d+\.\d+)\s+ms\s+(\d+)\s", baseline_line + ) + + benchmark_match = re.match( + r"\s+(.*?)\s+(\d+\.\d+)\s+ms\s+(\d+)\s", benchmark_line + ) if baseline_match and benchmark_match: baseline_value = float(baseline_match.group(2)) benchmark_value = float(benchmark_match.group(2)) # Calculate the percentage difference - percentage_diff = ((baseline_value - benchmark_value) / baseline_value) * 100 - + percentage_diff = ( + (baseline_value - benchmark_value) / baseline_value + ) * 100 + + # Get itaration number baseline_n = baseline_match.group(3) benchmark_n = benchmark_match.group(3) - - if(baseline_n == benchmark_n): + + # Check if iteration number is the same + if baseline_n == benchmark_n: total_n = baseline_n else: - total_n = baseline_n + " / " + benchmark_n - + total_n = baseline_n +\ + " / " + benchmark_n + # Highlight the faster value if baseline_value > benchmark_value: - html_file.write('\n'.format(baseline_match.group(1), total_n,baseline_value, benchmark_value, percentage_diff)) + html_file.write( + '\ + \ + \ + \ + \ + \ + \n'.format( + baseline_match.group(1), + total_n, + baseline_value, + benchmark_value, + percentage_diff, + ) + ) else: - html_file.write('\n'.format(baseline_match.group(1), total_n,baseline_value, benchmark_value, percentage_diff)) + html_file.write( + '\ + \ + \ + \ + \ + \ + \n'.format( + baseline_match.group(1), + total_n, + baseline_value, + benchmark_value, + percentage_diff, + ) + ) - html_file.write('
TestNBaseline (ms) Benchmark (ms)Difference (%)
TestNBaseline (ms)Benchmark (ms)Difference (%)
{}{}{:.2f}{:.2f}{:.2f}
{}{}{:.3f}{:.3f}{:.1f}
{}{}{:.2f}{:.2f}{:.2f}
{}{}{:.3f}{:.3f}{:.1f}

\n') + html_file.write("
\n") - html_file.write('
Freetype Benchmark\n\n') + html_file.write("
Freetype Benchmark
\n\n")