Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/matlab-software-development/matlab-write-performance-tests

matlab-write-performance-tests

Verified

Writes MATLAB performance tests using the matlab.perftest.TestCase framework. Use when asked to write, create, or add performance tests for MATLAB code, benchmark functions, measure execution time with statistical rigor, or use runperf.

references/FeaturePerformanceTest.m

View on GitHub
classdef FeaturePerformanceTest < matlab.perftest.TestCase
%FeaturePerformanceTest Performance tests for <Feature>
%   Tests <function/API> at various data sizes using the
%   matlab.perftest.TestCase framework.
%
%   Run with: results = runperf('FeaturePerformanceTest');

    properties (MethodSetupParameter)
        % Use struct for complex parameter sets
        DataSize = struct('Small', struct('n', 100, 'desc', 'baseline'), ...
                          'Medium', struct('n', 1000, 'desc', 'typical'), ...
                          'Large', struct('n', 10000, 'desc', 'stress'))
    end

    properties
        inputData
        expectedSize
        rngState
    end

    methods (TestMethodSetup)
        function setupData(testCase, DataSize)
            % ALL setup must be here, outside measurement boundary
            testCase.rngState = rng(42, 'twister');
            n = DataSize.n;
            testCase.inputData = randn(n, 1);
            testCase.expectedSize = [n 1];
        end
    end

    methods (TestMethodTeardown)
        function restoreRng(testCase)
            rng(testCase.rngState);
        end
    end

    methods (Test)
        function testFastFunction(testCase, DataSize) %#ok<INUSD>
            % Use keepMeasuring for fast operations (<10ms)
            data = testCase.inputData;
            while testCase.keepMeasuring
                result = targetFunction(data);
            end
            testCase.verifyEqual(size(result), testCase.expectedSize);
        end

        function testSlowFunction(testCase, DataSize) %#ok<INUSD>
            % For slower operations (>10ms), no keepMeasuring needed
            % The entire Test method body is measured
            data = testCase.inputData;
            result = slowTargetFunction(data);
            testCase.verifyNotEmpty(result);
        end

        function testWithBoundaries(testCase, DataSize) %#ok<INUSD>
            % Use startMeasuring/stopMeasuring for mid-test setup
            data = testCase.inputData;

            % Pre-computation (not measured)
            preparedData = preprocess(data);

            testCase.startMeasuring();
            result = targetFunction(preparedData);
            testCase.stopMeasuring();

            % Verification (not measured)
            testCase.verifyTrue(isvalid(result));
        end
    end
end

% Copyright 2026 The MathWorks, Inc.
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 319kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 319kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 319kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 196kUpdated Claude CodeCodexVerifiedInstall