Skip to content
Merged
Prev Previous commit
Next Next commit
Formatting
  • Loading branch information
savannahostrowski committed Nov 2, 2024
commit f0194a1632bf5edbf6dac557090ab20b94347a37
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pyperf
import argparse


def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="A CLI tool with many optional and positional arguments"
Expand All @@ -20,12 +21,14 @@ def create_parser() -> argparse.ArgumentParser:

return parser


def generate_arguments(i: int) -> list:
arguments = ["input.txt", "output.txt"]
for i in range(i):
arguments.extend([f"--option{i}", f"value{i}"])
return arguments


def bench_argparse(loops: int) -> None:
argument_lists = [
generate_arguments(500),
Expand All @@ -42,9 +45,11 @@ def bench_argparse(loops: int) -> None:

return pyperf.perf_counter() - t0


if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata['description'] = "Benchmark the argparse program with many optional arguments"
runner.metadata["description"] = (
"Benchmark the argparse program with many optional arguments"
)

runner.bench_time_func("argparse", bench_argparse)

Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Benchmark the argparse module with multiple subparsers,
each with their own options, and then parses a series of
command-line arguments.
Benchmark the argparse module with multiple subparsers, each with their own
options, and then parse a series of command-line arguments.

Author: Savannah Ostrowski
"""

import argparse
import pyperf


def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="A version control system CLI"
)
parser = argparse.ArgumentParser(description="A version control system CLI")

parser.add_argument("--version", action="version", version="1.0")

Expand All @@ -21,22 +19,38 @@ def create_parser() -> argparse.ArgumentParser:
add_parser = subparsers.add_parser("add", help="Add a file to the repository")
add_parser.add_argument("files", nargs="+", help="List of files to add to staging")

commit_parser = subparsers.add_parser("commit", help="Commit changes to the repository")
commit_parser = subparsers.add_parser(
"commit", help="Commit changes to the repository"
)
commit_parser.add_argument("-m", "--message", required=True, help="Commit message")

commit_group = commit_parser.add_mutually_exclusive_group(required=False)
commit_group.add_argument("--amend", action="store_true", help="Amend the last commit")
commit_group.add_argument("--no-edit", action="store_true", help="Reuse the last commit message")
commit_group.add_argument(
"--amend", action="store_true", help="Amend the last commit"
)
commit_group.add_argument(
"--no-edit", action="store_true", help="Reuse the last commit message"
)

push_parser = subparsers.add_parser(
"push", help="Push changes to remote repository"
)

push_parser = subparsers.add_parser("push", help="Push changes to remote repository")

network_group = push_parser.add_argument_group("Network options")
network_group.add_argument("--dryrun", action="store_true", help="Do not push changes, only simulate")
network_group.add_argument("--timeout", type=int, default=30, help="Timeout in seconds")
network_group.add_argument(
"--dryrun", action="store_true", help="Do not push changes, only simulate"
)
network_group.add_argument(
"--timeout", type=int, default=30, help="Timeout in seconds"
)

auth_group = push_parser.add_argument_group("Authentication options")
auth_group.add_argument("--username", required=True, help="Username for authentication")
auth_group.add_argument("--password", required=True, help="Password for authentication")
auth_group.add_argument(
"--username", required=True, help="Username for authentication"
)
auth_group.add_argument(
"--password", required=True, help="Password for authentication"
)

global_group = parser.add_mutually_exclusive_group()
global_group.add_argument("--verbose", action="store_true", help="Verbose output")
Expand All @@ -51,8 +65,17 @@ def bench_argparse(loops: int) -> None:
["add", "file1.txt", "file2.txt"],
["commit", "-m", "Initial commit"],
["commit", "-m", "Add new feature", "--amend"],
["push", "--dryrun", "--timeout", "60", "--username", "user", "--password", "pass"],
]
[
"push",
"--dryrun",
"--timeout",
"60",
"--username",
"user",
"--password",
"pass",
],
]

parser = create_parser()
range_it = range(loops)
Expand All @@ -64,8 +87,9 @@ def bench_argparse(loops: int) -> None:

return pyperf.perf_counter() - t0


if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata['description'] = "Benchmark the argparse program with subparsers"
runner.metadata["description"] = "Benchmark the argparse program with subparsers"

runner.bench_time_func("argparse", bench_argparse)