Main Content

keepMeasuring

Class: matlab.perftest.TestCase
Namespace: matlab.perftest

Measure code with automatic looping

Description

keepMeasuring(testcase) instructs the testing framework to iterate through a while loop as many times as it needs to get an accurate measurement of performance.

Performance tests that execute too quickly for MATLAB® to time accurately are filtered with an assumption failure. With the keepMeasuring method, the testing framework can measure significantly faster code by automatically determining the number of times to iterate through code and measuring the average performance.

You cannot put a keepMeasuring-while loop between calls to startMeasuring and stopMeasuring. Similarly, you cannot call the startMeasuring and stopMeasuring methods inside a keepMeasuring-while loop.

example

keepMeasuring(testcase,label) labels the measurement with label. Measurements generated in the same test method and with the same label are accumulated and summed. The label is appended in angle brackets to the test element name in the Samples and TestActivity properties of the MeasurementResult object.

Input Arguments

expand all

Instance of the test case, specified as a matlab.perftest.TestCase object.

Measurement boundary label, specified as a valid MATLAB identifier. A valid MATLAB identifier is a character vector or string scalar of alphanumerics (AZ, az, 09) and underscores, such that the first character is a letter and the length of the character vector is less than or equal to namelengthmax.

Examples

expand all

Create a performance test class, ZerosTest. This parameterized performance test measures the creation of three different sizes of arrays of zeros.

classdef ZerosTest < matlab.perftest.TestCase
    properties (TestParameter)
        Size = {1e2,1e3,1e4};
    end
    
    methods(Test)
        function testOne(testCase,Size)
            A = zeros(Size);
        end
    end
end

Run the performance test. The time to create the first two arrays is too close to the precision of the framework and the tests are filtered.

results = runperf('ZerosTest');
Running ZerosTest
........
================================================================================
ZerosTest/testOne(Size=100) was filtered.
    Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
.. ......
================================================================================
ZerosTest/testOne(Size=1000) was filtered.
    Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
.... ....
================================================================================
ZerosTest/testOne(Size=10000) was filtered.
    Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================

Done ZerosTest
__________

Failure Summary:

     Name                           Failed  Incomplete  Reason(s)
    ============================================================================
     ZerosTest/testOne(Size=100)                X       Filtered by assumption.
    ----------------------------------------------------------------------------
     ZerosTest/testOne(Size=1000)               X       Filtered by assumption.
    ----------------------------------------------------------------------------
     ZerosTest/testOne(Size=10000)              X       Filtered by assumption.

To get an accurate measurement, modify the ZerosTest class to use a keepMeasuring-while loop. The performance testing framework measures the code within the keepMeasuring-while loop.

classdef ZerosTest < matlab.perftest.TestCase
    properties (TestParameter)
        Size = {1e2,1e3,1e4};
    end
    
    methods(Test)
        function testOne(testCase,Size)
            while(testCase.keepMeasuring)
                A = zeros(Size);
            end
        end
    end
end

Rerun the performance test.

results = runperf('ZerosTest');
Running ZerosTest
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... .......
Done ZerosTest
__________

Version History

Introduced in R2018b