Debian Codegraph @

tests/run_mod_file.m

raw
1 % Copyright © 2011-2026 Dynare Team
2 %
3 % This file is part of Dynare.
4 %
5 % Dynare is free software: you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation, either version 3 of the License, or
8 % (at your option) any later version.
9 %
10 % Dynare is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License
16 % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
17
18 source_root = getenv('source_root');
19 addpath([source_root filesep 'tests' filesep 'utils']);
20 addpath([source_root filesep 'matlab']);
21 profile_suite_env = getenv('DYNARE_PROFILE_SUITE');
22 profile_suite = strcmpi(profile_suite_env, 'true') || strcmpi(profile_suite_env, '1') || strcmpi(profile_suite_env, 'yes');
23
24 if isoctave
25 load_octave_packages
26 end
27
28 fprintf('\n*** TESTING: %s ***\n\n', getenv('mod_file'));
29
30 tic;
31
32 % NB: all variables will be cleared by the call to Dynare
33 try
34 if ~isoctave && profile_suite
35 profile clear;
36 profile on;
37 end
38 % Read arguments from individual environment variables
39 dynare_arg_count = str2double(getenv('dynare_arg_count'));
40 if isnan(dynare_arg_count) || dynare_arg_count == 0
41 dynare(getenv('mod_file'), 'console')
42 else
43 args_cell = cell(1, dynare_arg_count);
44 for i = 1:dynare_arg_count
45 args_cell{i} = getenv(sprintf('dynare_arg_%d', i-1));
46 end
47 dynare(getenv('mod_file'), 'console', args_cell{:})
48 end
49 testFailed = false;
50 catch exception
51 printTestError(getenv('mod_file'), exception);
52 testFailed = true;
53 end
54
55 if ~isoctave && profile_suite
56 profile off;
57 pinfo = profile('info');
58 fprintf('\n*** Profiling summary (top 20 by total time) ***\n');
59 if ~isempty(pinfo.FunctionTable)
60 [~, idx] = sort([pinfo.FunctionTable.TotalTime], 'descend');
61 topN = min(20, numel(idx));
62 for k = 1:topN
63 ft = pinfo.FunctionTable(idx(k));
64 child_time = 0;
65 if isfield(ft, 'Children') && ~isempty(ft.Children)
66 child_time = sum([ft.Children.TotalTime]);
67 end
68 self_time = ft.TotalTime - child_time;
69 if isfield(ft, 'NumCalls')
70 calls = ft.NumCalls;
71 elseif isfield(ft, 'TotalCalls')
72 calls = ft.TotalCalls;
73 else
74 calls = NaN;
75 end
76 fprintf('%2d) %-60s total %8.3f s | self %8.3f s (%d calls)\n', k, ft.FunctionName, ft.TotalTime, self_time, calls);
77 end
78 else
79 fprintf('(no profile data)\n');
80 end
81 end
82
83 fprintf('\n*** Elapsed time (in seconds): %.1f\n\n', toc);
84
85 % Ensure proper termination and exit code
86 quit(testFailed)