bench/string: fix typo in shell script
[nit.git] / benchmarks / strings / bench_strings.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 source ../bench_common.sh
17 source ../bench_plot.sh
18
19 # Default number of times a command must be run with bench_command
20 # Can be overrided with 'the option -n'
21 count=5
22
23 benches="iteration
24 concat
25 substring
26 index
27 compiler
28 basic"
29
30 function usage()
31 {
32 echo "run_bench: [options]* bench_name args"
33 echo ""
34 echo "Options:"
35 echo " -v: verbose mode"
36 echo " -n count: number of execution for each bar (default: $count)"
37 echo " -h: this help"
38 echo " --head-only: do not try to pass benchmarks on variants"
39 echo " --maxlen min_maxln inc_maxln max_maxln: start bench with different values of threshold between ropes and flats"
40 echo ""
41 echo "Benches : "
42 echo " index: indexed access benchmark"
43 echo " - usage : index loops strlen_min strlen_inc strlen_max"
44 echo " concat: string concatenation benchmark"
45 echo " - usage : concat loops strlen min_cct cct_inc max_cct"
46 echo " iteration: iteration benchmark"
47 echo " - usage : iteration loops strlen_min strlen_inc strlen_max"
48 echo " substring: substring benchmark"
49 echo " - usage : substring loops strlen_min strlen_inc strlen_max"
50 echo " compiler: compiler benchmark"
51 echo " - usage : compiler"
52 echo " basic : basic functionnality test of the variants on Hello World"
53 echo " - usage : basic"
54 }
55
56 function bench_index()
57 {
58 if [ $# -lt 4 ]; then
59 echo "Wrong arguments for benchmark index."
60 usage
61 exit
62 fi
63 echo "Generating executable index_bench for variant $variant";
64
65 ../../bin/nitc --global index_bench.nit -D maxlen=$curr_maxln
66
67 bench_indexed_variant "string" $1 $2 $3 $4
68 bench_indexed_variant "buffer" $1 $2 $3 $4
69
70 rm index_bench
71 }
72
73 # $1: string or buffer
74 # $2: loops
75 # $3: strlen min
76 # $4: strlen inc
77 # $5: strlen max
78 function bench_indexed_variant()
79 {
80 tmp="${variant}_${1}_${curr_maxln}.out"
81 prepare_res_lines index_$tmp $tmp $tmp
82 for i in `seq "$3" "$4" "$5"`; do
83 bench_command $i index_$tmp$i ./index_bench -m $1 --loops $2 --strlen $i
84 done
85 }
86
87 function bench_concat()
88 {
89 if [ $# -lt 5 ]; then
90 echo "Wrong arguments for benchmark concat."
91 usage
92 exit
93 fi
94
95 echo "Generating executable chain_concat for variant $variant"
96 ../../bin/nitc chain_concat.nit -D maxlen=$curr_maxln
97
98 bench_concat_variant "string" $1 $2 $3 $4 $5
99 bench_concat_variant "buffer" $1 $2 $3 $4 $5
100
101 rm chain_concat
102 }
103
104 # $1: string or buffer
105 # $2: loops
106 # $3: strlen
107 # $4: concatenations min
108 # $5: concatenations inc
109 # $6: concatenations max
110 function bench_concat_variant()
111 {
112 tmp="${variant}_$1"
113 prepare_res_lines out/concat/concat_$tmp.out $tmp $tmp
114 for i in `seq "$4" "$5" "$6"`; do
115 bench_command $i $tmp$i ./chain_concat -m $1 --loops $2 --strlen $3 --ccts $i
116 done
117 }
118
119 function bench_iteration()
120 {
121 if [ $# -lt 4 ]; then
122 echo "Wrong arguments for benchmark iteration."
123 usage
124 exit
125 fi
126 echo "Generating executable iteration_bench for variant $variant"
127 ../../bin/nitc --global iteration_bench.nit -D maxlen=$curr_maxln
128
129 bench_iterate_variant "iterator" "string" $1 $2 $3 $4
130 bench_iterate_variant "index" "string" $1 $2 $3 $4
131 bench_iterate_variant "iterator" "buffer" $1 $2 $3 $4
132 bench_iterate_variant "index" "buffer" $1 $2 $3 $4
133
134 rm iteration_bench
135 }
136
137 # $1: iterator or index
138 # $2: string or buffer
139 # $3: loops
140 # $4: strlen min
141 # $5: strlen increment
142 # $6: strlen max
143 function bench_iterate_variant()
144 {
145 tmp="${variant}_$1_$2"
146 prepare_res_lines out/iteration/iteration_$tmp.out $tmp $tmp
147 for i in `seq "$4" "$5" "$6"`; do
148 bench_command $i $tmp$i ./iteration_bench -m $2 --iter-mode $1 --loops $3 --strlen $i
149 done
150 }
151
152 function bench_substring()
153 {
154 if [ $# -lt 4 ]; then
155 echo "Wrong arguments for benchmark substring."
156 usage
157 exit
158 fi
159 echo "Generating executable substr_bench for variant $variant"
160 ../../bin/nitc --global substr_bench.nit -D maxlen=$curr_maxln
161
162 bench_substring_variant "string" $1 $2 $3 $4
163 bench_substring_variant "buffer" $1 $2 $3 $4
164
165 rm substr_bench
166 }
167
168 # $1: string or buffer
169 # $2: loops
170 # $3: strlen min
171 # $4: strlen increment
172 # $5: strlen max
173 function bench_substring_variant()
174 {
175 tmp="${variant}_$1"
176 prepare_res_lines out/substring/substring_$tmp.out $tmp $tmp
177 for i in `seq "$3" "$4" "$5"`; do
178 bench_command $i $tmp$i ./substr_bench -m $1 --loops $2 --strlen $i
179 done
180 }
181
182 function bench_compiler()
183 {
184 prepare_res_lines out/compiler/compiler_$variant.out compiler_$variant compiler_$variant
185
186 echo "Pre-compiling nitc"
187 # Do it twice before bench to have stable times when generating C
188 ../../bin/nitc ../../src/nitc.nit -o ../../bin/nitc
189 echo "nitc (1/2)"
190 ../../bin/nitc ../../src/nitc.nit -o ../../bin/nitc
191 echo "nitc (2/2)"
192
193 bench_command nitc nitc_$variant ../../bin/nitc ../../src/nitc.nit -D maxlen=$curr_maxln
194
195 rm nitc
196 }
197
198 function bench_basic()
199 {
200 echo ../../bin/nitc ../../examples/hello_world.nit -D maxlen=$curr_maxln
201 ../../bin/nitc ../../examples/hello_world.nit -D maxlen=$curr_maxln
202 ./hello_world
203 rm hello_world
204 }
205
206 function launch_bench()
207 {
208 echo "---------------------------------------------------------"
209 echo " Trying variant $variant for benchmark $bench"
210 echo "---------------------------------------------------------"
211 git diff-index --quiet HEAD || {
212 die "Cannot run benches on a dirty working directory."
213 die "Please commit or stash your modifications and relaunch the command."
214 return
215 }
216 git am $curr_rev || {
217 die "Error when applying patch $curr_rev"
218 git am --abort
219 return
220 }
221 if [ "$need_bootstrap" = true ]; then
222 prepare_compiler
223 fi
224 bench_$bench "$@";
225 git reset --hard $head
226 }
227
228 function prepare_compiler()
229 {
230 cd ../../c_src
231 rm nitc
232 make clean
233 cd ../src
234 ./ncall.sh
235 mv nitc.good ../bin/nitc
236 cd ../benchmarks/strings
237 }
238
239 function main()
240 {
241 html="index.html"
242 head_only=false
243 bench_maxln=false
244 curr_maxln=64
245 echo >"$html" "<html><head></head><body>"
246 stop=false
247 while [ "$stop" = false ]; do
248 case "$1" in
249 --maxlen) bench_maxln=true; min_maxln=$2; inc_maxln=$3; max_maxln=$4; shift; shift; shift; shift;;
250 -v) verbose=true; shift;;
251 -h) usage; exit;;
252 -n) count="$2"; shift; shift;;
253 --head-only) head_only=true; shift;;
254 *) stop=true
255 esac
256 done
257
258 if [ $# -lt 1 ]; then
259 usage;
260 exit;
261 fi
262
263 isok=false
264 for i in $benches; do
265 if [ $1 = $i ]; then
266 isok=true;
267 fi
268 done
269 if [ "$isok" = false ]; then
270 usage;
271 exit;
272 fi
273
274 bench=$1
275 shift;
276
277 if [ "${bench_maxln}" = true ]; then
278 for i in `seq $min_maxln $inc_maxln $max_maxln`; do curr_maxln=$i; launch_benches "$@"; done;
279 else
280 launch_benches "$@";
281 fi
282
283 if [ "${need_plot}" = true ]; then
284 plot_lines out/$bench/$bench.gnu
285 fi
286
287 echo >> "$html" "</body></html>"
288 }
289
290 function launch_benches()
291 {
292 head=`git rev-parse HEAD`
293 variant="HEAD"
294 need_plot=true
295 need_bootstrap=false
296
297 if [ "$bench" = "basic" ]; then
298 need_plot=false
299 fi
300
301 if [ ! -d out ]; then
302 mkdir out
303 fi
304 cd out
305
306 if [ -d $bench ]; then
307 rm $bench/*
308 else
309 mkdir $bench
310 fi
311 cd ..
312
313 echo "---------------------------------------------------------"
314 echo " Trying variant HEAD for benchmark $bench"
315 echo "---------------------------------------------------------"
316 bench_$bench "$@";
317
318 if [ ! $head_only ]; then
319 for i in lib_variants/regular/*; do
320 curr_rev=$i
321 variant=`basename "$i" | cut -f 1 -d '.'`
322 launch_bench "$@"
323 done
324
325 need_bootstrap=true
326 for i in lib_variants/need_bootstrap/*; do
327 curr_rev=$i
328 variant=`basename "$i" | cut -f 1 -d '.'`
329 launch_bench "$@"
330 done
331 fi
332 }
333
334 main "$@";
335
336 if test -n "$died"; then
337 echo "Some commands failed"
338 exit 1
339 fi
340 exit 0