使用 Simulink Coverage 收集代码覆盖率度量
此示例展示了如何使用 Simulink® Coverage™ 在软件在环(SIL) 或处理器在环(PIL)仿真期间收集代码覆盖率度量。
您可以使用代码覆盖率工具和代码覆盖率报告来查看 SIL 仿真的记录代码覆盖率。
在此示例中,您在普通模式下的仿真期间测量模型覆盖率,在 SIL 模式下重复相同的仿真,并比较两次仿真记录的度量。
使用模型覆盖率和代码覆盖率报告中的超链接比较模型模型覆盖率和代码覆盖率结果。
有关测量 SIL 和 PIL 仿真的更多示例,请参阅用 SIL 和 PIL 仿真测试生成的代码 (Embedded Coder)。
初始设置
打开模型。
model = 'SILTopModel';
close_system(model,0);
open_system(model);
删除所有现有的构建文件夹。
buildFolder = RTW.getBuildDir(model); if isfolder(buildFolder.BuildDirectory) rmdir(buildFolder.BuildDirectory,'s'); end
配置覆盖率收集模型。
set_param(model, 'CovEnable', 'on') clear covCumulativeData
设置输入数据。
T = 0.1; % sample time [ticks_to_count, reset, counter_mode, count_enable, ... counter_mode_values_run1, counter_mode_values_run2, ... count_enable_values_run1, count_enable_values_run2] = ... SILTopModelData(T);
在普通模式下运行第一个仿真
仿真完成后,模型覆盖率报告打开。要从模型中的模块导航到覆盖率报告的相应部分,请使用覆盖率显示窗口。
counter_mode.signals.values = counter_mode_values_run1; count_enable.signals.values = count_enable_values_run1; set_param(model, 'SimulationMode', 'normal');
使用仿真数据检查器查看和比较仿真结果。
Simulink.sdi.view; Simulink.sdi.clear;
运行仿真。
simout_normal_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');
突出模型。
cvmodelview(simout_normal_run1.covdata);
捕获结果。
Simulink.sdi.createRun('Run 1 (normal mode)', 'namevalue',... {'simout_normal_run1'}, {simout_normal_run1});
在普通模式下运行第二次仿真
对于第一次仿真,报告显示该模型实现的 MCDC 覆盖率不足 100%。使用不同的输入信号运行第二次仿真,以将 MCDC 覆盖率提高到 100%。模型覆盖率报告配置为显示两次仿真运行的累计覆盖率。
counter_mode.signals.values = counter_mode_values_run2; count_enable.signals.values = count_enable_values_run2; set_param(model, 'SimulationMode', 'normal'); simout_normal_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on'); cvmodelview(simout_normal_run2.covdata); Simulink.sdi.createRun('Run 2 (normal mode)', 'namevalue',... {'simout_normal_run2'}, {simout_normal_run2});
配置模型来测量代码覆盖率
在运行 SIL 仿真之前,配置模型以收集代码覆盖率度量。
coverageSettings = get_param(model, 'CodeCoverageSettings'); coverageSettings.CoverageTool = 'Simulink Coverage'; coverageSettings.TopModelCoverage = 'on'; set_param(model, 'CodeCoverageSettings', coverageSettings);
在 SIL 模式下运行第一个仿真
您可以在 SIL 仿真中使用与在普通模式下第一次运行仿真时使用的相同输入信号。
在 SIL 模式下运行第一次仿真。
counter_mode.signals.values = counter_mode_values_run1; count_enable.signals.values = count_enable_values_run1; set_param(model, 'SimulationMode', 'software-in-the-loop'); set_param(model, 'CodeExecutionProfiling', 'off'); set_param(model, 'CodeProfilingInstrumentation', 'off'); simout_sil_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');
### Starting build procedure for: SILTopModel ### Successful completion of build procedure for: SILTopModel Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================================= SILTopModel Code generated and compiled. Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 16.746s ### Preparing to start SIL simulation ... Building with 'gcc'. MEX completed successfully. ### Updating code generation report with SIL files ... ### Starting SIL simulation for component: SILTopModel ### Application stopped ### Stopping SIL simulation for component: SILTopModel ### Completed code coverage analysis
cvmodelview(simout_sil_run1.covdata); Simulink.sdi.createRun('Run 1 (SIL mode)', 'namevalue',... {'simout_sil_run1'}, {simout_sil_run1});
当仿真完成后,使用覆盖率突出显示查看模型上的代码覆盖率结果。要查看模型元素的 SIL 代码覆盖率摘要,请将光标放在模型元素上。
您还可以在 HTML 代码覆盖率报告中查看代码覆盖率结果。摘要部分显示所有函数都已被调用,但是 SIL 仿真运行未能实现决策、条件或 MCDC 覆盖率的全覆盖。
cvhtml('codecovreport',simout_sil_run1.covdata);
要导航到模块中相应的模型元素,请使用代码覆盖率报告中的超链接。
在 SIL 模式下运行第二次仿真
在 SIL 仿真中使用与在普通模式下第二次仿真运行中使用的相同输入信号。
counter_mode.signals.values = counter_mode_values_run2; count_enable.signals.values = count_enable_values_run2; set_param(model, 'SimulationMode', 'software-in-the-loop'); set_param(model, 'CodeExecutionProfiling', 'off'); set_param(model, 'CodeProfilingInstrumentation', 'off'); simout_sil_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on');
### Starting build procedure for: SILTopModel ### Generated code for 'SILTopModel' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for: SILTopModel Build Summary Top model targets built: Model Action Rebuild Reason ====================================================================== SILTopModel Code compiled. Compilation artifacts were out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 3.3108s ### Preparing to start SIL simulation ... ### Starting SIL simulation for component: SILTopModel ### Application stopped ### Stopping SIL simulation for component: SILTopModel ### Completed code coverage analysis
Simulink.sdi.createRun('Run 2 (SIL mode)', 'namevalue',... {'simout_sil_run2'}, {simout_sil_run2});
使用两种 SIL 模式仿真的累计覆盖率突出显示模型,以查看模型生成的代码是否实现了全覆盖。
cvmodelview(simout_sil_run1.covdata + simout_sil_run2.covdata);
比较普通和 SIL 仿真的度量
每次运行后,仿真数据检查器都会自动打开,以便您查看和分析结果。要确认 SIL 和普通模式运行的记录信号相同,请查看比较和检查窗格中的信息。