Main Content

matlab.perftest.TestCase class

Package: matlab.perftest
Superclasses: matlab.unittest.TestCase

Superclass of matlab.perftest performance test classes

Description

Use the matlab.perftest.TestCase class to write class-based performance tests that can define measurement boundaries. By default, the framework measures performance around the test method boundary. However, test classes that inherit from matlab.perftest.TestCase can use the startMeasuring and stopMeasuring methods to define boundaries to measure specific code segments.

The matlab.perftest.TestCase derives from the matlab.unittest.TestCase class.

Construction

The testing framework constructs the matlab.perftest.TestCase instances.

Methods

keepMeasuringMeasure code with automatic looping
startMeasuringDesignate start of measurement boundary
stopMeasuringDesignate end of measurement boundary

Inherited Methods

addTeardownDynamically add teardown code to test case
applyFixture Use fixture with test case
createTemporaryFolder Create temporary folder
forInteractiveUseCreate test case for interactive use
getSharedTestFixtures Provide access to shared test fixtures
logRecord diagnostic information during test execution
onFailureDynamically add diagnostics for test failures
runRun tests corresponding to test case

Also, the TestCase class inherits methods from these classes:

matlab.unittest.qualifications.AssertableQualification to validate preconditions of a test
matlab.unittest.qualifications.AssumableQualification to filter test content
matlab.unittest.qualifications.FatalAssertableQualification to abort test execution
matlab.unittest.qualifications.VerifiableQualification to produce soft-failure conditions

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a performance test class, preallocationTest. The performance testing framework measures the time for each of the four test methods.

classdef preallocationTest < matlab.perftest.TestCase
    methods(Test)
        function testOnes(testCase)
            x = ones(1,1e7);
        end
        
        function testIndexingWithVariable(testCase)
            id = 1:1e7;
            x(id) = 1;
        end
        
        function testIndexingOnLHS(testCase)
            x(1:1e7) = 1;
        end
        
        function testForLoop(testCase)
            for i=1:1e7
                x(i) = 1;
            end
        end
        
    end
end

Create a performance test class, fprintfTest. The performance testing framework measures the code between the calls to the startMeasuring and stopMeasuring methods. This boundary restricts the performance testing framework to measuring only the call to the fprintf function. It excludes setup and teardown actions, and qualifications testing.

classdef fprintfTest < matlab.perftest.TestCase
    methods(Test)
        function testPrintingToFile(testCase)
            file = tempname;
            fid = fopen(file, 'w');
            testCase.assertNotEqual(fid, -1, 'IO Problem');
            
            stringToWrite = repmat('abcdef', 1, 1000000);
            
            testCase.startMeasuring();
            fprintf(fid, '%s', stringToWrite);
            testCase.stopMeasuring();
            
            testCase.verifyEqual(fileread(file), stringToWrite);
            fclose(fid);
        end
    end
end

Version History

Introduced in R2016a