Skip to content

Commit

Permalink
add levels to usage tracking, move tests to units dir
Browse files Browse the repository at this point in the history
  • Loading branch information
NishchayKarle committed Jun 4, 2024
1 parent afde2dd commit 0792a5a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 94 deletions.
12 changes: 7 additions & 5 deletions parsl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from parsl.executors.threads import ThreadPoolExecutor
from parsl.monitoring import MonitoringHub
from parsl.usage_tracking.api import UsageInformation
from parsl.usage_tracking.levels import DISABLED as USAGE_TRACKING_DISABLED
from parsl.usage_tracking.levels import LEVEL_3 as USAGE_TRACKING_LEVEL_3
from parsl.utils import RepresentationMixin

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -105,7 +107,7 @@ def __init__(self,
strategy_period: Union[float, int] = 5,
max_idletime: float = 120.0,
monitoring: Optional[MonitoringHub] = None,
usage_tracking: Optional[int] = None,
usage_tracking: int = 0,
initialize_logging: bool = True) -> None:

executors = tuple(executors or [])
Expand Down Expand Up @@ -139,8 +141,8 @@ def __init__(self,
self.strategy = strategy
self.strategy_period = strategy_period
self.max_idletime = max_idletime
self.validate_usage_tracking(usage_tracking)
self.usage_tracking = usage_tracking
self._validate_usage_tracking()
self.initialize_logging = initialize_logging
self.monitoring = monitoring
self.std_autopath: Optional[Callable] = std_autopath
Expand All @@ -160,10 +162,10 @@ def _validate_executors(self) -> None:
raise ConfigurationError('Executors must have unique labels ({})'.format(
', '.join(['label={}'.format(repr(d)) for d in duplicates])))

def _validate_usage_tracking(self) -> None:
if self.usage_tracking is not None and int(self.usage_tracking) not in {0, 1, 2, 3}:
def validate_usage_tracking(self, level: int) -> None:
if not USAGE_TRACKING_DISABLED <= level <= USAGE_TRACKING_LEVEL_3:
raise ConfigurationError(
f"Usage Tracking values must be 0, 1, 2, or 3 and not {self.usage_tracking}"
f"Usage Tracking values must be 0, 1, 2, or 3 and not {level}"
)

def get_usage_information(self):
Expand Down
Empty file removed parsl/tests/test_usage/__init__.py
Empty file.
14 changes: 0 additions & 14 deletions parsl/tests/test_usage/test_invalid.py

This file was deleted.

32 changes: 0 additions & 32 deletions parsl/tests/test_usage/test_usage_tracking.py

This file was deleted.

40 changes: 40 additions & 0 deletions parsl/tests/unit/test_usage_tracking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test usage_tracking values."""

import pytest

import parsl
from parsl.config import Config
from parsl.errors import ConfigurationError
from typeguard import TypeCheckError


@pytest.mark.local
def test_config_load():
"""Test loading a config with usage tracking."""
with parsl.load(Config(usage_tracking=3)):
pass
parsl.clear()


@pytest.mark.local
@pytest.mark.parametrize("level", (0, 1, 2, 3, False, True))
def test_valid(level):
"""Test valid usage_tracking values."""
Config(usage_tracking=level)
assert Config(usage_tracking=level).usage_tracking == level


@pytest.mark.local
@pytest.mark.parametrize("level", (12, 1000, -1))
def test_invalid_values(level):
"""Test invalid usage_tracking values."""
with pytest.raises(ConfigurationError):
Config(usage_tracking=level)


@pytest.mark.local
@pytest.mark.parametrize("level", ('abcd', None, bytes(1), 1.0, 1j, object()))
def test_invalid_types(level):
"""Test invalid usage_tracking types."""
with pytest.raises(TypeCheckError):
Config(usage_tracking=level)
6 changes: 6 additions & 0 deletions parsl/usage_tracking/levels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Module for defining the usage tracking levels."""

DISABLED = 0 # Tracking is disabled
LEVEL_1 = 1 # Share info about Parsl version, Python version, platform
LEVEL_2 = 2 # Share info about config + level 1
LEVEL_3 = 3 # Share info about app count, app fails, execution time + level 2
51 changes: 8 additions & 43 deletions parsl/usage_tracking/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from parsl.utils import setproctitle
from parsl.version import VERSION as PARSL_VERSION
from parsl.errors import ConfigurationError
from parsl.usage_tracking.levels import DISABLED as USAGE_TRACKING_DISABLED
from parsl.usage_tracking.levels import LEVEL_3 as USAGE_TRACKING_LEVEL_3

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -118,18 +120,10 @@ def __init__(self, dfk, port=50077,
def check_tracking_level(self) -> int:
"""Check if tracking is enabled and return level.
Checks the following:
1. PARSL_TRACKING environment variable
- Possible values:
["true", "false", "True", "False", "0", "1", "2", "3"]
- Other values are treated as Level 0 (disabled)
Checks usage_tracking in Config
- Possible values: [True, False, 0, 1, 2, 3]
2. usage_tracking in Config
- Possible values: [True, False, 0, 1, 2, 3]
Parsl will choose the lowest level that is specified by any of the parameters, and 0 if none of them are set
True/False values are treated as Level 1/Level 0 respectively.
True/False values are treated as Level 1/Level 0 respectively.
Returns: int
- 0 : Tracking is disabled
Expand All @@ -140,41 +134,12 @@ def check_tracking_level(self) -> int:
- 3 : Tracking is enabled with level 3
Share info about app count, app fails, execution time + level 2
"""
inf = sys.maxsize

envvar = str(os.environ.get("PARSL_TRACKING", None)).lower()
if envvar == "none":
envvar_level = inf

elif envvar == "false":
envvar_level = 0

elif envvar == "true":
envvar_level = 1

elif envvar in {"0", "1", "2", "3"}:
envvar_level = int(envvar)

else:
raise ConfigurationError(
f"PARSL_TRACKING values must be true, false, 0, 1, 2, or 3 and not {os.environ.get('PARSL_TRACKING')}"
)

if self.config.usage_tracking is None:
config_level = inf

elif 0 <= int(self.config.usage_tracking) <= 3:
config_level = int(self.config.usage_tracking)

else:
if not USAGE_TRACKING_DISABLED <= self.config.usage_tracking <= USAGE_TRACKING_LEVEL_3:
raise ConfigurationError(
f"Usage Tracking values must be 0, 1, 2, or 3 and not {self.config.usage_tracking}"
)

if min(envvar_level, config_level) > 3:
return 0

return min(envvar_level, config_level)
return self.config.usage_tracking

def construct_start_message(self) -> bytes:
"""Collect preliminary run info at the start of the DFK.
Expand All @@ -186,7 +151,7 @@ def construct_start_message(self) -> bytes:
'parsl_v': self.parsl_version,
'python_v': self.python_version,
'platform.system': platform.system(),
'tracking_level': self.tracking_level}
'tracking_level': int(self.tracking_level)}

if self.tracking_level >= 2:
message['components'] = get_parsl_usage(self.dfk._config)
Expand Down

0 comments on commit 0792a5a

Please sign in to comment.