Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions scripts/benchmark/benchmark_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path

from tqdm.auto import tqdm
from util import make_request

RESULTS_DIR = Path(__file__).parent / "results"
RESULTS_DIR.mkdir(exist_ok=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default path should be benchmark/results.


if __name__ == "__main__":
"""Main function to benchmark the PESUAuth API.

Expand Down Expand Up @@ -123,14 +128,30 @@
else:
success.append(0)

outfile = (
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
if output:
outfile = Path(output)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check to ensure path to the outfile exists. Make the parent directories if it does not exist.

else:
filename = (
f"benchmark_[t={timestamp}]"
f"_[n={num_requests}]"
f"_[w={max_workers}]"
f"_[r={route}]"
f"_[execution_mode={'par' if parallel else 'seq'}]"
".csv"
)
outfile = RESULTS_DIR / filename

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simply compute and use the results dir here, since it is not being used anywhere else. Also, no need to make it a constant.


outfile.parent.mkdir(parents=True, exist_ok=True)

"""outfile = (
output
if output
else (
f"benchmark_[num_requests={num_requests}]_[max_workers={max_workers}]_"
f"[parallel={parallel}]_[route={route}]_[timeout={timeout}].csv"
)
)
)"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this docstring?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhh this was old code, i thought it would be a god idea to keep this as a docstring in case a slip up happens πŸ˜…


with open(
outfile,
Expand All @@ -139,6 +160,7 @@
f.write("status,time\n")
f.writelines(f"{s},{t}\n" for s, t in zip(success, times, strict=False))

print(f"Results saved to the following directory: {outfile}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outfile is a file, not directory. So update the string to: Results saved to:

print(f"Benchmark completed. Successful requests: {sum(success)} out of {len(success)}")
print(f"Average time per request: {sum(times) / len(times):.2f} seconds")
print(f"Total time taken: {sum(times):.2f} seconds")