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

[POC] Optimize parallel context loading in the TestContext framework #34323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gnosly
Copy link

@gnosly gnosly commented Jan 25, 2025

Hi,

when I recently activated the junit thread parallelisation in my project I discovered that different tests are waiting each other even if they are creating different application contexts.
In fact a synchronised block inside DefaultCacheAwareContextLoaderDelegate.loadContext is preventing different threads to load also different contexts.
Waiting the context creation is essential to reuse the context when different tests are requesting the same context (context caching). But it is inefficient when the contexts are different.

So I replaced the synchronised block with a lazy creation of the context (using the combination of Map, Future and a separated thread pool).

Now during the test execution the DefaultCacheAwareContextLoaderDelegate will put the context key and the lazy computation in the contextMap - and this operation is really fast. Then DefaultCacheAwareContextLoaderDelegate will wait the context creation during the Future.get invocation . If different tests are requesting the same context they will wait the same unique computation, preserving the caching feature. If they are requesting different contexts the creation of them will be scheduled in parallel.

I prepared this poc to show you the idea. If you believe this is correct, please give me some guideline in order to prepare a better pull request.

I also created this project that reproduce the bottleneck.

Just to give you an real example, with this change I reduce the testing time from 4 to 3 minutes for a project.

Please let me know,
Thank you

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 25, 2025
@sbrannen sbrannen added the in: test Issues in the test module label Jan 25, 2025
@sbrannen sbrannen changed the title [POC] Optimisation in context creation for testing [POC] Optimize parallel context creation in the TestContext framework Jan 25, 2025
@sbrannen sbrannen changed the title [POC] Optimize parallel context creation in the TestContext framework [POC] Optimize parallel context loading in the TestContext framework Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants