Main Content

stopMeasuring

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

指定测量结束边界

说明

示例

stopMeasuring(testcase) 指定测量结束边界。调用此方法和 startMeasuring 方法,以限制只测量 startMeasuringstopMeasuring 方法调用之间的代码。定义此边界以允许在测量中排除设置、验证和拆解代码。

性能框架允许在 Test 属性标记的每个方法中对 startMeasuringstopMeasuring 方法进行多次非嵌套调用。创建有界性能测试时,请记住以下几点:

  • 调用 startMeasuring 方法的测试方法必须在同一测试方法的作用域内调用 stopMeasuring 方法。

  • 调用 startMeasuring 方法之后,必须在同一测试方法的作用域内调用 stopMeasuring 方法。同样,调用 stopMeasuring 方法之前必须先调用 startMeasuring

  • 您不能在其条件中具有 keepMeasuring 方法的 while 循环内调用 startMeasuringstopMeasuring 方法。同样,您不能在对 startMeasuringstopMeasuring 的调用之间放置一个具有 keepMeasuring 条件的 while 循环。

  • 如果测试方法多次调用 startMeasuringstopMeasuring,则性能框架会对测量值进行累加并求和。

如果框架在某个测试方法中遇到不支持的 startMeasuringstopMeasuring 用法,它会将对应的 MeasurementResult 实例标记为无效。

示例

stopMeasuring(testcase,label) 指定测量结束边界,并用 label 为测量添加标签。指定带标签的测量边界类似于指定不带标签的测量边界。在调用具有标签的 stopMeasuring 之前,必须在同一测试方法的作用域内调用具有相同标签的 startMeasuring。如果一个测试方法有多个具有相同标签的边界,则性能框架会根据标签累积测量值并计算其总和。性能框架不支持嵌套的测量边界。

标签以尖括号括起,追加到 MeasurementResultSamplesTestActivity 属性中的测试元素名称之后。

输入参数

全部展开

测试用例的实例,指定为 matlab.perftest.TestCase 对象。

测量边界标签,指定为有效的 MATLAB 标识符。有效的 MATLAB 标识符是由字母数字(AZaz09)和下划线构成的字符向量或字符串标量,第一个字符应为字母且字符向量长度须小于或等于 namelengthmax

示例

全部展开

创建性能测试类 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

创建性能测试类 fprintfTest2。多个边界(对 startMeasuringstopMeasuring 的调用)使性能框架能够测量打开文件、写入文件和关闭文件的代码。

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

运行性能测试并查看样本摘要。性能框架测量出,testPrintingToFile 测试打开、写入和关闭文件的平均时间约为 0.02 秒。您的结果可能有所不同。

results = runperf('fprintfTest2');
T = sampleSummary(results)
Running fprintfTest2
........
Done fprintfTest2
__________


T =

  1×7 table

                 Name                  SampleSize      Mean      StandardDeviation      Min        Median       Max   
    _______________________________    __________    ________    _________________    ________    ________    ________

    fprintfTest2/testPrintingToFile        4         0.017003        0.0004943        0.016651    0.016814    0.017736

创建性能测试类 examplePerfTest。第一个测试具有带标签的测试边界,用于生成随机数数组、用单一输出测量对 svd 的调用,以及用多个输出测量对 svd 的调用。第二个测试在对 svd 的调用周围具有不带标签的边界。

classdef examplePerfTest < matlab.perftest.TestCase
    methods(Test)
        function testSVD1(testCase)
            testCase.startMeasuring('arrayGen')
            X = rand(1000);
            testCase.stopMeasuring('arrayGen')
            
            testCase.startMeasuring('SVD_1out')
            S = svd(X);
            testCase.stopMeasuring('SVD_1out')
            
            testCase.startMeasuring("SVD_3out")
            [U2,S2,V2] = svd(X);
            testCase.stopMeasuring("SVD_3out")
            
            testCase.verifyEqual(S,diag(S2),'RelTol',1e-14)
        end
        
        function testSVD2(testCase)
            sz = 732;
            X = rand(sz);
            
            testCase.startMeasuring()
            [U,S,V] = svd(X);
            testCase.stopMeasuring()
            
            testCase.verifyTrue(isdiag(S))
            testCase.verifyTrue(issorted(diag(S),'descend'))
            testCase.verifySize(S,[sz sz 1])
        end
    end
end

运行性能测试并查看样本摘要。您的结果可能有所不同。来自 testSVD1 的标签以尖括号括起,追加到结果中的测试元素名称之后。

results = runperf('examplePerfTest');
T = sampleSummary(results)
Running examplePerfTest
..........
..........
..........
..........
Done examplePerfTest
__________


T =

  4×7 table

                   Name                    SampleSize      Mean       StandardDeviation       Min        Median        Max   
    ___________________________________    __________    _________    _________________    _________    _________    ________

    examplePerfTest/testSVD1 <arrayGen>        21        0.0096508        0.0012428        0.0087596    0.0092564    0.013911
    examplePerfTest/testSVD1 <SVD_1out>        21          0.11978        0.0098172          0.10585      0.12274     0.13575
    examplePerfTest/testSVD1 <SVD_3out>        21          0.30664         0.020991          0.26882       0.3051     0.35018
    examplePerfTest/testSVD2                   11          0.13294         0.011135          0.11127      0.13557     0.15162

版本历史记录

在 R2016a 中推出