From b1566966f8381605fee455dde9aeb9dda99cb3db Mon Sep 17 00:00:00 2001 From: Simon Heybrock Date: Wed, 30 Oct 2024 07:36:25 +0100 Subject: [PATCH] Try sharing common parts of workflow compute to make tests run faster This is demonstrating an approach that could save some test runtime. In this example a parametrized test is running a nearly identical workflow 8 times, each time loading data from scratch, etc. The change here moves some of the common parts into a module-scope fixture. On my machine this reduces the test runtime from 50 seconds to 22 seconds. I do not know if this is a great solution. If we want to go with this, a helper utility could be made: Given a sciline.Pipeline and a set of keys, compute all intermediate results that to not depend on those keys and set the results as "static" data in the workflow. This would avoid error-prone manual authoring of fixtures like the one I added here. --- tests/loki/iofq_test.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tests/loki/iofq_test.py b/tests/loki/iofq_test.py index e979ce07..c257817a 100644 --- a/tests/loki/iofq_test.py +++ b/tests/loki/iofq_test.py @@ -13,6 +13,7 @@ from ess import loki, sans from ess.sans.conversions import ElasticCoordTransformGraph from ess.sans.types import ( + BackgroundRun, BackgroundSubtractedIofQ, BackgroundSubtractedIofQxy, BeamCenter, @@ -21,15 +22,20 @@ CorrectForGravity, Denominator, DimsToKeep, + Incident, IofQ, IofQxy, MaskedData, + MaskedSolidAngle, Numerator, QBins, QxBins, QyBins, ReturnEvents, SampleRun, + TofMonitor, + Transmission, + TransmissionRun, UncertaintyBroadcastMode, WavelengthBands, WavelengthBins, @@ -85,6 +91,23 @@ def test_pipeline_can_compute_IofQ(uncertainties, qxy: bool): assert_identical(result, reference) +@pytest.fixture(scope='module') +def workflow_with_data(): + pipeline = make_workflow() + pipeline[BeamCenter] = sans.beam_center_from_center_of_mass(pipeline) + for run in (SampleRun, BackgroundRun): + keys = ( + MaskedData[run], + MaskedSolidAngle[run], + TofMonitor[run, Incident], + TofMonitor[TransmissionRun[run], Incident], + TofMonitor[TransmissionRun[run], Transmission], + ) + for key, value in pipeline.compute(keys).items(): + pipeline[key] = value + return pipeline + + @pytest.mark.parametrize( 'uncertainties', [UncertaintyBroadcastMode.drop, UncertaintyBroadcastMode.upper_bound], @@ -98,10 +121,11 @@ def test_pipeline_can_compute_IofQ(uncertainties, qxy: bool): BackgroundSubtractedIofQxy, ], ) -def test_pipeline_can_compute_IofQ_in_event_mode(uncertainties, target): - pipeline = make_workflow() +def test_pipeline_can_compute_IofQ_in_event_mode( + uncertainties, target, workflow_with_data +): + pipeline = workflow_with_data.copy() pipeline[UncertaintyBroadcastMode] = uncertainties - pipeline[BeamCenter] = sans.beam_center_from_center_of_mass(pipeline) reference = pipeline.compute(target) pipeline[ReturnEvents] = True result = pipeline.compute(target)