Main Content

빠르게 실행되는 테스트 코드 측정하기

너무 빠르게 실행되어 MATLAB®이 정확하게 측정할 수 없는 성능 테스트는 가정 실패로 필터링됩니다. keepMeasuring 메서드를 사용하면 테스트 프레임워크가 코드를 반복할 횟수를 자동으로 결정하여 평균 성능을 측정하기 때문에 실행 속도가 매우 빠른 코드를 측정할 수 있습니다.

현재 작업 폴더에, 여러 사전할당 메서드를 비교하는 클래스 기반 테스트 PreallocationTest.m을 만듭니다. 테스트 메서드에 가설 검정(Qualification)이 포함되어 있으므로 startMeasuring 메서드와 stopMeasuring 메서드를 사용하여 측정할 코드의 범위를 정의합니다.

classdef PreallocationTest < matlab.perftest.TestCase
    methods(Test)
        function testOnes(testCase)
            testCase.startMeasuring
            x = ones(1,1e5);
            testCase.stopMeasuring
            testCase.verifyEqual(size(x),[1 1e5])
        end
        
        function testIndexingWithVariable(testCase)
            import matlab.unittest.constraints.IsSameSetAs
            testCase.startMeasuring
            id = 1:1e5;
            x(id) = 1;
            testCase.stopMeasuring
            testCase.verifyThat(x,IsSameSetAs(1))
        end
        
        function testIndexingOnLHS(testCase)
            import matlab.unittest.constraints.EveryElementOf
            import matlab.unittest.constraints.IsEqualTo
            testCase.startMeasuring
            x(1:1e5) = 1;
            testCase.stopMeasuring
            testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))
        end
        
        function testForLoop(testCase)
            testCase.startMeasuring
            for i=1:1e5
                x(i) = 1;
            end
            testCase.stopMeasuring
            testCase.verifyNumElements(x,1e5)
        end
    end
end

성능 테스트로 PreallocationTest를 실행합니다. 측정값이 프레임워크의 정밀도에 너무 가깝기 때문에 두 개의 테스트가 필터링됩니다.

results = runperf('PreallocationTest');
Running PreallocationTest
........
================================================================================
PreallocationTest/testOnes was filtered.
    Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
.. .......... ....
================================================================================
PreallocationTest/testIndexingOnLHS was filtered.
    Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
...... ..
Done PreallocationTest
__________

Failure Summary:

     Name                                 Failed  Incomplete  Reason(s)
    ==================================================================================
     PreallocationTest/testOnes                       X       Filtered by assumption.
    ----------------------------------------------------------------------------------
     PreallocationTest/testIndexingOnLHS              X       Filtered by assumption.

측정된 코드를 자동으로 순환하고 측정 결과의 평균을 내도록 프레임워크에 지시하려면 startMeasuringstopMeasuring 대신 keepMeasuring-while 루프를 사용하도록 PreallocationTest를 수정하십시오.

classdef PreallocationTest < matlab.perftest.TestCase
    methods(Test)
        function testOnes(testCase)
            while(testCase.keepMeasuring)
                x = ones(1,1e5);
            end
            testCase.verifyEqual(size(x),[1 1e5])
        end
        
        function testIndexingWithVariable(testCase)
            import matlab.unittest.constraints.IsSameSetAs
            while(testCase.keepMeasuring)
                id = 1:1e5;
                x(id) = 1;
            end
            testCase.verifyThat(x,IsSameSetAs(1))
        end
        
        function testIndexingOnLHS(testCase)
            import matlab.unittest.constraints.EveryElementOf
            import matlab.unittest.constraints.IsEqualTo
            while(testCase.keepMeasuring)
                x(1:1e5) = 1;
            end
            testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))
        end
        
        function testForLoop(testCase)
            while(testCase.keepMeasuring)
                for i=1:1e5
                    x(i) = 1;
                end
            end
            testCase.verifyNumElements(x,1e5)
        end
    end
end

테스트를 다시 실행하십시오. 모든 테스트가 완료됩니다.

results = runperf('PreallocationTest');
Running PreallocationTest
.......... .......... .......... ..
Done PreallocationTest
__________

결과를 봅니다.

sampleSummary(results)
ans =

  4×7 table

                       Name                       SampleSize       Mean       StandardDeviation       Min          Median         Max    
    __________________________________________    __________    __________    _________________    __________    __________    __________

    PreallocationTest/testOnes                        4         3.0804e-05       1.8337e-07        3.0577e-05    3.0843e-05    3.0953e-05
    PreallocationTest/testIndexingWithVariable        4         0.00044536       1.7788e-05        0.00042912    0.00044396    0.00046441
    PreallocationTest/testIndexingOnLHS               4         5.6352e-05       1.8863e-06        5.5108e-05    5.5598e-05    5.9102e-05
    PreallocationTest/testForLoop                     4          0.0097656       0.00018202         0.0096181     0.0097065      0.010031

참고 항목

|

관련 항목