1efac255e036cb85ca69e653a6353f3d1c289d5b
[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 failed=
41
42 # Execute the commands $count times
43 for i in `seq 1 "$count"`; do
44 /usr/bin/time -f "%U" -o "$timeout" -a "$@" > $outputopts 2>&1 || { failed=true; die "$1: failed"; }
45 echo -n "$i. "
46 tail -n 1 "$timeout"
47 done
48
49 line=`compute_stats "$timeout"`
50 echo "$line ($res)"
51 echo $line >> "$res"
52
53 if test -n "$xml"; then
54 echo >>"$xml" "<testcase classname='bench.`basename $res .dat`' name='$title' time='`echo $line | cut -f 1 -d " "`' timestamp='`date -Iseconds`'>"
55 if test -n "$failed"; then
56 echo >>"$xml" "<error message='Command failed'/>"
57 fi
58 echo >>"$xml" "</testcase>"
59 fi
60 test -z "$failed"
61 }
62
63 # Run a simble command witout storing the execution time
64 # Used to display command on verbose and skip long executions when dry_run is given
65 # $@ command to execute
66 function run_command()
67 {
68 if [ "$dry_run" = "true" ]; then return; fi
69 echo " $ $@"
70 "$@" || die "$@: failed"
71 }
72
73 # Check if the test should be skiped according to its name
74 # $1: name of the test
75 # $2: description of the test
76 # $NOTSKIPED: arguments
77 function skip_test()
78 {
79 if test -z "$NOTSKIPED"; then
80 echo "* $1"
81 return 0
82 fi
83 if test "$NOTSKIPED" = "all"; then
84 : # Execute anyway
85 elif echo "$1" | egrep "$NOTSKIPED" >/dev/null 2>&1; then
86 : # Found one to execute
87 else
88 return 0
89 fi
90 if test -n "$html"; then
91 echo >>"$html" "<h2 id="$1">$1</h2>"
92 fi
93 echo "*"
94 echo "* $1 *****"
95 echo "*"
96 return 1
97 }
98
99 # Helper function. Print the error message and set $died to 1
100 function die()
101 {
102 echo >&2 "error: $*"
103 died=1
104 return 1
105 }