How to extract execution timing (Initialization, Execution, Termination times) from Simulink Test?

22 ビュー (過去 30 日間)
Akshaya
Akshaya 2025 年 2 月 3 日 10:17
コメント済み: Akshaya 2025 年 2 月 6 日 9:36
I am trying to extract the execution timing (Initialization, Execution, and Termination times) from my Simulink Test Manager Baseline Tests to analyze different modes for faster execution.
What I Am Doing:
  • run multiple Baseline Tests programmatically from a Simulink Test Suite using MATLAB code.
  • tried to extract the Initialization, Execution, and Termination times after each test run.
  • tried using Simulink.SimulationOutput.SimulationMetadata.TimingInfo. But this does not work in my case.
% Define the test file path
testFilePath = 'testfile.mldatx';
% Load the test file
sltest.testmanager.load(testFilePath);
% Get all test files currently loaded
testFiles = sltest.testmanager.getTestFiles();
% Debugging: Check if test files are loaded
if isempty(testFiles)
error("[ERROR] No test files found! Ensure the test file exists and is valid.");
end
fprintf("[DEBUG] Number of Test Files: %d\n", numel(testFiles));
% List all test files
for i = 1:numel(testFiles)
fprintf("[DEBUG] Test File %d: %s\n", i, testFiles(i).Name);
end
% Select the first test file
testFile = testFiles(1);
% Get all test suites inside the test file
testSuites = testFile.getTestSuites();
fprintf("[DEBUG] Number of Test Suites in File: %d\n", numel(testSuites));
% List all test suites
for i = 1:numel(testSuites)
fprintf("[DEBUG] Test Suite %d: %s\n", i, testSuites(i).Name);
end
% Select the first test suite
testSuite = testSuites(1);
% Get all test cases in this suite
testCases = testSuite.getTestCases();
fprintf("[DEBUG] Number of Test Cases in Suite '%s': %d\n", testSuite.Name, numel(testCases));
% List all test cases
for i = 1:numel(testCases)
fprintf("[DEBUG] Test Case %d: %s\n", i, testCases(i).Name);
end
% Select the first test case
testCase = testCases(1);
fprintf("[DEBUG] Selected Test Case: %s\n", testCase.Name);
% Run the test case
fprintf("[DEBUG] Running the test case: %s\n", testCase.Name);
resultSet = run(testCase);
% Get test case results
testCaseResults = resultSet.getTestCaseResults();
fprintf("[DEBUG] Number of Test Case Results: %d\n", numel(testCaseResults));
% List test case results
for i = 1:numel(testCaseResults)
fprintf("[DEBUG] Test Case Result %d: %s\n", i, testCaseResults(i).Name);
end
% Select the first test case result
testResult = testCaseResults(1);
% Check available properties in test case result
fprintf("[DEBUG] Listing available properties in test case result:\n");
disp(properties(testResult));
% extracting Simulation Metadata
if isprop(testResult, 'SimulationMetadata')
simMetadata = testResult.SimulationMetadata;
fprintf("[DEBUG] Listing available properties in SimulationMetadata:\n");
disp(properties(simMetadata));
% extract Timing Info
if isprop(simMetadata, 'TimingInfo')
timingInfo = simMetadata.TimingInfo;
fprintf("\nExecution Timing Summary:\n");
fprintf(" Initialization Time : %.4f sec\n", timingInfo.InitializationElapsedWallTime);
fprintf(" Execution Time : %.4f sec\n", timingInfo.ExecutionElapsedWallTime);
fprintf(" Termination Time : %.4f sec\n", timingInfo.TerminationElapsedWallTime);
fprintf(" Total Simulation Time: %.4f sec\n", timingInfo.TotalElapsedWallTime);
else
fprintf("[ERROR] No 'TimingInfo' found in SimulationMetadata.\n");
end
else
fprintf("[ERROR] No 'SimulationMetadata' property found in test result.\n");
end
% extracting Baseline Run (in case the timing is stored there)
if isprop(testResult, 'Baseline')
baselineRun = testResult.getBaselineRun();
fprintf("[DEBUG] Listing available properties in Baseline Run:\n");
disp(properties(baselineRun));
end
% Unload test file to free memory
fprintf("[DEBUG] Unloading test file to free memory.\n");
sltest.testmanager.clear;

回答 (1 件)

Karan Singh
Karan Singh 2025 年 2 月 6 日 9:22
How about using a "tic-toc".
This way you can manually measure execution time by wrapping "run(testCase)" inside "tic" and "toc".
tic; % Start timing
resultSet = run(testCase); % Run test
executionTime = toc; % End timing
fprintf("\nManual Execution Timing:\n");
fprintf(" Total Execution Time: %.4f sec\n", executionTime);
Karan
  1 件のコメント
Akshaya
Akshaya 2025 年 2 月 6 日 9:36
I just don't want execution time alone. I want to check the intializationTime, and Termination time too. Basically when tried to use the SimulationMetadata to run a Model, there is a TimingInfo feild which gives me this.
But when trying to run a Test, why is there no TimingInfo feild in SimulationMetadata?
To my undestanding even the test takes some time to terminate actually. After the model compiles and then the test executes. So I want to know all those timing. Is there any possible way ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOutputs についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by