[POC] Optimize parallel context loading in the TestContext framework #34323
+108
−55
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theFuture.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