⚡️ Speed up function test_register_func_with_custom_action by 62%
#40
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.
📄 62% (0.62x) speedup for
test_register_func_with_custom_actioninframework/py/flwr/clientapp/client_app_test.py⏱️ Runtime :
309 microseconds→191 microseconds(best of19runs)📝 Explanation and details
The optimized version replaces heavy
Mockobjects with lightweight custom classes for test data objects, achieving a 61% speedup.Key optimization:
Mock(metadata=Mock(message_type=...))which incurs significant overhead, the code uses simple inline classesMetaandMessageObjwith__slots__for memory efficiency and faster instantiation.object()for context: ReplacesMock()with plainobject()since the test only checks identity (_cxt is context), eliminating unnecessary Mock infrastructure.Mock()only where needed - foroutput_message(identity assertion) andfunc_code(.assert_called_once()method).Performance impact: The line profiler shows the original nested Mock creation took ~4.2ms (34.6% of runtime), while the optimized lightweight objects take only ~0.02ms (0.3% of runtime) - a 99% reduction in object setup time.
Test case effectiveness: This optimization particularly benefits test suites with many iterations or parameterized tests (like the
@pytest.mark.parametrizedecorator used here), where object setup overhead compounds across multiple test runs. The speedup scales well for large test batches since the per-iteration setup cost is dramatically reduced.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
To edit these changes
git checkout codeflash/optimize-test_register_func_with_custom_action-mhcv6q62and push.