README: document nit_env.sh
[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 test -z "$xml" && return
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 }
60
61 # Run a simble command witout storing the execution time
62 # Used to display command on verbose and skip long executions when dry_run is given
63 # $@ command to execute
64 function run_command()
65 {
66 if [ "$dry_run" = "true" ]; then return; fi
67 echo " $ $@"
68 "$@" || die "$@: failed"
69 }
70
71 # Check if the test should be skiped according to its name
72 # $1: name of the test
73 # $2: description of the test
74 # $NOTSKIPED: arguments
75 function skip_test()
76 {
77 if test -z "$NOTSKIPED"; then
78 echo "* $1"
79 return 0
80 fi
81 if test "$NOTSKIPED" = "all"; then
82 : # Execute anyway
83 elif echo "$1" | egrep "$NOTSKIPED" >/dev/null 2>&1; then
84 : # Found one to execute
85 else
86 return 0
87 fi
88 if test -n "$html"; then
89 echo >>"$html" "<h2>$1</h2>"
90 fi
91 echo "*"
92 echo "* $1 *****"
93 echo "*"
94 return 1
95 }
96
97 # Helper function. Print the error message and set $died to 1
98 function die()
99 {
100 echo >&2 "error: $*"
101 died=1
102 }