matlab.perftest.TestCase.keepMeasuring - Measure code with automatic looping - MATLAB (original) (raw)
Main Content
Class: matlab.perftest.TestCase
Namespace: matlab.perftest
Measure code with automatic looping
Syntax
Description
keepMeasuring([testcase](#mw%5F23ca2be6-1640-4e4f-82a6-82bd7862ef1a%5Fsep%5Fshared-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 thekeepMeasuring
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 tostartMeasuring
and stopMeasuring
. Similarly, you cannot call the startMeasuring
and stopMeasuring
methods inside akeepMeasuring-while
loop.
keepMeasuring([testcase](#mw%5F23ca2be6-1640-4e4f-82a6-82bd7862ef1a%5Fsep%5Fshared-testcase),[label](#d126e1306091))
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
andTestActivity
properties of the MeasurementResult
object.
Input Arguments
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 (A
–Z
,a
–z
,0
–9
) 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
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