-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(anta): Limit concurrency #680
base: main
Are you sure you want to change the base?
feat(anta): Limit concurrency #680
Conversation
bf88158
to
d7de654
Compare
|
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
b623e90
to
4879120
Compare
Conflicts have been resolved. A maintainer will review the pull request shortly. |
|
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
68e259e
to
83206e8
Compare
Conflicts have been resolved. A maintainer will review the pull request shortly. |
CodSpeed Performance ReportMerging #680 will not alter performanceComparing Summary
Benchmarks breakdown
|
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
Conflicts have been resolved. A maintainer will review the pull request shortly. |
- Developing ANTA tests: advanced_usages/custom-tests.md | ||
- ANTA as a Python Library: advanced_usages/as-python-lib.md | ||
- Caching in ANTA: advanced_usages/caching.md | ||
- Scaling ANTA: advanced_usages/scaling.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a Settings section that render the BaseSettings models in anta.settings?
|
||
On POSIX systems, this value is used to set the soft limit for the current process. | ||
The value cannot exceed the system's hard limit. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing ANTA_NOFILE attribute in the doctsring.
|
||
|
||
class MaxConcurrencySettings(BaseSettings): | ||
"""Environment variable for configuring the maximum number of concurrent tests in the event loop.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing ANTA_MAX_CONCURRENCY attribute in the docstring
class HttpxResourceLimitsSettings(BaseSettings): | ||
"""Environment variables for configuring the underlying HTTPX client resource limits. | ||
|
||
The limits are set using the following environment variables: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use attribute section to have a table in mkdocs?
if self.max_concurrency <= 0: | ||
msg = "Concurrency limit must be greater than 0." | ||
raise RuntimeError(msg) | ||
|
||
# NOTE: The `aiter` built-in function is not available in Python 3.9 | ||
tests = generator.__aiter__() # pylint: disable=unnecessary-dunder-call | ||
tests_ended = False | ||
tests_pending: set[Task[TestResult]] = set() | ||
|
||
while tests_pending or not tests_ended: | ||
# Add tests to the pending set until the limit is reached or no more tests are available | ||
while len(tests_pending) < self.max_concurrency and not tests_ended: | ||
try: | ||
# NOTE: The `anext` built-in function is not available in Python 3.9 | ||
test = await tests.__anext__() # pylint: disable=unnecessary-dunder-call | ||
except StopAsyncIteration: # noqa: PERF203 | ||
tests_ended = True | ||
logger.debug("All tests have been added to the pending set.") | ||
else: | ||
# Ensure the coroutine is scheduled to run and add it to the pending set | ||
tests_pending.add(asyncio.create_task(test)) | ||
logger.debug("Added a test to the pending set: %s", test) | ||
|
||
if len(tests_pending) >= self.max_concurrency: | ||
logger.debug("Concurrency limit reached: %s tests running. Waiting for tests to complete.", self.max_concurrency) | ||
|
||
if not tests_pending: | ||
logger.debug("No pending tests and all tests have been processed. Exiting.") | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.max_concurrency <= 0: | |
msg = "Concurrency limit must be greater than 0." | |
raise RuntimeError(msg) | |
# NOTE: The `aiter` built-in function is not available in Python 3.9 | |
tests = generator.__aiter__() # pylint: disable=unnecessary-dunder-call | |
tests_ended = False | |
tests_pending: set[Task[TestResult]] = set() | |
while tests_pending or not tests_ended: | |
# Add tests to the pending set until the limit is reached or no more tests are available | |
while len(tests_pending) < self.max_concurrency and not tests_ended: | |
try: | |
# NOTE: The `anext` built-in function is not available in Python 3.9 | |
test = await tests.__anext__() # pylint: disable=unnecessary-dunder-call | |
except StopAsyncIteration: # noqa: PERF203 | |
tests_ended = True | |
logger.debug("All tests have been added to the pending set.") | |
else: | |
# Ensure the coroutine is scheduled to run and add it to the pending set | |
tests_pending.add(asyncio.create_task(test)) | |
logger.debug("Added a test to the pending set: %s", test) | |
if len(tests_pending) >= self.max_concurrency: | |
logger.debug("Concurrency limit reached: %s tests running. Waiting for tests to complete.", self.max_concurrency) | |
if not tests_pending: | |
logger.debug("No pending tests and all tests have been processed. Exiting.") | |
return | |
if self.max_concurrency <= 0: | |
msg = "Concurrency limit must be greater than 0." | |
raise RuntimeError(msg) | |
tests_ended = False | |
tests_pending: set[Task[TestResult]] = set() | |
while tests_pending or not tests_ended: | |
async for test in generator: | |
# Add tests to the pending set until the limit is reached or no more tests are available | |
if len(tests_pending) < self.max_concurrency: | |
# Ensure the coroutine is scheduled to run and add it to the pending set | |
tests_pending.add(asyncio.create_task(test)) | |
logger.debug("Added a test to the pending set: %s", test) | |
else: | |
logger.debug("Concurrency limit reached: %s tests running. Waiting for tests to complete.", self.max_concurrency) | |
break | |
if len(tests_pending) < self.max_concurrency and not tests_ended: | |
tests_ended = True | |
logger.debug("All tests have been added to the pending set.") | |
if not tests_pending: | |
logger.debug("No pending tests and all tests have been processed. Exiting.") | |
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks equivalent right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't troubleshoot much but coroutines are not awaited properly with this code:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/home/cbaillar/git_projects/anta/anta/_runner.py:449: RuntimeWarning: coroutine 'VerifySSHStatus.test' was never awaited
async for test in generator:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/home/cbaillar/git_projects/anta/anta/_runner.py:449: RuntimeWarning: coroutine 'VerifyRoutingProtocolModel.test' was never awaited
async for test in generator:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/home/cbaillar/git_projects/anta/anta/_runner.py:449: RuntimeWarning: coroutine 'VerifyAcctConsoleMethods.test' was never awaited
async for test in generator:
@@ -37,6 +37,7 @@ def test_anta_dry_run( | |||
|
|||
results = session_results[request.node.callspec.id] | |||
|
|||
# TODO: Use AntaRunner in ANTA v2.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
I think the anta.runner.main
function uses the AntaRunner
class already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can consider removing main()
altogether and use AntaRunner
everywhere in v2.0.0.
@@ -69,6 +70,7 @@ def test_anta( | |||
|
|||
results = session_results[request.node.callspec.id] | |||
|
|||
# TODO: Use AntaRunner in ANTA v2.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put this TODO everywhere we are using main()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default value code could be simplified/cleaned.
Good idea to have this anta.settings
module, we should use it for ANTA_DEBUG
, in another PR ;)
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
Conflicts have been resolved. A maintainer will review the pull request shortly. |
925be23
to
216598a
Compare
|
Description
This PR improves the test runner by introducing a generator-based approach for managing test coroutines and setting a configurable limit on the number of concurrent tests.
Instead of loading all test coroutines into a list, the runner now uses a generator to yield tests. This approach prevents memory overload and improves performance when dealing with a large number of tests.
A limit on the number of concurrent tests is introduced to avoid overwhelming the runner. This limit is configurable with an environement variable (hidden).
Implementation:
The generator yields test coroutines, ensuring that only a limited number of tests are scheduled and run concurrently.
Upon reaching the concurrency limit, the runner waits for some tests to complete before scheduling new ones from the generator.
Fixes: #713, #832
Checklist: