Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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

startMeasuringDesignate start of measurement boundary
stopMeasuringDesignate end of measurement boundary

Inherited Methods

addTeardownDynamically add teardown routine
applyFixtureUse fixture with TestCase
forInteractiveUseCreate TestCase for interactive use
getSharedTestFixturesProvide access to shared test fixtures
logRecord diagnostic information
onFailureDynamically add diagnostics for test failures
runRun TestCase test

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

expand 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

Introduced in R2016a

Was this topic helpful?