Skip to content

Commit

Permalink
Update cli and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
cyliang368 committed Jun 16, 2024
1 parent 32c8cae commit f582cab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
25 changes: 23 additions & 2 deletions python/grass/benchmark/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def plot_nprocs_cli(args):
results.results,
filename=args.output,
title=args.title,
metric=args.metric,
)


Expand Down Expand Up @@ -161,9 +162,17 @@ def add_results_subcommand(parent_subparsers):

def add_plot_io_arguments(parser):
"""Add input and output arguments to *parser*."""
parser.add_argument("input", help="file with results (JSON)", metavar="input_file")
parser.add_argument(
"output", help="output file (e.g., PNG)", nargs="?", metavar="output_file"
"input", help="file with results (e.g. results.json)", metavar="input_file"
)
parser.add_argument(
"output",
help=(
"output file with extension (e.g., figure.png)."
" If not provided, the plot will be opened in a new window."
),
nargs="?",
metavar="output_file",
)


Expand All @@ -176,6 +185,16 @@ def add_plot_title_argument(parser):
)


def add_plot_metric_argument(parser):
"""Add metric argument to *parser*."""
parser.add_argument(
"--metric",
help="Metric for the plot (default: time)",
default="time",
choices=["time", "speedup", "efficiency"],
)


def add_plot_subcommand(parent_subparsers):
"""Add plot subcommand."""
main_parser = add_subcommand_parser(
Expand All @@ -198,6 +217,7 @@ def add_plot_subcommand(parent_subparsers):
)
add_plot_io_arguments(nprocs)
add_plot_title_argument(nprocs)
add_plot_metric_argument(nprocs)
nprocs.set_defaults(handler=plot_nprocs_cli)


Expand All @@ -206,6 +226,7 @@ def define_arguments():
parser = argparse.ArgumentParser(
description="Process results from module benchmarks.",
prog=get_executable_name(),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
subparsers = add_subparsers(parser, dest="command")

Expand Down
33 changes: 23 additions & 10 deletions python/grass/benchmark/testsuite/test_benchmark_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class TestBenchmarkCLI(TestCase):
"""Tests that benchmarkin CLI works"""

json_filename = "plot_test.json"
png_filename1 = "plot_test1.png"
png_filename2 = "plot_test2.png"
png_filenames = [f"plot_test1_{i}.png" for i in range(4)]
png_filenames.append("plot_test2.png")

def tearDown(self):
"""Remove test files"""
remove_file(self.json_filename)
remove_file(self.png_filename1)
remove_file(self.png_filename2)
for filename in self.png_filenames:
remove_file(filename)

def test_plot_nprocs_workflow(self):
"""Test that plot nprocs workflow runs"""
Expand All @@ -67,8 +67,15 @@ def test_plot_nprocs_workflow(self):
except grass.exceptions.ParameterError:
self.skipTest("r.univar without nprocs parameter")
save_results_to_file([result], self.json_filename)
benchmark_main(["plot", "nprocs", self.json_filename, self.png_filename1])
self.assertTrue(Path(self.png_filename1).is_file())

metrics = ["time", "speedup", "efficiency"]
benchmark_main(["plot", "nprocs", self.json_filename, self.png_filenames[0]])
for png_fname, metric in zip(self.png_filenames[1:4], metrics):
benchmark_main(
["plot", "nprocs", "--metric", metric, self.json_filename, png_fname]
)
for filename in self.png_filenames[:4]:
self.assertTrue(Path(filename).is_file())

def test_plot_cells_workflow(self):
"""Test that plot cells workflow runs"""
Expand All @@ -82,12 +89,18 @@ def test_plot_cells_workflow(self):
resolutions=[1000, 500],
)
save_results_to_file([result], self.json_filename)
benchmark_main(["plot", "cells", self.json_filename, self.png_filename1])
self.assertTrue(Path(self.png_filename1).is_file())
benchmark_main(["plot", "cells", self.json_filename, self.png_filenames[0]])
self.assertTrue(Path(self.png_filenames[0]).is_file())
benchmark_main(
["plot", "cells", "--resolutions", self.json_filename, self.png_filename2]
[
"plot",
"cells",
"--resolutions",
self.json_filename,
self.png_filenames[-1],
]
)
self.assertTrue(Path(self.png_filename2).is_file())
self.assertTrue(Path(self.png_filenames[-1]).is_file())


if __name__ == "__main__":
Expand Down

0 comments on commit f582cab

Please sign in to comment.