File: bench_run.sci

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (363 lines) | stat: -rw-r--r-- 10,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2007-2008 - INRIA - Pierre MARECHAL <pierre.marechal@inria.fr>
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt

//-----------------------------------------------------------------------------
// Launch benchmarks
//-----------------------------------------------------------------------------

function bench_run(varargin)
	
	lhs = argn(1);
	rhs = argn(2);
	
	global test_list;
	global test_count;
	global boucle_for_time;
	
	test_list          = [];
	test_count         = 0;
	boucle_for_time    = 0;
	
	just_list_tests    = %F;
	print_help         = %F;
	nb_run             = "10000";
	
	xml_str            = "";
	
	// =======================================================
	// Gestion des tests à lancer
	// =======================================================
	
	if (rhs == 0) ..
		| ((rhs == 1) & (varargin(1)==[])) ..
		| (((rhs == 2)|(rhs == 3)) & (varargin(1)==[]) & (varargin(2)==[])) then
		
		// No input argument
		// bench_run()
		// bench_run([])
		// => Launch each test of each module
		
		module_list = getmodules();
		module_list = gsort(module_list,"lr","i");
		for k=1:size(module_list,'*')
			bench_add_module(module_list(k));
		end
	
	elseif (rhs == 1) ..
		| ((rhs == 2) & (varargin(2)==[])) ..
		| ((rhs == 3) & (varargin(2)==[])) then
		
		// One input argument
		// bench_run(<module_name>)
		// bench_run([<module_name_1>,<module_name_2>])
		
		// varargin(1) = [<module_name_1>,<module_name_2>]
		
		module_mat = varargin(1);
		
		[nl,nc] = size(module_mat);
		
		// bench_run([<module_name_1>,<module_name_2>])
		
		for i=1:nl
			for j=1:nc
				if( with_module(module_mat(i,j)) ) then
					bench_add_module(module_mat(i,j));
				else
					error(sprintf(gettext("%s is not an installed module"),module_mat(i,j)));
				end
			end
		end
		
	elseif (rhs == 2) | (rhs == 3) then
		
		// Two input arguments
		// bench_run(<module_name>,<test_name>)
		// bench_run(<module_name>,[<test_name_1>,<test_name_2>] )
		
		// varargin(1) = <module_name> ==> string 1x1
		// varargin(2) = <test_name_1> ==> mat nl x nc
		
		module     = varargin(1);
		test_mat   = varargin(2);
		
		if ((or(size(module) <> [1,1])) & (test_mat <> [])) then
			example = bench_examples();
			err     = ["" ; gettext("error : Input argument sizes are not valid") ; "" ; example ];
			printf("%s\n",err);
			return;
		end
		
		[nl,nc] = size(test_mat);
		
		for i=1:nl
			for j=1:nc
				
				if (fileinfo(SCI+"/modules/"+module+"/tests/benchmarks/"+test_mat(i,j)+".tst")<>[]) then
					bench_add_onebench(module,test_mat(i,j));
				else
					error(sprintf(gettext("The test ""%s"" is not available from the ""%s"" module"),test_mat(i,j),module));
				end
				
			end
		end
	else
		error(gettext('Number of parameters incorrect.'));
	end
	
	// =======================================================
	// Gestion des options
	// =======================================================
	
	if rhs == 3 then
		
		option_mat =  varargin(3);
		
		if grep(option_mat,"list") <> [] then
			just_list_tests    = %T;
		end
		
		if grep(option_mat,"help") <> [] then
			print_help         = %T;
		end
		
		if grep(option_mat,"nb_run=") <> [] then
			nb_run_line = grep(option_mat,"nb_run=");
			nb_run      = strsubst(option_mat(nb_run_line),"nb_run=","");
		end
		
	end
	
	if print_help then
		
		// =======================================================
		// Display help
		// =======================================================
		
		example = bench_examples();
		printf("%s\n",example);
		return;
	
	elseif just_list_tests then
		
		// =======================================================
		// Just list tests
		// =======================================================
		
		printf("\n");
		
		for i=1:test_count
			printf("   %03d - ",i);
			printf("[%s] %s\n",test_list(i,1),test_list(i,2));
		end
		
	else
		
		// =======================================================
		// Test launch
		// =======================================================
		
		printf("\n");
		
		xml_str = [ xml_str ; "<benchmarks>" ];
		
		// Calcul de la durée de la boucle for en µs
		
		timer();
		for i = 1:1000000
		end
		timing = timer();
		boucle_for_time = timing;
		
		printf("             Boucle For .......................................        %1.2f %ss [ 1000000 x]\n\n",boucle_for_time,ascii(181));
		
		for i=1:test_count
			
			// Display
			printf("   %03d/%03d - ",i,test_count);
			printf("[%s] %s ",test_list(i,1),test_list(i,2));
			for j = length(test_list(i,2) + test_list(i,1)):45
				printf(".");
			end
			printf(" ");
			
			// Bench process
			[returned_time,nb_run_done] = bench_run_onebench(test_list(i,1),test_list(i,2),nb_run);
			
			// Display
			returned_time_str           = sprintf("%4.2f %ss",returned_time,ascii(181));
			for j = length(returned_time_str):13
				printf(' ');
			end
			printf("%s [",returned_time_str);
			for j = length(nb_run_done):7
				printf(' ');
			end
			printf("%s x]\n",nb_run_done);
			
			// XML print
			xml_str = [ xml_str ; ..
				"    <bench>"; ..
				"        <module>"+test_list(i,1)+"</module>"; ..
				"        <id>"+test_list(i,2)+"</id>"; ..
				"        <duration>"+strsubst(returned_time_str," "+ascii(181)+"s","")+"</duration>"; ..
				"        <nb_run>"+nb_run_done+"</nb_run>"; ..
				"    </bench>" ];
			
		end
		
	end
	
	// XML management
	xml_str = [ xml_str ; "</benchmarks>" ];
	xml_file_name = SCI+"/bench_"+getversion()+"_"+date()+".xml";
	mputl(xml_str,xml_file_name);
	
	clearglobal test_list;
	clearglobal test_count;
	clearglobal boucle_for_time;
	
endfunction

//-----------------------------------------------------------------------------
// Pierre MARECHAL
// Scilab team
// Date : 28 oct. 2007
//
// => List all test files in the module <module_mat>
// => Add them to the test_mat matrix
//-----------------------------------------------------------------------------

function bench_add_module(module_mat,test_type)
	
	module_test_dir = SCI+"/modules/"+module_mat+"/tests/benchmarks";
	test_mat        = gsort(basename(listfiles(module_test_dir+"/*.tst")),"lr","i");
	
	nl = size(test_mat,"*");
	for i=1:nl
		bench_add_onebench(module_mat,test_mat(i));
	end
	
endfunction

//-----------------------------------------------------------------------------
// Pierre MARECHAL
// Scilab team
// Date : 28 oct. 2007
//
// => Add the test <test> to the test_mat matrix
//-----------------------------------------------------------------------------

function bench_add_onebench(module,test)
	
	global test_list;
	global test_count;
	
	test_count = test_count + 1;
	
	test_list( test_count , 1 ) = module;
	test_list( test_count , 2 ) = test;
	
endfunction

//-----------------------------------------------------------------------------
// Pierre MARECHAL
// Scilab team
// Date : 28 oct. 2007
//
// => Run one test
//-----------------------------------------------------------------------------

function [returned_time,nb_run_done] = bench_run_onebench(module,test,nb_run)
	
	returned_time = 0;
	
	fullPath      = SCI+"/modules/"+module+"/tests/benchmarks/"+test;
	
	tstfile       = pathconvert(fullPath+".tst",%f,%f);
	scefile       = pathconvert(fullPath+".sce",%f,%f);
	datfile       = pathconvert(fullPath+".dat",%f,%f);
	
	// Get the tst file
	txt = mgetl(tstfile);
	
	// Check if the nb run is defined in the test
	
	check_nb_run_line = grep(txt,"<-- BENCH NB RUN :");
	
	nb_run_done   = nb_run;
	
	if check_nb_run_line <> [] then
		nb_run_line   = txt(check_nb_run_line);
		nb_run_start  = strindex(nb_run_line,"<-- BENCH NB RUN :") + length("<-- BENCH NB RUN :");
		nb_run_end    = strindex(nb_run_line,"-->") - 1;
		nb_run        = stripblanks(part(nb_run_line,[nb_run_start:nb_run_end]));
		nb_run_done   = nb_run;
	end
	
	// get the <-- BENCH START --> and <-- BENCH END --> tags
	line_start = grep(txt,"<-- BENCH START -->");
	line_end   = grep(txt,"<-- BENCH END -->");
	
	// Get the context and the bench
	context    = txt([1:line_start-1]);
	bench      = txt([line_start+1:line_end-1]);
	
	// Remove blank lines
	context(find(context == '' )) = [];
	bench  (find(bench   == '' )) = [];
	
	tst_str = [ context ;
				"nb_run = "+nb_run+";";
				"timer();";
				"for i = 1:nb_run";
				bench;
				"end";
				"timing = timer();";
				"returned_time = timing * 1000000 / nb_run;"]
	
	mputl(tst_str,scefile);
	exec(scefile);
	returned_time = returned_time - boucle_for_time;
	return;
	
endfunction

//-----------------------------------------------------------------------------
// Pierre MARECHAL
// Scilab team
// Date : 28 oct. 2007
//
// => Check ref or generate ref
//-----------------------------------------------------------------------------

function example = bench_examples()
	
	example = [ sprintf("Examples :\n\n") ];
	
	example = [ example ; sprintf("// Launch all tests\n") ];
	example = [ example ; sprintf("bench_run();\n") ];
	example = [ example ; sprintf("bench_run([]);\n") ];
	example = [ example ; sprintf("bench_run([],[]);\n") ];
	example = [ example ; "" ];
	example = [ example ; sprintf("// Test one or several module\n") ];
	example = [ example ; sprintf("bench_run(''core'');\n") ];
	example = [ example ; sprintf("bench_run(''core'',[]);\n") ];
	example = [ example ; sprintf("bench_run([''core'',''string'']);\n") ];
	example = [ example ; "" ];
	example = [ example ; sprintf("// Launch one or several test in a specified module\n") ];
	example = [ example ; sprintf("bench_run(''core'',[''trycatch'',''opcode'']);\n") ];
	example = [ example ; "" ];
	example = [ example ; sprintf("// With options\n") ];
	example = [ example ; sprintf("bench_run([],[],''list'');\n") ];
	example = [ example ; sprintf("bench_run([],[],''help'');\n") ];
	example = [ example ; sprintf("bench_run([],[],''nb_run=2000'');\n") ];
	example = [ example ; "" ];
	
endfunction