1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2007-2008 - INRIA - Pierre MARECHAL <pierre.marechal@inria.fr>
3 // Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
14 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
18 function [modutests_names, elapsed_time, nb_iterations] = bench_run(varargin)
41 // =======================================================
42 // Gestion des tests à lancer
43 // =======================================================
46 | ((rhs == 1) & (varargin(1)==[] | varargin(1)=="[]" | varargin(1) == "")) ..
47 | (((rhs >= 2)) & (varargin(1)==[] | varargin(1)=="[]" | varargin(1) == "") & (varargin(2)==[] | varargin(2)=="[]" | varargin(2) == "")) then
52 // => Launch each test of each module
54 module_list = getmodules();
55 module_list = gsort(module_list,"lr","i");
56 for k=1:size(module_list,"*")
57 bench_add_module(module_list(k));
61 | ((rhs >= 2) & (varargin(2)==[] | varargin(2)=="[]" | varargin(2) == "")) ..
64 // bench_run(<module_name>)
65 // bench_run([<module_name_1>,<module_name_2>])
67 // varargin(1) = [<module_name_1>,<module_name_2>]
69 module_mat = varargin(1);
71 [nl,nc] = size(module_mat);
73 // bench_run([<module_name_1>,<module_name_2>])
77 if( with_module(module_mat(i,j)) ) then
78 bench_add_module(module_mat(i,j));
80 if isdir(module_mat(i,j)) then
81 bench_add_dir(module_mat(i,j));
83 error(msprintf(gettext("%s: %s is not an installed module"), "bench_run", module_mat(i,j)));
89 elseif (rhs >= 2 & rhs <= 4) then
91 // Two input arguments
92 // bench_run(<module_name>,<test_name>)
93 // bench_run(<module_name>,[<test_name_1>,<test_name_2>] )
95 module_mat = varargin(1);
96 test_mat = varargin(2);
97 bench_list_reduced = [];
99 // get module and test lists
100 bench_list = bench_list_tests(module_mat);
101 // only keep relevant tests
102 // after this loop bench_test_reduced contains the module and relevant tests
103 for i = 1:size(test_mat, "*")
104 found_tests = find(bench_list(:,2) == test_mat(i));
105 if ~isempty(found_tests)
106 bench_list_reduced = [bench_list_reduced; bench_list(found_tests, :)];
108 // At least one element in the test list is wrong
110 error(msprintf(_("%s: Wrong value for input argument #%d: test %s not found in the list of modules"), "bench_run", 2, test_mat(i)));
114 for i=1:size(bench_list_reduced, "r") //loops over each row of bench_list_reduced
115 bench_add_onebench(bench_list_reduced(i, 1), bench_list_reduced(i, 2));
118 error(msprintf(gettext("%s: Wrong number of input argument(s): %d to %d expected.\n"), "bench_run", 0, 4));
121 // =======================================================
122 // Gestion des options
123 // =======================================================
127 option_mat = varargin(3);
129 if grep(option_mat,"list") <> [] then
130 just_list_tests = %T;
133 if grep(option_mat,"help") <> [] then
137 nb_run_line = grep(option_mat,"/nb_run\s*=\s*/", "r")
138 if ~isempty(nb_run_line) then
139 nb_run_override = %t;
140 stripped_option = option_mat(nb_run_line);
141 idx_nonblank = strindex(stripped_option, "/[^ \t\b]/", "r");
142 stripped_option = part(stripped_option, idx_nonblank);
143 nb_run = strsubst(stripped_option, "nb_run=","");
150 // =======================================================
152 // =======================================================
154 example = bench_examples();
155 printf("%s\n",example);
158 elseif just_list_tests then
160 // =======================================================
162 // =======================================================
167 printf(" %03d - ",i);
168 printf("[%s] %s\n",test_list(i,1),test_list(i,2));
173 // =======================================================
175 // =======================================================
177 // Protect user modes during tests
178 saved_modes = mode();
180 saved_format = format();
181 saved_warning = warning("query");
182 saved_funcprot = funcprot();
186 xml_str = [ xml_str ; "<benchmarks>" ];
188 // Calcul de la durée de la boucle for en µs
194 boucle_for_time = timing * 1000;
196 printf(" For Loop (as reference) ........................... %4.2f ms [ 1000000 x]\n\n",boucle_for_time);
198 // Creation of return values the size of test_count
203 printf(" %03d/%03d - ",i,test_count);
204 printf("[%s] %s ",test_list(i,1),test_list(i,2));
205 for j = length(test_list(i,2) + test_list(i,1)):45
211 [returned_time, nb_run_done] = bench_run_onebench(test_list(i,1), test_list(i,2), nb_run);
213 // restore user modes inside the loop
214 // Protects from tests that modify those settings
217 format(saved_format([2 1]));
218 warning(saved_warning);
219 funcprot(saved_funcprot);
221 elapsed_time = [elapsed_time; returned_time];
222 nb_iterations = [nb_iterations; nb_run_done];
225 returned_time_str = sprintf("%4.2f ms",returned_time);
226 for j = length(returned_time_str):13
229 printf("%s [",returned_time_str);
230 for j = length(nb_run_done):7
233 printf("%s x]\n",nb_run_done);
236 xml_str = [ xml_str ; ..
238 " <module>"+test_list(i,1)+"</module>"; ..
239 " <id>"+test_list(i,2)+"</id>"; ..
240 " <duration>"+strsubst(returned_time_str," ms","")+"</duration>"; ..
241 " <nb_run>"+nb_run_done+"</nb_run>"; ..
247 modutests_names = test_list;
248 nb_iterations = eval(nb_iterations);
252 // exportToFile can be
253 // * "", "[]" or []: default behaviour, write the output file in the TMPDIR/benchmarks
254 // path/to/directory/: export a timestamped xml file to the output directory
255 // path/to/directory/filename.xml: exports filename.xml to the directory
256 // get the current date to create a timestamp
258 // Close the final tag for export
259 xml_str = [ xml_str ; "</benchmarks>" ];
260 if size(unique(modutests_names(:,1)), "r") == 1
261 module_name = tokens(pathconvert(modutests_names(1, 1), %f, %f, "u"), "/"); // name of the only module tested
262 module_name = module_name($);
268 exportToFile = varargin(4);
269 if (isempty(exportToFile) | exportToFile == "[]")
275 [xml_file_name, ierr, fd_xml] = bench_file_output_path(exportToFile);
278 mputl(xml_str, fd_xml);
282 clearglobal test_list;
283 clearglobal test_count;
284 clearglobal boucle_for_time;
289 //-----------------------------------------------------------------------------
292 // Date : 28 oct. 2007
294 // => List all test files in the module <module_mat>
295 // => Add them to the test_mat matrix
296 //-----------------------------------------------------------------------------
298 function bench_add_module(module_mat,test_type)
300 module_test_dir = SCI+"/modules/"+module_mat+"/tests/benchmarks";
301 test_mat = gsort(basename(listfiles(module_test_dir+"/*.tst")),"lr","i");
303 nl = size(test_mat,"*");
305 bench_add_onebench(module_mat,test_mat(i));
310 function [bench_list] = bench_list_tests(module_mat)
312 module_test_dir = [];
314 for i = 1:size(module_mat, "*")
315 if with_module(module_mat(i))
316 // module_mat(i) is a scilab module
317 module_test_dir = [module_test_dir; SCI+"/modules/"+module_mat(i)+"/tests/benchmarks"];
319 // module_mat(i) is a directory
320 module_test_dir = [module_test_dir; module_mat(i) + "/tests/benchmarks"];
322 test_mat = gsort(basename(listfiles(module_test_dir(i) + "/*.tst")),"lr","i");
323 bench_list = [bench_list; [repmat(module_mat(i), size(test_mat, "*"), 1), test_mat]];
327 //-----------------------------------------------------------------------------
330 // Date : 28 oct. 2007
332 // => Add the test <test> to the test_mat matrix
333 //-----------------------------------------------------------------------------
335 function bench_add_onebench(module,test)
340 test_count = test_count + 1;
342 test_list( test_count , 1 ) = module;
343 test_list( test_count , 2 ) = test;
347 //-----------------------------------------------------------------------------
350 // Date : 28 oct. 2007
353 //-----------------------------------------------------------------------------
355 function [returned_time,nb_run_done] = bench_run_onebench(module, test, nb_run)
356 // runs the benchmark for module
359 if with_module(module)
360 fullPath = SCI+"/modules/"+module+"/tests/benchmarks/"+test;
362 fullPath = module + "/tests/benchmarks/" + test;
365 tstfile = pathconvert(fullPath+".tst",%f,%f);
366 scefile = pathconvert(TMPDIR+"/"+test+".sce",%f,%f);
369 txt = mgetl(tstfile);
371 // Check if the nb run is defined in the test
373 check_nb_run_line = grep(txt,"<-- BENCH NB RUN :");
375 nb_run_done = nb_run;
377 if (check_nb_run_line <> [] & ~nb_run_override) then
378 nb_run_line = txt(check_nb_run_line);
379 nb_run_start = strindex(nb_run_line,"<-- BENCH NB RUN :") + length("<-- BENCH NB RUN :");
380 nb_run_end = strindex(nb_run_line,"-->") - 1;
381 nb_run = stripblanks(part(nb_run_line,[nb_run_start:nb_run_end]));
382 nb_run_done = nb_run;
385 // get the <-- BENCH START --> and <-- BENCH END --> tags
386 line_start = grep(txt,"<-- BENCH START -->");
387 line_end = grep(txt,"<-- BENCH END -->");
389 // Get the context and the bench
390 // Take the whole file as bench if the tags are not found
391 if isempty(line_start) | isempty(line_end)
396 context = txt([1:line_start-1]);
397 bench = txt([line_start+1:line_end-1]);
398 after = txt([line_end:$]);
401 // Remove blank lines
402 context(find(context == "" )) = [];
403 bench (find(bench == "" )) = [];
405 tst_str = [ context ;
406 "nb_run = "+nb_run+";";
408 "for __loop_iterator__ = 1:nb_run";
413 "returned_time = timing * 1000;"]
415 mputl(tst_str,scefile);
417 returned_time = returned_time - (boucle_for_time * nb_run / 1d6)
422 //-----------------------------------------------------------------------------
425 // Date : 28 oct. 2007
427 // => Check ref or generate ref
428 //-----------------------------------------------------------------------------
430 function example = bench_examples()
432 example = [ sprintf("Examples :\n\n") ];
434 example = [ example ; sprintf("// Launch all tests\n") ];
435 example = [ example ; sprintf("// This may take some time...\n") ];
436 example = [ example ; sprintf("// bench_run();\n") ];
437 example = [ example ; sprintf("// bench_run([]);\n") ];
438 example = [ example ; sprintf("// bench_run([],[]);\n") ];
439 example = [ example ; "" ];
440 example = [ example ; sprintf("// Test one or several module\n") ];
441 example = [ example ; sprintf("bench_run(''core'');\n") ];
442 example = [ example ; sprintf("bench_run(''core'',[]);\n") ];
443 example = [ example ; sprintf("bench_run([''core'',''string'']);\n") ];
444 example = [ example ; "" ];
445 example = [ example ; sprintf("// Launch one or several test in a specified module\n") ];
446 example = [ example ; sprintf("bench_run(''core'',[''trycatch'',''opcode'']);\n") ];
447 example = [ example ; "" ];
448 example = [ example ; sprintf("// With options\n") ];
449 example = [ example ; sprintf("bench_run([],[],''list'');\n") ];
450 example = [ example ; sprintf("bench_run([],[],''help'');\n") ];
451 example = [ example ; sprintf("bench_run(""string"",[],''nb_run=100'');\n") ];
452 example = [ example ; sprintf("// results in an output file in the local directory\n") ];
453 example = [ example ; sprintf("bench_run(""string"",[],''nb_run=100'', ""my_output_file.xml"");\n") ];
454 example = [ example ; sprintf("// results in an output directory TMPDIR/benchmarks/ is the default \n") ];
455 example = [ example ; sprintf("bench_run(""string"",[],''nb_run=100'', TMPDIR);\n") ];
456 example = [ example ; "" ];
460 function bench_add_dir(directory)
461 // Scans directory for tests/benchmarks and add the benchmarks
462 module_test_dir = directory + "/tests/benchmarks";
463 test_mat = gsort(basename(listfiles(module_test_dir+"/*.tst")),"lr","i");
465 nl = size(test_mat,"*");
467 bench_add_onebench(directory, test_mat(i));
471 function [xml_file_name, ierr, fd_xml] = bench_file_output_path(exportPath, module_name)
473 // Default for export is TMPDIR/benchmarks/
474 exportPath = TMPDIR + "/benchmarks";
475 if ~isdir(exportPath)
476 createdir(exportPath);
480 // Create timestamp and scilab short version
481 current_date = getdate();
482 current_date = msprintf("%d-%02d-%02d_%02d%02d%02d", current_date(1), current_date(2), current_date(6), current_date(7), current_date(8), current_date(9));
483 sciversion = getversion("scilab");
484 sciversion = string(sciversion);
485 sciversion = sciversion(1) + "." + sciversion(2) + "." + sciversion(3);
487 // Manage a single module name separation
488 if (module_name <> "")
489 module_name_sep = module_name + "_";
491 module_name_sep = "";
495 // The exportPath is a directory
496 // build the inside this directory
497 xml_file_name = exportPath + "/bench_" + module_name_sep + sciversion + "_" + current_date +".xml";
498 ierr = execstr("fd_xml = mopen(xml_file_name,''wt'');","errcatch");
500 // The exportPath is not a directory
501 xml_file_name = exportPath;
502 ierr = execstr("fd_xml = mopen(xml_file_name,''wt'');","errcatch");
505 [xml_file_alt, ierr, fd_xml] = bench_file_output_path("", module_name);
506 msg = msprintf(_("%s: Cannot create file %s, created file %s instead.\n"), "bench_run", fullpath(xml_file_name), strsubst(fullpath(xml_file_alt), TMPDIR, "TMPDIR"));