文件交换Pick of the Week

我们最好的用户提交

Timing Experiments

Sean‘s pick this week is to revisitRichard’s pick from last week并专注于时间的实验。

今天,我将使用性能测试框架。上周,理查德(Richard)进行了一些不同的实验,他想运行和时间。具体来说,他还想看看与第n跑相比的第一次运行情况。

The performance testing framework makes it very easy to do this. It leverages the same infrastructure as the unit test framework, but allows you to time parts of a test and run the tests either a fixed number of times or until the time of the Nth run converges.

Here I’ll take the algorithmic pieces of Richard’s code and put them into their own tests. There are two test classes which inherit frommatlab.perftest.TestCase。这些类都在 +测试包中,这使我可以自动创建所有测试的套件。

The framework makes it easy toparameterize testsso I can exercise both models under both run modes just by passing in the parameters.

Here are the two classes:

classdefWithWithoutPersistence < matlab.perftest.TestCase% Test with and without persistent data under normal and acceleratorproperties(classSetupparameter)modelName = {'withoutPersistentLoading',,,,'withPersistentLoading'};% Both modelsRunMode = {'普通的',,,,'Accelerator'};% Both modes结尾methods(TestClassSetup)在测试开始之前运行%函数开始模拟(万博1manbetx〜)%启动simul万博1manbetxink以确保不包括初始时间。start_万博1manbetxsimulink结尾函数loadModel(testCase, ModelName, RunMode)% Take RunMode to force it to close and reload between modesload_system(ModelName); testCase.addTeardown(@()close_system(ModelName));结尾结尾%测试方法methods(Test)函数testSimulation(~, ModelName, RunMode)% Simulatesim(ModelName,'SimulationMode',runmode);结尾结尾结尾

和:

classdeffastrestart % Test fast restartmethods(TestClassSetup)函数开始模拟(万博1manbetx〜)%启动simul万博1manbetxink以确保不包括初始时间。start_万博1manbetxsimulinkload_system('withoutPersistentLoading');结尾结尾methods(TestClassTeardown)函数RestoreModel(〜)%恢复状态set_param('withoutPersistentLoading',,,,'Fastrestart',,,,'off');close_system('withoutPersistentLoading');结尾结尾methods(Test)函数testFastRestart(~)% Simulatesim('withoutPersistentLoading',,,,'Fastrestart',,,,'上');结尾结尾结尾

From there, we’ll create the test suite and create a fixed time experiment to run one warmup and 10 samples.

importmatlab.unittest.TestSuiteimportmatlab.perftest.FixedTimeExperiment套件= testsuite.frompackage('Test',,,,“包含苹果包”,,,,true); fte = FixedTimeExperiment.withFixedSampleSize(10,'numwarmups',,,,1);results = run(fte, suite); testactivity = vertcat(results.TestActivity); disp(testactivity)
运行test.fastrestart ..........完成测试。法式__________运行测试。。。。Done Test.WithWithoutPersistence __________ Name Passed Failed Incomplete MeasuredTime Objective Timestamp Host Platform Version TestResult RunIdentifier __________________________________________________________________________________________________ ______ ______ __________ ____________ _________ ____________________ ___________ ________ _____________________ ________________________________ ____________________________________ Test.FastRestart/testFastRestart true false false 3.6662 warmup 10-May-2018 19:50:01 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.022907 sample 10-May-2018 19:50:01 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.023864 sample 10-May-2018 19:50:01 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.034052 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.023421 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.035024 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.024277 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.021401 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.021795 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.021364 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.FastRestart/testFastRestart true false false 0.024193 sample 10-May-2018 19:50:02 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 4.271 warmup 10-May-2018 19:50:06 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.9064 sample 10-May-2018 19:50:10 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.8215 sample 10-May-2018 19:50:14 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.9881 sample 10-May-2018 19:50:18 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.983 sample 10-May-2018 19:50:22 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.7034 sample 10-May-2018 19:50:26 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.8451 sample 10-May-2018 19:50:30 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.7176 sample 10-May-2018 19:50:33 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.7336 sample 10-May-2018 19:50:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.8346 sample 10-May-2018 19:50:41 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation true false false 3.7633 sample 10-May-2018 19:50:45 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.9732 warmup 10-May-2018 19:50:49 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.8535 sample 10-May-2018 19:50:53 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.8244 sample 10-May-2018 19:50:57 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7287 sample 10-May-2018 19:51:00 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.8755 sample 10-May-2018 19:51:04 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7872 sample 10-May-2018 19:51:08 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7933 sample 10-May-2018 19:51:12 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.8545 sample 10-May-2018 19:51:16 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.9182 sample 10-May-2018 19:51:20 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7648 sample 10-May-2018 19:51:24 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7802 sample 10-May-2018 19:51:27 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 3.6732 warmup 10-May-2018 19:51:31 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.18088 sample 10-May-2018 19:51:31 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.07304 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.065155 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.064901 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.06622 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.064989 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.064424 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.064974 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.063272 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Normal]/testSimulation true false false 0.067388 sample 10-May-2018 19:51:32 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 3.7391 warmup 10-May-2018 19:51:36 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.25133 sample 10-May-2018 19:51:36 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.11322 sample 10-May-2018 19:51:36 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.11479 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.14222 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.12787 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.12439 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.12707 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.1336 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.11642 sample 10-May-2018 19:51:37 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196 Test.WithWithoutPersistence[ModelName=withPersistentLoading,RunMode=Accelerator]/testSimulation true false false 0.12538 sample 10-May-2018 19:51:38 ah-sdewolsk win64 9.4.0.813654 (R2018a) [1x1 matlab.unittest.TestResult] cc5b36fc-7c9f-4f09-9054-c60025f31196

Now we can either usevarfunto analyze the groups or push this to Excel to be stored or pivoted out.

mt = varfun(@median, testactivity,。。。“分组”,{'姓名',,,,'客观的'},。。。“ inputvariobles”,{“测量时间”})
mt = 10×4 table Name Objective GroupCount median_MeasuredTime __________________________________________________________________________________________________ _________ __________ ___________________ Test.FastRestart/testFastRestart sample 10 0.023642 Test.FastRestart/testFastRestart warmup 1 3.6662 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation sample 10 3.828 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Normal]/testSimulation warmup 1 4.271 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation sample 10 3.8088 Test.WithWithoutPersistence[ModelName=withoutPersistentLoading,RunMode=Accelerator]/testSimulation warmup 1 3.9732 Test.forwithOutperSistence [modelName = withPersisTentLoading,runmode = normal]/testssimulation样本10 0.065072 test.与with twithOutPersistence [modelName = withpersistentLoading,runmode = normal = normal]/testssimulation Humport 1 3.6732 test。delname = withperSistentLoading,runmode = ACCELERATOR]/testSimulation样本10 0.12623 test.withOutPersistence [modelName = withPersisTentLoading,runmode = Accelerator = Accelerator]/testssimulation Hallup 1 3.7391

Or:

撰写(测试效率,'TestActivity.xlsx'

Comments

Do you have a use for repetitively timing MATLAB code or Simulink models?

Give it a try and let us know what you thinkhere

与Matlab®R2018A一起出版

|
  • 打印
  • 发送电子邮件

Comments

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.