readme: add information section
[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_maxlen
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_maxlen
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_maxlen
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_maxlen
194
195 rm nitc
196 }
197
198 function bench_basic()
199 {
200 ../../bin/nitc ../../examples/hello_world.nit -D maxlen=$curr_maxlen
201 ./hello_world
202 rm hello_world
203 }
204
205 function launch_bench()
206 {
207 echo "---------------------------------------------------------"
208 echo " Trying variant $variant for benchmark $bench"
209 echo "---------------------------------------------------------"
210 git diff-index --quiet HEAD || {
211 die "Cannot run benches on a dirty working directory."
212 die "Please commit or stash your modifications and relaunch the command."
213 return
214 }
215 git am $curr_rev || {
216 die "Error when applying patch $curr_rev"
217 git am --abort
218 return
219 }
220 if [ "$need_bootstrap" = true ]; then
221 prepare_compiler
222 fi
223 bench_$bench "$@";
224 git reset --hard $head
225 }
226
227 function prepare_compiler()
228 {
229 cd ../../c_src
230 rm nitc
231 make clean
232 cd ../src
233 ./ncall.sh
234 mv nitc.good ../bin/nitc
235 cd ../benchmarks/strings
236 }
237
238 function main()
239 {
240 html="index.html"
241 head_only=false
242 bench_maxln=false
243 curr_maxln=64
244 echo >"$html" "<html><head></head><body>"
245 stop=false
246 while [ "$stop" = false ]; do
247 case "$1" in
248 --maxlen) bench_maxln=true; min_maxln=$2; inc_maxln=$3; max_maxln=$4; shift; shift; shift; shift;;
249 -v) verbose=true; shift;;
250 -h) usage; exit;;
251 -n) count="$2"; shift; shift;;
252 --head-only) head_only=true; shift;;
253 *) stop=true
254 esac
255 done
256
257 if [ $# -lt 1 ]; then
258 usage;
259 exit;
260 fi
261
262 isok=false
263 for i in $benches; do
264 if [ $1 = $i ]; then
265 isok=true;
266 fi
267 done
268 if [ "$isok" = false ]; then
269 usage;
270 exit;
271 fi
272
273 bench=$1
274 shift;
275
276 if [ "${bench_maxln}" = true ]; then
277 for i in `seq $min_maxln $inc_maxln $max_maxln`; do curr_maxln=$i; launch_benches "$@"; done;
278 else
279 launch_benches "$@";
280 fi
281
282 if [ "${need_plot}" = true ]; then
283 plot_lines out/$bench/$bench.gnu
284 fi
285
286 echo >> "$html" "</body></html>"
287 }
288
289 function launch_benches()
290 {
291 head=`git rev-parse HEAD`
292 variant="HEAD"
293 need_plot=true
294 need_bootstrap=false
295
296 if [ "$bench" = "basic" ]; then
297 need_plot=false
298 fi
299
300 if [ ! -d out ]; then
301 mkdir out
302 fi
303 cd out
304
305 if [ -d $bench ]; then
306 rm $bench/*
307 else
308 mkdir $bench
309 fi
310 cd ..
311
312 echo "---------------------------------------------------------"
313 echo " Trying variant HEAD for benchmark $bench"
314 echo "---------------------------------------------------------"
315 bench_$bench "$@";
316
317 if [ ! $head_only ]; then
318 for i in lib_variants/regular/*; do
319 curr_rev=$i
320 variant=`basename "$i" | cut -f 1 -d '.'`
321 launch_bench "$@"
322 done
323
324 need_bootstrap=true
325 for i in lib_variants/need_bootstrap/*; do
326 curr_rev=$i
327 variant=`basename "$i" | cut -f 1 -d '.'`
328 launch_bench "$@"
329 done
330 fi
331 }
332
333 main "$@";
334
335 if test -n "$died"; then
336 echo "Some commands failed"
337 exit 1
338 fi
339 exit 0