Skip to content

Commit

Permalink
Added option to run CLI with dask profiler
Browse files Browse the repository at this point in the history
For the cli tool recently added in pytroll#184, this adds an option to profile
the run via dask and visualise the output as a bokeh plot.
  • Loading branch information
gerritholl committed Oct 24, 2023
1 parent 39b0290 commit 328642b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
8 changes: 4 additions & 4 deletions trollflow2/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def cli(args=None):
if args.dask_profiler:
profs.append(stack.enter_context(dask.diagnostics.Profiler()))
if args.dask_resource_profiler:
profs.append(stack.enter_context(dask.diagnostics.ResourceProfiler(dt=args.dask_resource_profiles)))
profs.append(stack.enter_context(dask.diagnostics.ResourceProfiler(dt=args.dask_resource_profiler)))
process_files(args.files, json.loads(args.metadata), args.product_list, produced_files)
if args.dask_profiler:
dask.diagnostics.visualize(
[profs], show=False, save=True, filename=args.dask_profiler)
if args.dask_profiler:
dask.diagnostics.visualize(
profs, show=False, save=True, filename=args.dask_profiler)


def _read_log_config(args):
Expand Down
29 changes: 24 additions & 5 deletions trollflow2/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
"""


@pytest.fixture
def product_list_filename(tmp_path):
"""Filename for a test product list, with contents."""
product_list = "my_product_list.yaml"
filename = os.fspath(tmp_path / product_list)
with open(filename, "w") as fd:
fd.write(yaml_test_noop)
return filename


def test_arg_parsing():
"""Test parsing args."""
product_list = "my_product_list.yaml"
Expand Down Expand Up @@ -91,17 +101,26 @@ def test_cli_raises_an_error_when_product_list_is_empty(tmp_path, caplog, empty_
assert "check YAML file" in caplog.text


def test_cli_starts_processing_when_files_are_provided(tmp_path):
def test_cli_starts_processing_when_files_are_provided(tmp_path, product_list_filename):
"""Test that the cli start processing when files are provided."""
product_list = "my_product_list.yaml"
files = ["file1", "file2"]
product_list_filename = os.fspath(tmp_path / product_list)
with open(product_list_filename, "w") as fd:
fd.write(yaml_test_noop)
from trollflow2.launcher import process_files
new_process = mock.Mock(wraps=process_files)
mda = {"dish": "pizza"}
with mock.patch("trollflow2.cli.process_files", new=new_process):
with mock.patch("trollflow2.cli.Queue") as q_mock:
cli(["-p", os.fspath(product_list_filename), "-m", json.dumps(mda), *files])
new_process.assert_called_once_with(files, mda, product_list_filename, q_mock.return_value)


def test_cli_dask_profiler(product_list_filename, tmp_path):
"""Test that dask profiles are written."""
from trollflow2.launcher import process_files
new_process = mock.Mock(wraps=process_files)
proffile = tmp_path / "dask-prof.html"
with (mock.patch("trollflow2.cli.process_files", new=new_process),
mock.patch("trollflow2.cli.Queue")):
cli(["-p", os.fspath(product_list_filename), "--dask-profiler",
os.fspath(proffile), "--dask-resource-profiler", "0.1",
"-m", json.dumps({"food": "soy"}), "aquafaba", "tempeh"])
assert proffile.exists()

0 comments on commit 328642b

Please sign in to comment.