runperf
Run set of tests for performance measurement
Description
results = runperf
runs all the tests in your current folder
for performance measurements and returns an array of matlab.perftest.TimeResult
objects. Each element in
results
corresponds to an element in the test suite.
The performance testing framework runs the tests using a variable number of
measurements to reach a sample mean with a 0.05 (5%) relative margin of error within
a 0.95 (95%) confidence level. It runs the tests 5 times to warm up the code, and
then between 4 and 256 times to collect measurements that meet the statistical
objectives. If the sample mean does not meet the 0.05 relative margin of error
within a 0.95 confidence level after 256 test runs, then the framework stops running
the test and displays a warning. In this case, the TimeResult
object
contains information for the 5 warm-up runs and 256 measurement runs.
The runperf
function provides a simple way to run a
collection of tests as a performance experiment.
results = runperf(___,
runs a set of tests with additional options specified by one or more name-value
arguments.Name,Value
)
Examples
Run Script as Performance Test
In your current folder, create a script-based test,
onesTest.m
, that uses three different methods to
initialize a 3000-by-1000 matrix of ones.
rows = 3000; cols = 1000; %% Ones Function X = ones(rows,cols); %% Loop Assignment Without Preallocation for r = 1:rows for c = 1:cols X(r,c) = 1; end end %% Loop Assignment With Preallocation X = zeros(rows,cols); for r = 1:rows for c = 1:cols X(r,c) = 1; end end
Run the script as a performance test. The returned
results
variable is a 1-by-3
TimeResult
array. Each element in the array
corresponds to one of the tests defined in
onesTest.m
.
results = runperf("onesTest")
Running onesTest .......... .......... ....... Done onesTest __________ results = 1×3 TimeResult array with properties: Name Valid Samples TestActivity Totals: 3 Valid, 0 Invalid. 23.1678 seconds testing time.
Display the measurement results for the second test, which loops the assignment without preallocation.
results(2)
ans = TimeResult with properties: Name: 'onesTest/LoopAssignmentWithoutPreallocation' Valid: 1 Samples: [4×7 table] TestActivity: [9×12 table] Totals: 1 Valid, 0 Invalid. 22.8078 seconds testing time.
Display the complete table of test measurements. The performance testing
framework ran five warm-up runs, followed by four measurement runs
(indicated as sample
in the Objective
column). Your results might vary.
results(2).TestActivity
ans = 9×12 table Name Passed Failed Incomplete MeasuredTime Objective Timestamp Host Platform Version TestResult RunIdentifier ___________________________________________ ______ ______ __________ ____________ _________ ____________________ ___________ ________ __________________________________ ______________________________ ____________________________________ onesTest/LoopAssignmentWithoutPreallocation true false false 2.5463 warmup 14-Oct-2022 13:51:36 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.5294 warmup 14-Oct-2022 13:51:38 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.4956 warmup 14-Oct-2022 13:51:41 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.5369 warmup 14-Oct-2022 13:51:43 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.535 warmup 14-Oct-2022 13:51:46 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.5856 sample 14-Oct-2022 13:51:49 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.5344 sample 14-Oct-2022 13:51:51 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.542 sample 14-Oct-2022 13:51:54 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b onesTest/LoopAssignmentWithoutPreallocation true false false 2.4653 sample 14-Oct-2022 13:51:56 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 1×1 matlab.unittest.TestResult 47ea2cab-5c34-4393-ba91-9715fb919d9b
Display the mean measured time for the second test. To exclude data
collected in the warm-up runs, use the values in the
Samples
property.
mean(results(2).Samples.MeasuredTime)
ans = 2.5318
To compare the different initialization methods in the script, create a
table of summary statistics from results
. In this
example, the ones
function was the fastest way to
initialize the matrix to ones. The performance testing framework made four
measurement runs for this test.
T = sampleSummary(results)
T = 3×7 table Name SampleSize Mean StandardDeviation Min Median Max ___________________________________________ __________ _________ _________________ ________ _________ _________ onesTest/OnesFunction 4 0.0052392 8.9302e-05 0.005171 0.0052078 0.0053703 onesTest/LoopAssignmentWithoutPreallocation 4 2.5318 0.049764 2.4653 2.5382 2.5856 onesTest/LoopAssignmentWithPreallocation 4 0.023947 0.00046027 0.023532 0.023921 0.024415
Create Class-Based Performance Tests
Compare the performance of various preallocation approaches by creating a test class that derives from matlab.perftest.TestCase
.
In a file named preallocationTest.m
in your current folder, create the preallocationTest
test class. The class contains four Test
methods that correspond to different approaches to creating a vector of ones. When you run any of these methods with the runperf
function, the function measures the time it takes to run the code inside the method.
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
Run performance tests for all the tests with "Indexing"
in their name. Your results might vary, and you might see a warning if runperf
does not meet statistical objectives.
results = runperf("preallocationTest","Name","*Indexing*")
Running preallocationTest .......... .......... .......... .. Done preallocationTest __________
results = 1×2 TimeResult array with properties: Name Valid Samples TestActivity Totals: 2 Valid, 0 Invalid. 3.011 seconds testing time.
To compare the preallocation methods, create a table of summary statistics from results
. In this example, the testIndexingOnLHS
method was the faster way to initialize the vector to ones.
T = sampleSummary(results)
T=2×7 table
Name SampleSize Mean StandardDeviation Min Median Max
__________________________________________ __________ ________ _________________ ________ ________ ________
preallocationTest/testIndexingWithVariable 17 0.1223 0.014378 0.10003 0.12055 0.15075
preallocationTest/testIndexingOnLHS 5 0.027557 0.0013247 0.026187 0.027489 0.029403
Input Arguments
tests
— Tests
string array | character vector | cell array of character vectors
Tests, specified as a string array, character vector, or cell array of character vectors. Use this argument to specify your test content. For example, you can specify a test file, a test class, a folder that contains test files, a namespace that contains test classes, or a project folder that contains test files.
Example: runperf("myTestFile.m")
Example: runperf(["myTestFile/test1"
"myTestFile/test3"])
Example: runperf("myNamespace.MyTestClass")
Example: runperf(pwd)
Example: runperf({'myNamespace.MyTestClass','myTestFile.m',pwd,'myNamespace.innerNamespace'})
Example: runperf("C:\projects\project1")
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: runperf(tests,Name="productA_*")
runs test elements
with a name that starts with "productA_"
.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: runperf(tests,"Name","productA_*")
runs test elements
with a name that starts with "productA_"
.
IncludeSubfolders
— Option to run tests in subfolders
false
or 0
(default) | true
or 1
Option to run tests in subfolders, specified as a numeric or logical
0
(false
) or
1
(true
). By default, the
framework runs tests in the specified folders, but not in their
subfolders.
IncludeInnerNamespaces
— Option to run tests in inner namespaces
false
or 0
(default) | true
or 1
Option to run tests in inner namespaces, specified as a numeric or
logical 0
(false
) or
1
(true
). By default, the
framework runs tests in the specified namespaces, but not in their inner
namespaces.
IncludeReferencedProjects
— Option to include tests from referenced projects
false
or 0
(default) | true
or 1
Option to include tests from referenced projects, specified as a numeric or logical
0
(false
) or 1
(true
). For more information on referenced projects, see Componentize Large Projects.
InvalidFileFoundAction
— Action to take against invalid test file
"warn"
(default) | "error"
Action to take against an invalid test file in a folder or namespace that the function is processing, specified as one of these values:
"warn"
— The function issues a warning for each invalid test file in a folder or namespace and runs the tests in the valid files."error"
— The function throws an error if it finds an invalid test file in a folder or namespace.
An invalid test file is a test file that the framework cannot run.
Examples include a test file that contains syntax errors, a
function-based test file that is missing local functions, and a file
with a Test
method that is passed an undefined
parameterization property.
BaseFolder
— Name of base folder
string array | character vector | cell array of character vectors
Name of the base folder that contains the test file, specified as a string array, character
vector, or cell array of character vectors. This argument
filters the test suite. For the testing framework to include a
test in the filtered suite, the Test
element
must be contained in one of the base folders specified by
BaseFolder
. If none of the
Test
elements match a base
folder, an empty test suite is returned. Use the wildcard
character (*
) to match any number of
characters. Use the question mark character
(?
) to match a single
character.
For test files defined in namespaces, the base folder is the parent of the top-level namespace folder.
DependsOn
— Names of files and folders that contain source code
string vector | character vector | cell vector of character vectors
Names of the files and folders that contain source code, specified as a string vector, character vector, or cell vector of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the file that defines the test must depend on the specified source code. If none of the test files depend on the source code, an empty test suite is returned.
The specified value must represent at least one existing file. If you specify a folder, the framework extracts the paths to the files within the folder.
You must have a MATLAB®
Test™ license to use DependsOn
. For more information about
selecting tests by source code dependency, see matlabtest.selectors.DependsOn
(MATLAB Test).
Example: DependsOn=["myFile.m" "myFolder"]
Example: DependsOn=["folderA" "C:\work\folderB"]
Name
— Name of test
string array | character vector | cell array of character vectors
Name of the test, specified as a string array, character vector, or cell array of
character vectors. This argument filters the test suite. For the testing framework to
include a test in the filtered suite, the Name
property of the
Test
element must match one of the names specified by
Name
. If none of the Test
elements have a
matching name, an empty test suite is returned. Use the wildcard character
(*
) to match any number of characters. Use the question mark
character (?
) to match a single character.
For a given test file, the name of a test uniquely identifies the smallest runnable portion of the test content. The test name includes the namespace name, filename (excluding the extension), procedure name, and information about parameterization.
ParameterProperty
— Name of parameterization property
string array | character vector | cell array of character vectors
Name of a test class property that defines a parameter used by the test, specified as a string
array, character vector, or cell array of character vectors. This argument filters the
test suite. For the testing framework to include a test in the filtered suite, the
Parameterization
property of the Test
element
must contain at least one of the property names specified by
ParameterProperty
. If none of the Test
elements have a matching property name, an empty test suite is returned. Use the
wildcard character (*
) to match any number of characters. Use the
question mark character (?
) to match a single character.
ParameterName
— Name of parameter
string array | character vector | cell array of character vectors
Name of a parameter used by the test, specified as a string array, character vector, or cell array of character vectors. MATLAB generates parameter names based on the test class property that defines the parameters. For example:
If the property value is a cell array, MATLAB generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions.
If the property value is a structure, MATLAB generates parameter names from the structure fields.
The ParameterName
argument filters the test suite. For the testing
framework to include a test in the filtered suite, the
Parameterization
property of the
Test
element must contain at least one of the
parameter names specified by ParameterName
. If none of
the Test
elements have a matching parameter name, an
empty test suite is returned. Use the wildcard character
(*
) to match any number of characters. Use the
question mark character (?
) to match a single
character.
ProcedureName
— Name of test procedure
string array | character vector | cell array of character vectors
Name of the test procedure, specified as a string array, character vector, or cell
array of character vectors. This argument filters the test suite. For the testing
framework to include a test in the filtered suite, the ProcedureName
property of the Test
element must match one of the procedure names
specified by ProcedureName
. If none of the Test
elements have a matching procedure name, an empty test suite is returned. Use the
wildcard character (*
) to match any number of characters. Use the
question mark character (?
) to match a single character.
In a class-based test, the name of a test procedure is the name of a
Test
method that contains the test. In a function-based test, it
is the name of a local function that contains the test. In a script-based test, it is a
name generated from the test section title. Unlike the name of a test suite element, the
name of a test procedure does not include any namespace name, filename, or information
about parameterization.
Superclass
— Name of class that test class derives from
string array | character vector | cell array of character vectors
Name of the class that the test class derives from, specified as a string array,
character vector, or cell array of character vectors. This argument filters the test
suite. For the testing framework to include a test in the filtered suite, the
TestClass
property of the Test
element must
point to a test class that derives from one of the classes specified by
Superclass
. If none of the Test
elements match
a class, an empty test suite is returned.
Tag
— Name of tag
string array | character vector | cell array of character vectors
Name of a tag used by the test, specified as a string array, character vector, or cell
array of character vectors. This argument filters the test suite. For the testing
framework to include a test in the filtered suite, the Tags
property
of the Test
element must contain at least one of the tag names
specified by Tag
. If none of the Test
elements
have a matching tag name, an empty test suite is returned. Use the wildcard character
(*
) to match any number of characters. Use the question mark
character (?
) to match a single character.
Tips
To customize the statistical objectives of the performance test, use the
TimeExperiment
class to construct and run the performance test.When you use shared test fixtures in your tests and specify the input to the
runperf
function as a string array or cell array of character vectors, the testing framework sorts the array to reduce shared fixture setup and teardown operations. As a result, the tests might run in an order that is different from the order of elements in the input array. For more information, seesortByFixtures
.When you write class-based tests, you can run your tests as a standalone application (requires MATLAB Compiler™). Compiling performance tests is not currently supported. For more information, see Compile MATLAB Unit Tests.
Alternatives
To create a test suite explicitly, you can use the testsuite
function or the matlab.unittest.TestSuite
methods that
create a suite. Then, you can run your performance test with the run
method of your specified TimeExperiment
.
Version History
Introduced in R2016aR2024a: Include inner namespaces using IncludeInnerNamespaces
name-value argument, renamed from IncludeSubpackages
The IncludeSubpackages
name-value argument is now named IncludeInnerNamespaces
. The behavior remains the same, and existing instances of IncludeSubpackages
in your code continue to work as expected. There are no plans to remove support for existing references to IncludeSubpackages
.
R2024a: Specify any type of source file when filtering test suite by source code dependency
If you have a MATLAB
Test license, you can specify any type of source file using the
DependsOn
name-value argument. In previous releases, you can
specify files only with a .m
, .p
,
.mlx
, .mlapp
, .mat
, or
.slx
extension.
R2023a: Filter test suite by source code dependency
You can filter a test suite by test file dependency on specified source code. Use the
DependsOn
name-value argument (requires MATLAB
Test) to specify the source files and folders.
R2023a: Run tests that verify requirement sets
If you have Requirements Toolbox™ and MATLAB
Test installed, you can use the runperf
function to
run tests that verify requirement sets. To run tests, specify one or more
requirement set files as a string scalar or string vector. For example,
results = runperf("myRequirementSet.slreqx")
runs the tests
that verify the specified requirement set.
R2023a: Number of warm-up measurements has increased
The number of times that runperf
exercises the test code to
warm it up has increased from four to five. This change results in typically fewer
samples required to meet the objective relative margin of error.
If your code relies on the previous value, you might need to update your code. For
example, if you use warmupTable = results(1).TestActivity(1:4,:)
to create a table of warm-up measurements, replace 4
with
5
.
R2022b: Specify action to take against invalid test files
To specify whether the framework issues a warning or throws an error when it
encounters an invalid test file in a folder or namespace, use the
InvalidFileFoundAction
name-value argument.
R2022b: Parameter names generated from cell arrays are more descriptive
When you assign a nonempty cell array to a parameterization property, the testing framework generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions. In previous releases, if the property value is a cell array of character vectors, the framework generates parameter names from the values in the cell array. Otherwise, the framework specifies parameter names as value1
, value2
, …, valueN
.
If your code uses parameter names to create or filter test suites, replace the old parameter
names with the descriptive parameter names. For example, update suite =
testsuite(pwd,"ParameterName","value1")
by replacing value1
with a descriptive parameter name.
R2022a: IncludeSubfolders
treats folders and namespaces the same way
The IncludeSubfolders
name-value argument treats folders and
namespaces the same way. For example,
runperf(pwd,IncludeSubfolders=true)
runs all the tests in the
current folder and any of its subfolders, including namespace folders. In previous
releases, IncludeSubfolders
ignores namespace folders.
R2021b: runperf
ignores project files that do not define test procedures
The runperf
function ignores any files in a MATLAB project that do not define test procedures. For example, if an
abstract TestCase
class definition file is labeled with the
Test
classification, the function ignores it. In previous
releases, MATLAB produces an error if runperf
is called on a
project that uses the Test
classification for any files other
than concrete test files.
R2021b: Tests in projects cannot run without the Java Virtual Machine (JVM) software
If MATLAB runs without the Java® Virtual Machine (JVM®) software, runperf
cannot run the tests in a
MATLAB project. The reason is that the project cannot be opened without the
JVM software. In previous releases, when MATLAB runs without the JVM software, runperf
creates a suite from the test
files in the project and runs the suite.
R2019a: Run tests in a MATLAB project
When your current folder is a project root folder or when you pass the path to a
project root folder to the runperf
function, the function runs
all test files contained in the specified project that are labeled with the
Test
classification.
R2019a: Run tests from referenced projects
To run the tests from referenced projects, use the
IncludeReferencedProjects
name-value argument.
R2019a: Performance test results are returned as TimeResult
objects
The runperf
function returns a
matlab.perftest.TimeResult
array containing the results of the
specified performance tests. In previous releases, the function returns an array of
matlab.unittest.measurement.MeasurementResult
objects.
R2019a: Maximum number of sample measurements has increased
The default maximum number of sample measurements that
runperf
makes when running performance measurements has
increased from 32 to 256.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)