7a0906592b008394324c5515193c125c5cf1c8f2
[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 ""
39 echo "Benches : "
40 echo " index: indexed access benchmark"
41 echo " - usage : index loops strlen_min strlen_inc strlen_max"
42 echo " concat: string concatenation benchmark"
43 echo " - usage : concat loops strlen min_cct cct_inc max_cct"
44 echo " iteration: iteration benchmark"
45 echo " - usage : iteration loops strlen_min strlen_inc strlen_max"
46 echo " substring: substring benchmark"
47 echo " - usage : substring loops strlen_min strlen_inc strlen_max"
48 echo " compiler: compiler benchmark"
49 echo " - usage : compiler"
50 echo " basic : basic functionnality test of the variants on Hello World"
51 echo " - usage : basic"
52 }
53
54 function bench_index()
55 {
56 if [ $# -lt 4 ]; then
57 echo "Wrong arguments for benchmark index."
58 usage
59 exit
60 fi
61 echo "Generating executable index_bench for variant $variant"
62 ../../bin/nitc --global index_bench.nit
63
64 bench_indexed_variant "string" $1 $2 $3 $4
65 bench_indexed_variant "buffer" $1 $2 $3 $4
66
67 rm index_bench
68 }
69
70 # $1: string or buffer
71 # $2: loops
72 # $3: strlen min
73 # $4: strlen inc
74 # $5: strlen max
75 function bench_indexed_variant()
76 {
77 tmp="${variant}_$1"
78 prepare_res index_$tmp.out $tmp $tmp
79 for i in `seq "$3" "$4" "$5"`; do
80 bench_command $i index_$tmp$i ./index_bench -m $1 --loops $2 --strlen $i
81 done
82 }
83
84 function bench_concat()
85 {
86 if [ $# -lt 5 ]; then
87 echo "Wrong arguments for benchmark concat."
88 usage
89 exit
90 fi
91
92 echo "Generating executable chain_concat for variant $variant"
93 ../../bin/nitc --global chain_concat.nit
94
95 bench_concat_variant "string" $1 $2 $3 $4 $5
96 bench_concat_variant "buffer" $1 $2 $3 $4 $5
97
98 rm chain_concat
99 }
100
101 # $1: string or buffer
102 # $2: loops
103 # $3: strlen
104 # $4: concatenations min
105 # $5: concatenations inc
106 # $6: concatenations max
107 function bench_concat_variant()
108 {
109 tmp="${variant}_$1"
110 prepare_res out/concat/concat_$tmp.out $tmp $tmp
111 for i in `seq "$4" "$5" "$6"`; do
112 bench_command $i $tmp$i ./chain_concat -m $1 --loops $2 --strlen $3 --ccts $i
113 done
114 }
115
116 function bench_iteration()
117 {
118 if [ $# -lt 4 ]; then
119 echo "Wrong arguments for benchmark iteration."
120 usage
121 exit
122 fi
123 echo "Generating executable iteration_bench for variant $variant"
124 ../../bin/nitc --global iteration_bench.nit
125
126 bench_iterate_variant "iterator" "string" $1 $2 $3 $4
127 bench_iterate_variant "index" "string" $1 $2 $3 $4
128 bench_iterate_variant "iterator" "buffer" $1 $2 $3 $4
129 bench_iterate_variant "index" "buffer" $1 $2 $3 $4
130
131 rm iteration_bench
132 }
133
134 # $1: iterator or index
135 # $2: string or buffer
136 # $3: loops
137 # $4: strlen min
138 # $5: strlen increment
139 # $6: strlen max
140 function bench_iterate_variant()
141 {
142 tmp="${variant}_$1_$2"
143 prepare_res out/iteration/iteration_$tmp.out $tmp $tmp
144 for i in `seq "$4" "$5" "$6"`; do
145 bench_command $i $tmp$i ./iteration_bench -m $2 --iter-mode $1 --loops $3 --strlen $i
146 done
147 }
148
149 function bench_substring()
150 {
151 if [ $# -lt 4 ]; then
152 echo "Wrong arguments for benchmark substring."
153 usage
154 exit
155 fi
156 echo "Generating executable substr_bench for variant $variant"
157 ../../bin/nitc --global substr_bench.nit
158
159 bench_substring_variant "string" $1 $2 $3 $4
160 bench_substring_variant "buffer" $1 $2 $3 $4
161
162 rm substr_bench
163 }
164
165 # $1: string or buffer
166 # $2: loops
167 # $3: strlen min
168 # $4: strlen increment
169 # $5: strlen max
170 function bench_substring_variant()
171 {
172 tmp="${variant}_$1"
173 prepare_res out/substring/substring_$tmp.out $tmp $tmp
174 for i in `seq "$3" "$4" "$5"`; do
175 bench_command $i $tmp$i ./substr_bench -m $1 --loops $2 --strlen $i
176 done
177 }
178
179 function bench_compiler()
180 {
181 prepare_res out/compiler/compiler_$variant.out compiler_$variant compiler_$variant
182
183 echo "Pre-compiling nitc"
184 # Do it twice before bench to have stable times when generating C
185 ../../bin/nitc ../../src/nitc.nit -o ../../bin/nitc
186 echo "nitc (1/2)"
187 ../../bin/nitc ../../src/nitc.nit -o ../../bin/nitc
188 echo "nitc (2/2)"
189
190 bench_command nitc nitc_$variant ../../bin/nitc ../../src/nitc.nit
191
192 rm nitc
193 }
194
195 function bench_basic()
196 {
197 ../../bin/nitc ../../examples/hello_world.nit
198 ./hello_world
199 rm hello_world
200 }
201
202 function launch_bench()
203 {
204 echo "---------------------------------------------------------"
205 echo " Trying variant $variant for benchmark $bench"
206 echo "---------------------------------------------------------"
207 git diff-index --quiet HEAD || {
208 echo "Cannot run benches on a dirty working directory."
209 echo "Please commit or stash your modifications and relaunch the command."
210 exit 1
211 }
212 git am $curr_rev || {
213 echo "Error when applying patch $curr_rev"
214 git am --abort;
215 exit 1;
216 }
217 if [ "$need_bootstrap" = true ]; then
218 prepare_compiler
219 fi
220 bench_$bench "$@";
221 git reset --hard $head
222 }
223
224 function prepare_compiler()
225 {
226 cd ../../c_src
227 rm nitc
228 make clean
229 cd ../src
230 ./ncall.sh
231 mv nitc.good ../bin/nitc
232 cd ../benchmarks/strings
233 }
234
235 function main()
236 {
237 stop=false
238 while [ "$stop" = false ]; do
239 case "$1" in
240 -v) verbose=true; shift;;
241 -h) usage; exit;;
242 -n) count="$2"; shift; shift;;
243 *) stop=true
244 esac
245 done
246
247 if [ $# -lt 1 ]; then
248 usage;
249 exit;
250 fi
251
252 isok=false
253 for i in $benches; do
254 if [ $1 = $i ]; then
255 isok=true;
256 fi
257 done
258 if [ "$isok" = false ]; then
259 usage;
260 exit;
261 fi
262
263 bench=$1
264 shift;
265
266 git diff-index --quiet HEAD || {
267 echo "Cannot run benches on a dirty working directory."
268 echo "Please commit or stash your modifications and relaunch the command."
269 exit 1
270 }
271
272 head=`git rev-parse HEAD`
273 variant="HEAD"
274 need_plot=true
275 need_bootstrap=false
276
277 if [ "$bench" = "basic" ]; then
278 need_plot=false
279 fi
280
281 if [ ! -d out ]; then
282 mkdir out
283 fi
284 cd out
285
286 if [ -d $bench ]; then
287 rm $bench/*
288 else
289 mkdir $bench
290 fi
291 cd ..
292
293 echo "---------------------------------------------------------"
294 echo " Trying variant HEAD for benchmark $bench"
295 echo "---------------------------------------------------------"
296 bench_$bench "$@";
297
298 for i in lib_variants/regular/*; do
299 curr_rev=$i
300 variant=`basename "$i" | cut -f 1 -d '.'`
301 launch_bench "$@"
302 done
303
304 need_bootstrap=true
305 for i in lib_variants/need_bootstrap/*; do
306 curr_rev=$i
307 variant=`basename "$i" | cut -f 1 -d '.'`
308 launch_bench "$@"
309 done
310
311 if [ "${need_plot}" = true ]; then
312 plot out/$bench/$bench.gnu
313 fi
314 }
315
316 main "$@";