Merge branch 'jc/test-must-be-empty'
[git] / t / perf / run
1 #!/bin/sh
2
3 case "$1" in
4         --help)
5                 echo "usage: $0 [--config file] [other_git_tree...] [--] [test_scripts]"
6                 exit 0
7                 ;;
8         --config)
9                 shift
10                 GIT_PERF_CONFIG_FILE=$(cd "$(dirname "$1")"; pwd)/$(basename "$1")
11                 export GIT_PERF_CONFIG_FILE
12                 shift ;;
13 esac
14
15 die () {
16         echo >&2 "error: $*"
17         exit 1
18 }
19
20 run_one_dir () {
21         if test $# -eq 0; then
22                 set -- p????-*.sh
23         fi
24         echo "=== Running $# tests in ${GIT_TEST_INSTALLED:-this tree} ==="
25         for t in "$@"; do
26                 ./$t $GIT_TEST_OPTS
27         done
28 }
29
30 unpack_git_rev () {
31         rev=$1
32         echo "=== Unpacking $rev in build/$rev ==="
33         mkdir -p build/$rev
34         (cd "$(git rev-parse --show-cdup)" && git archive --format=tar $rev) |
35         (cd build/$rev && tar x)
36 }
37
38 build_git_rev () {
39         rev=$1
40         name="$2"
41         for config in config.mak config.mak.autogen config.status
42         do
43                 if test -e "../../$config"
44                 then
45                         cp "../../$config" "build/$rev/"
46                 fi
47         done
48         echo "=== Building $rev ($name) ==="
49         (
50                 cd build/$rev &&
51                 if test -n "$GIT_PERF_MAKE_COMMAND"
52                 then
53                         sh -c "$GIT_PERF_MAKE_COMMAND"
54                 else
55                         make $GIT_PERF_MAKE_OPTS
56                 fi
57         ) || die "failed to build revision '$mydir'"
58 }
59
60 run_dirs_helper () {
61         mydir=${1%/}
62         shift
63         while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
64                 shift
65         done
66         if test $# -gt 0 -a "$1" = --; then
67                 shift
68         fi
69         if [ ! -d "$mydir" ]; then
70                 rev=$(git rev-parse --verify "$mydir" 2>/dev/null) ||
71                 die "'$mydir' is neither a directory nor a valid revision"
72                 if [ ! -d build/$rev ]; then
73                         unpack_git_rev $rev
74                 fi
75                 build_git_rev $rev "$mydir"
76                 mydir=build/$rev
77         fi
78         if test "$mydir" = .; then
79                 unset GIT_TEST_INSTALLED
80         else
81                 GIT_TEST_INSTALLED="$mydir/bin-wrappers"
82                 # Older versions of git lacked bin-wrappers; fallback to the
83                 # files in the root.
84                 test -d "$GIT_TEST_INSTALLED" || GIT_TEST_INSTALLED=$mydir
85                 export GIT_TEST_INSTALLED
86         fi
87         run_one_dir "$@"
88 }
89
90 run_dirs () {
91         while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
92                 run_dirs_helper "$@"
93                 shift
94         done
95 }
96
97 get_subsections () {
98         section="$1"
99         test -z "$GIT_PERF_CONFIG_FILE" && return
100         git config -f "$GIT_PERF_CONFIG_FILE" --name-only --get-regex "$section\..*\.[^.]+" |
101         sed -e "s/$section\.\(.*\)\..*/\1/" | sort | uniq
102 }
103
104 get_var_from_env_or_config () {
105         env_var="$1"
106         conf_sec="$2"
107         conf_var="$3"
108         conf_opts="$4" # optional
109         # $5 can be set to a default value
110
111         # Do nothing if the env variable is already set
112         eval "test -z \"\${$env_var+x}\"" || return
113
114         test -z "$GIT_PERF_CONFIG_FILE" && return
115
116         # Check if the variable is in the config file
117         if test -n "$GIT_PERF_SUBSECTION"
118         then
119                 var="$conf_sec.$GIT_PERF_SUBSECTION.$conf_var"
120                 conf_value=$(git config $conf_opts -f "$GIT_PERF_CONFIG_FILE" "$var") &&
121                 eval "$env_var=\"$conf_value\"" && return
122         fi
123         var="$conf_sec.$conf_var"
124         conf_value=$(git config $conf_opts -f "$GIT_PERF_CONFIG_FILE" "$var") &&
125         eval "$env_var=\"$conf_value\"" && return
126
127         test -n "${5+x}" && eval "$env_var=\"$5\""
128 }
129
130 run_subsection () {
131         get_var_from_env_or_config "GIT_PERF_REPEAT_COUNT" "perf" "repeatCount" "--int" 3
132         export GIT_PERF_REPEAT_COUNT
133
134         get_var_from_env_or_config "GIT_PERF_DIRS_OR_REVS" "perf" "dirsOrRevs"
135         set -- $GIT_PERF_DIRS_OR_REVS "$@"
136
137         get_var_from_env_or_config "GIT_PERF_MAKE_COMMAND" "perf" "makeCommand"
138         get_var_from_env_or_config "GIT_PERF_MAKE_OPTS" "perf" "makeOpts"
139
140         get_var_from_env_or_config "GIT_PERF_REPO_NAME" "perf" "repoName"
141         export GIT_PERF_REPO_NAME
142
143         GIT_PERF_AGGREGATING_LATER=t
144         export GIT_PERF_AGGREGATING_LATER
145
146         if test $# = 0 -o "$1" = -- -o -f "$1"; then
147                 set -- . "$@"
148         fi
149
150         codespeed_opt=
151         test "$GIT_PERF_CODESPEED_OUTPUT" = "true" && codespeed_opt="--codespeed"
152
153         run_dirs "$@"
154
155         if test -z "$GIT_PERF_SEND_TO_CODESPEED"
156         then
157                 ./aggregate.perl $codespeed_opt "$@"
158         else
159                 json_res_file="test-results/$GIT_PERF_SUBSECTION/aggregate.json"
160                 ./aggregate.perl --codespeed "$@" | tee "$json_res_file"
161                 send_data_url="$GIT_PERF_SEND_TO_CODESPEED/result/add/json/"
162                 curl -v --request POST --data-urlencode "json=$(cat "$json_res_file")" "$send_data_url"
163         fi
164 }
165
166 get_var_from_env_or_config "GIT_PERF_CODESPEED_OUTPUT" "perf" "codespeedOutput" "--bool"
167 get_var_from_env_or_config "GIT_PERF_SEND_TO_CODESPEED" "perf" "sendToCodespeed"
168
169 cd "$(dirname $0)"
170 . ../../GIT-BUILD-OPTIONS
171
172 mkdir -p test-results
173 get_subsections "perf" >test-results/run_subsections.names
174
175 if test $(wc -l <test-results/run_subsections.names) -eq 0
176 then
177         (
178                 run_subsection "$@"
179         )
180 else
181         while read -r subsec
182         do
183                 (
184                         GIT_PERF_SUBSECTION="$subsec"
185                         export GIT_PERF_SUBSECTION
186                         echo "======== Run for subsection '$GIT_PERF_SUBSECTION' ========"
187                         run_subsection "$@"
188                 )
189         done <test-results/run_subsections.names
190 fi