Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.perftest.TestCase 类

命名空间: matlab.perftest
超类: matlab.unittest.TestCase

matlab.perftest 性能测试类的超类

描述

使用 matlab.perftest.TestCase 类编写基于类的性能测试,可用于定义测量边界。默认情况下,测试框架会围绕测试方法边界来测量性能。但是,从 matlab.perftest.TestCase 继承的测试类可以使用 startMeasuringstopMeasuring 方法来定义边界,以测量特定的代码段。

matlab.perftest.TestCase 派生自 matlab.unittest.TestCase 类。

构造

测试框架可以构造 matlab.perftest.TestCase 实例。

方法

keepMeasuring使用自动循环测量代码
startMeasuring指定测量开始边界
stopMeasuring指定测量结束边界

继承的方法

addTeardown向测试用例动态添加拆解代码
applyFixture 将脚手架与测试用例配合使用
createTemporaryFolder Create temporary folder
forInteractiveUse创建供交互测试的测试用例
getSharedTestFixtures 提供对共享测试脚手架的访问权限
log在测试执行期间记录诊断信息
onFailure动态添加测试失败的诊断
run运行与测试用例对应的测试

TestCase 类还从以下类继承方法:

matlab.unittest.qualifications.Assertable用来确认测试先决条件的验证
matlab.unittest.qualifications.Assumable用于过滤测试内容的验证
matlab.unittest.qualifications.FatalAssertable用于中止测试执行的验证
matlab.unittest.qualifications.Verifiable产生软失败条件的验证

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

创建性能测试类 preallocationTest。性能测试框架测量所有四个测试方法的时间。

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

创建性能测试类 fprintfTest。性能测试框架测量 startMeasuringstopMeasuring 方法调用之间的代码。此边界限制性能测试框架只测量对 fprintf 函数的调用。不包括设置和拆解操作以及验证测试。

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

版本历史记录

在 R2016a 中推出