benchmarks/string: Added String benchmarks
[nit.git] / benchmarks / bench_common.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 # Common functions for all the bench scripts
17
18 # Run a single command multiple time and store the execution times
19 # in the current $res file.
20 #
21 # $1: title of the command
22 # $2: long desription of the command
23 # rest: the command to execute
24 function bench_command()
25 {
26 if [ "$dry_run" = "true" ]; then return; fi
27 local title="$1"
28 local desc="$2"
29 shift
30 shift
31 if test "$verbose" = true; then outputopts="/dev/stdout"; else outputopts="/dev/null"; fi
32 timeout="time.out"
33 echo "$title" > "$timeout"
34 echo "# $desc" >> "$timeout"
35 echo "\$ $@" >> "$timeout"
36 echo
37 echo "** [$title] $desc **"
38 echo " $ $@"
39
40 # Execute the commands $count times
41 for i in `seq 1 "$count"`; do
42 /usr/bin/time -f "%U" -o "$timeout" -a "$@" > $outputopts 2>&1 || die "$1: failed"
43 echo -n "$i. "
44 tail -n 1 "$timeout"
45 done
46
47 line=`compute_stats "$timeout"`
48 echo "$line ($res)"
49 echo $line >> "$res"
50 }
51
52 # Run a simble command witout storing the execution time
53 # Used to display command on verbose and skip long executions when dry_run is given
54 # $@ command to execute
55 function run_command()
56 {
57 if [ "$dry_run" = "true" ]; then return; fi
58 echo " $ $@"
59 "$@" || die "$@: failed"
60 }
61
62 # Check if the test should be skiped according to its name
63 # $1: name of the test
64 # $2: description of the test
65 # $NOTSKIPED: arguments
66 function skip_test()
67 {
68 if test -z "$NOTSKIPED"; then
69 echo "* $1"
70 return 0
71 fi
72 if test "$NOTSKIPED" = "all"; then
73 : # Execute anyway
74 elif echo "$1" | egrep "$NOTSKIPED" >/dev/null 2>&1; then
75 : # Found one to execute
76 else
77 return 0
78 fi
79 if test -n "$html"; then
80 echo >>"$html" "<h2>$1</h2>"
81 fi
82 echo "*"
83 echo "* $1 *****"
84 echo "*"
85 return 1
86 }
87