Skip to content

Commit dc83d57

Browse files
Improve perf code
1 parent 06db8a6 commit dc83d57

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

.github/workflows/perf.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
###
1+
####################################################################################
22
# Runs benchmarks for BlackSheep source code for various versions of Python
33
# and Operating System and publishes the results.
44
# See the perf folder for more information.
5-
###
5+
####################################################################################
66
name: Benchmark
77

88
on:
99
push:
10+
paths:
11+
- 'perf/**'
1012
workflow_dispatch:
1113

1214
jobs:
1315
perf-tests:
1416
strategy:
1517
fail-fast: false
1618
matrix:
17-
python-version: ["3.11"] #, "3.12", "3.13"
18-
os: [ubuntu-latest] # , macos-latest, windows-latest]
19+
python-version: ["3.11", "3.12", "3.13"]
20+
os: [ubuntu-latest, macos-latest, windows-latest]
1921
runs-on: ${{ matrix.os }}
2022

2123
steps:
@@ -97,9 +99,9 @@ jobs:
9799
98100
export PYTHONPATH="."
99101
python perf/genreport.py
100-
# python perf/genreport.py --output windows-results.xlsx --platform Windows
101-
# python perf/genreport.py --output linux-results.xlsx --platform Linux
102-
# python perf/genreport.py --output macos-results.xlsx --platform macOS
102+
python perf/genreport.py --output windows-results.xlsx --platform Windows
103+
python perf/genreport.py --output linux-results.xlsx --platform Linux
104+
python perf/genreport.py --output macos-results.xlsx --platform macOS
103105
104106
- name: Upload reports
105107
uses: actions/upload-artifact@v4

perf/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
This folder contains scripts to benchmark the performance of the library. The
44
purpose of these benchmarks is to measure how changes in code affect
5-
performance, across Git commits.
5+
performance, across Git commits, Python versions, and operating system.
66

77
Benchmarks measure execution time and memory utilization.
88

9+
> [!TIP]
10+
>
11+
> Download the results from the GitHub Workflow.
12+
> The `benchmark-reports` artifacts include Excel files with tables and charts.
13+
>
14+
> [![Build](https://github.com/Neoteroi/BlackSheep/workflows/Benchmark/badge.svg)](https://github.com/Neoteroi/BlackSheep/actions/workflows/perf.yml)
15+
916
The code can both collect information and compare it depending on the Git
1017
commit SHA.
1118

perf/genreport.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def load_results(
3030
"""Load all benchmark results from the directory"""
3131
results = []
3232
for filename in glob.glob(f"{results_dir}/blacksheep_perf_*.json"):
33-
print(filename)
3433
with open(filename, "r") as f:
3534
data = json.load(f)
3635
if not _match_filter(data, python_filter, platform_filter):
@@ -189,7 +188,7 @@ def _add_ms_chart(workbook, df, worksheet, max_row):
189188

190189
# Add a chart title and some axis labels.
191190
chart.set_title({"name": "Performance comparison (lower is better)"})
192-
chart.set_x_axis({"name": "commit"})
191+
chart.set_x_axis({"name": ""})
193192
chart.set_y_axis({"name": "avg ms"})
194193

195194
# Set an Excel chart style. Colors with white outline and shadow.
@@ -240,6 +239,18 @@ def _add_charts(workbook, df, worksheet, max_row):
240239
_add_mem_chart(workbook, df, worksheet, max_row)
241240

242241

242+
def _set_number_format(workbook, worksheet, df):
243+
# Set number format for columns with "avg_ms" or "peak_mb" in their header
244+
number_format = workbook.add_format(
245+
{"num_format": "0.00000000", "align": "left"}
246+
) # 8 decimal points
247+
for col in df.columns:
248+
if "avg_ms" in col or "peak_mb" in col:
249+
col_index = df.columns.get_loc(col) # Get the column index
250+
col_letter = chr(65 + col_index) # Convert column index to Excel letter
251+
worksheet.set_column(f"{col_letter}:{col_letter}", None, number_format)
252+
253+
243254
def write_excel(df, output_file_name: str):
244255
if not output_file_name.endswith(".xlsx"):
245256
output_file_name = output_file_name + ".xlsx"
@@ -256,10 +267,19 @@ def write_excel(df, output_file_name: str):
256267
# Get the xlsxwriter workbook and worksheet objects.
257268
workbook = writer.book
258269
worksheet = writer.sheets["results"]
270+
_set_number_format(workbook, worksheet, df)
259271

260272
# Get the dimensions of the dataframe.
261-
(max_row, _) = df.shape
273+
(max_row, max_col) = df.shape
262274

275+
# Add a table to the worksheet
276+
worksheet.add_table(
277+
f"A1:{chr(65 + max_col - 1)}{max_row + 1}",
278+
{
279+
"columns": [{"header": col} for col in df.columns],
280+
"style": "Table Style Medium 9",
281+
},
282+
)
263283
# Configure the first series.
264284
# Plot time metrics
265285

@@ -309,7 +329,7 @@ def write_excel(df, output_file_name: str):
309329
results = load_results(args.results_dir, args.python, args.platform)
310330
if not results:
311331
print(f"No benchmark results found in {args.results_dir}")
312-
exit(1)
332+
exit(0)
313333

314334
df = create_comparison_table(results)
315335
df = _aggregate(df, args.group_by)

0 commit comments

Comments
 (0)