Skip to content

Commit

Permalink
Add Argument validation, report interval must be >= sample interval
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed Jun 19, 2024
1 parent eb1237c commit 970fd49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ options:
False)
--sample-interval SAMPLE_INTERVAL, --s-i SAMPLE_INTERVAL
Interval in seconds between status checks of the
running process. Sample interval should be larger than
the runtime of the process or `duct` may underreport
the number of processes started. (default: 1.0)
running process. Sample interval must be less than or
equal to Report interval, and achieves the best
results when sample is significantly less than the
runtime of the process. (default: 1.0)
--report-interval REPORT_INTERVAL, --r-i REPORT_INTERVAL
Interval in seconds at which to report aggregated
data. (default: 60.0)
Expand Down
11 changes: 9 additions & 2 deletions src/duct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ class Arguments:
outputs: Outputs
record_types: RecordTypes

def __post_init__(self) -> None:
if self.report_interval < self.sample_interval:
raise argparse.ArgumentError(
None,
"--report-interval must be greater than or equal to --sample-interval.",
)

@classmethod
def from_argv(cls) -> Arguments: # pragma: no cover
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -373,8 +380,8 @@ def from_argv(cls) -> Arguments: # pragma: no cover
type=float,
default=float(os.getenv("DUCT_SAMPLE_INTERVAL", "1.0")),
help="Interval in seconds between status checks of the running process. "
"Sample interval should be larger than the runtime of the process or `duct` may "
"underreport the number of processes started.",
"Sample interval must be less than or equal to Report interval, and achieves the "
"best results when sample is significantly less than the runtime of the process.",
)
parser.add_argument(
"--report-interval",
Expand Down
48 changes: 48 additions & 0 deletions test/test_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import argparse
import pytest
from duct.__main__ import Arguments, Outputs, RecordTypes


def test_sample_less_than_report_interval() -> None:
args = Arguments(
command="fake",
command_args=[],
output_prefix="fake",
sample_interval=0.01,
report_interval=0.1,
capture_outputs=Outputs.NONE,
outputs=Outputs.ALL,
record_types=RecordTypes.SYSTEM_SUMMARY,
clobber=False,
)
assert args.sample_interval <= args.report_interval


def test_sample_equal_to_report_interval() -> None:
args = Arguments(
command="fake",
command_args=[],
output_prefix="fake",
sample_interval=0.1,
report_interval=0.1,
capture_outputs=Outputs.NONE,
outputs=Outputs.ALL,
record_types=RecordTypes.SYSTEM_SUMMARY,
clobber=False,
)
assert args.sample_interval == args.report_interval


def test_sample_equal_greater_than_report_interval() -> None:
with pytest.raises(argparse.ArgumentError):
Arguments(
command="fake",
command_args=[],
output_prefix="fake",
sample_interval=1.0,
report_interval=0.1,
capture_outputs=Outputs.NONE,
outputs=Outputs.ALL,
record_types=RecordTypes.SYSTEM_SUMMARY,
clobber=False,
)

0 comments on commit 970fd49

Please sign in to comment.