Merge: doc: fixed some typos and other misc. corrections
[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 # Global variables
17
18 # Number of times a command must be run with bench_command
19 count=1
20
21 # User time limit (in second) before a command is aborted
22 usertimelimit=120
23
24 # Common functions for all the bench scripts
25
26 # Run a single command multiple time and store the execution times
27 # in the current $res file.
28 #
29 # $1: title of the command
30 # $2: long desription of the command
31 # rest: the command to execute
32 function bench_command()
33 {
34 if [ "$dry_run" = "true" ]; then return; fi
35 local title="$1"
36 local desc="$2"
37 shift
38 shift
39 if test "$verbose" = true; then outputopts="/dev/stdout"; else outputopts="/dev/null"; fi
40 timeout="time.out"
41 echo "$title" > "$timeout"
42 echo "# $desc" >> "$timeout"
43 echo "\$ $@" >> "$timeout"
44 echo
45 echo "** [$title] $desc **"
46 echo " $ $@"
47
48 failed=
49
50 # Execute the commands $count times
51 for i in `seq 1 "$count"`; do
52 (
53 ulimit -t "$usertimelimit" 2> /dev/null
54 /usr/bin/time -f "%U" -o "$timeout" -a "$@" > $outputopts 2>&1
55 ) || { err=$?; failed=true; die "$1: failed with $err"; }
56 echo -n "$i. "
57 tail -n 1 "$timeout"
58 test -n "$failed" && break
59 done
60
61 line=`compute_stats "$timeout"`
62 echo "$line ($res)"
63 echo $line >> "$res"
64
65 if test -n "$xml"; then
66 echo >>"$xml" "<testcase classname='bench.`basename $res .dat`' name='$title' time='`echo $line | cut -f 1 -d " "`' timestamp='`date -Iseconds`'>"
67 if test -n "$failed"; then
68 echo >>"$xml" "<error message='Command failed'/>"
69 fi
70 echo >>"$xml" "</testcase>"
71 fi
72 test -z "$failed"
73 }
74
75 # Run a simble command witout storing the execution time
76 # Used to display command on verbose and skip long executions when dry_run is given
77 # $@ command to execute
78 function run_command()
79 {
80 if [ "$dry_run" = "true" ]; then return; fi
81 echo " $ $@"
82 "$@" || die "$@: failed"
83 }
84
85 # Check if the test should be skiped according to its name
86 # $1: name of the test
87 # $2: description of the test
88 # $NOTSKIPED: arguments
89 function skip_test()
90 {
91 if test -z "$NOTSKIPED"; then
92 echo "* $1"
93 return 0
94 fi
95 if test "$NOTSKIPED" = "all"; then
96 : # Execute anyway
97 elif echo "$1" | egrep "$NOTSKIPED" >/dev/null 2>&1; then
98 : # Found one to execute
99 else
100 return 0
101 fi
102 if test -n "$html"; then
103 echo >>"$html" "<h2 id="$1">$1</h2>"
104 fi
105 echo "*"
106 echo "* $1 *****"
107 echo "*"
108 return 1
109 }
110
111 # Helper function. Print the error message and set $died to 1
112 function die()
113 {
114 echo >&2 "error: $*"
115 died=1
116 return 1
117 }