Skip to content
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

Open
wants to merge 70 commits into
base: main
Choose a base branch
from

Conversation

carl-baillargeon
Copy link
Contributor

@carl-baillargeon carl-baillargeon commented May 16, 2024

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.

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: #832

Checklist:

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Contributor

github-actions bot commented Jul 4, 2024

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@mtache mtache changed the title refactor(anta): Refactor runner to use a generator with a limit feat(anta): Limit concurrency Oct 2, 2024
Copy link
Contributor

github-actions bot commented Oct 2, 2024

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link

codspeed-hq bot commented Oct 2, 2024

CodSpeed Performance Report

Merging #680 will not alter performance

Comparing carl-baillargeon:refactor/runner_limit (6f69bd6) with main (f841d66)

Summary

✅ 22 untouched benchmarks
🆕 2 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 test_setup_tests[1-device] N/A 17.1 ms N/A
🆕 test_setup_tests[2-devices] N/A 17.2 ms N/A

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Copy link
Contributor

github-actions bot commented Nov 4, 2024

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

anta/tools.py Outdated
Comment on lines 446 to 462
while pending or not coros_ended:
while len(pending) < limit and not coros_ended:
try:
# NOTE: The `anext` built-in function is not available in Python 3.9
coro = await coroutines.__anext__() # pylint: disable=unnecessary-dunder-call
except StopAsyncIteration: # noqa: PERF203
coros_ended = True
else:
pending.add(asyncio.create_task(coro))

if not pending:
return

done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)

while done:
yield done.pop()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while pending or not coros_ended:
while len(pending) < limit and not coros_ended:
try:
# NOTE: The `anext` built-in function is not available in Python 3.9
coro = await coroutines.__anext__() # pylint: disable=unnecessary-dunder-call
except StopAsyncIteration: # noqa: PERF203
coros_ended = True
else:
pending.add(asyncio.create_task(coro))
if not pending:
return
done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
while done:
yield done.pop()
while pending or not coros_ended:
async for test in coroutines:
# Add tests to the pending set until the limit is reached or no more tests are available
if len(pending) < limit:
# Ensure the coroutine is scheduled to run and add it to the pending set
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.", limit)
break
if len(pending) < limit and not coros_ended:
coros_ended = True
logger.debug("All tests have been added to the pending set.")
if not pending:
logger.debug("No pending tests and all tests have been processed. Exiting.")
return
done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
while done:
yield done.pop()

Comment on lines +65 to +67
# AntaRunner returns test results as they finish, not necessarily in order
# TODO: Add CLI option to choose sorting order
ctx.obj["result_manager"].sort(sort_by=["name", "test", "result", "custom_field"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want this in this PR? (other perf impact I would think)

@gmuloc gmuloc modified the milestones: v1.3.0, v.1.4.0 Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor: ANTA NRFU RunInformation log should be adjusted to be less confusing
3 participants