Merge branch 'dr/ref-filter-push-track-fix'
[git] / t / perf / perf-lib.sh
1 # Performance testing framework.  Each perf script starts much like
2 # a normal test script, except it sources this library instead of
3 # test-lib.sh.  See t/perf/README for documentation.
4 #
5 # Copyright (c) 2011 Thomas Rast
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see http://www.gnu.org/licenses/ .
19
20 # These variables must be set before the inclusion of test-lib.sh below,
21 # because it will change our working directory.
22 TEST_DIRECTORY=$(pwd)/..
23 TEST_OUTPUT_DIRECTORY=$(pwd)
24 ABSOLUTE_GIT_TEST_INSTALLED=$(
25         test -n "$GIT_TEST_INSTALLED" && cd "$GIT_TEST_INSTALLED" && pwd)
26
27 TEST_NO_CREATE_REPO=t
28 TEST_NO_MALLOC_CHECK=t
29
30 . ../test-lib.sh
31
32 if test -z "$GIT_TEST_INSTALLED"; then
33         perf_results_prefix=
34 else
35         perf_results_prefix=$(printf "%s" "${GIT_TEST_INSTALLED%/bin-wrappers}" | tr -c "[a-zA-Z0-9]" "[_*]")"."
36         GIT_TEST_INSTALLED=$ABSOLUTE_GIT_TEST_INSTALLED
37 fi
38
39 # Variables from test-lib that are normally internal to the tests; we
40 # need to export them for test_perf subshells
41 export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
42
43 MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
44 export MODERN_GIT
45
46 perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results
47 test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
48 mkdir -p "$perf_results_dir"
49 rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
50
51 die_if_build_dir_not_repo () {
52         if ! ( cd "$TEST_DIRECTORY/.." &&
53                     git rev-parse --build-dir >/dev/null 2>&1 ); then
54                 error "No $1 defined, and your build directory is not a repo"
55         fi
56 }
57
58 if test -z "$GIT_PERF_REPO"; then
59         die_if_build_dir_not_repo '$GIT_PERF_REPO'
60         GIT_PERF_REPO=$TEST_DIRECTORY/..
61 fi
62 if test -z "$GIT_PERF_LARGE_REPO"; then
63         die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
64         GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
65 fi
66
67 test_perf_do_repo_symlink_config_ () {
68         test_have_prereq SYMLINKS || git config core.symlinks false
69 }
70
71 test_perf_create_repo_from () {
72         test "$#" = 2 ||
73         BUG "not 2 parameters to test-create-repo"
74         repo="$1"
75         source="$2"
76         source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
77         objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
78         mkdir -p "$repo/.git"
79         (
80                 cd "$source" &&
81                 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
82                         cp -R "$objects_dir" "$repo/.git/"; } &&
83                 for stuff in "$source_git"/*; do
84                         case "$stuff" in
85                                 */objects|*/hooks|*/config|*/commondir)
86                                         ;;
87                                 *)
88                                         cp -R "$stuff" "$repo/.git/" || exit 1
89                                         ;;
90                         esac
91                 done
92         ) &&
93         (
94                 cd "$repo" &&
95                 "$MODERN_GIT" init -q &&
96                 test_perf_do_repo_symlink_config_ &&
97                 mv .git/hooks .git/hooks-disabled 2>/dev/null &&
98                 if test -f .git/index.lock
99                 then
100                         # We may be copying a repo that can't run "git
101                         # status" due to a locked index. Since we have
102                         # a copy it's fine to remove the lock.
103                         rm .git/index.lock
104                 fi
105         ) || error "failed to copy repository '$source' to '$repo'"
106 }
107
108 # call at least one of these to establish an appropriately-sized repository
109 test_perf_fresh_repo () {
110         repo="${1:-$TRASH_DIRECTORY}"
111         "$MODERN_GIT" init -q "$repo" &&
112         (
113                 cd "$repo" &&
114                 test_perf_do_repo_symlink_config_
115         )
116 }
117
118 test_perf_default_repo () {
119         test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
120 }
121 test_perf_large_repo () {
122         if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
123                 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
124                 echo "warning: This will work, but may not be a sufficiently large repo" >&2
125                 echo "warning: for representative measurements." >&2
126         fi
127         test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
128 }
129 test_checkout_worktree () {
130         git checkout-index -u -a ||
131         error "git checkout-index failed"
132 }
133
134 # Performance tests should never fail.  If they do, stop immediately
135 immediate=t
136
137 # Perf tests require GNU time
138 case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
139 GTIME="${GTIME:-/usr/bin/time}"
140
141 test_run_perf_ () {
142         test_cleanup=:
143         test_export_="test_cleanup"
144         export test_cleanup test_export_
145         "$GTIME" -f "%E %U %S" -o test_time.$i "$SHELL" -c '
146 . '"$TEST_DIRECTORY"/test-lib-functions.sh'
147 test_export () {
148         [ $# != 0 ] || return 0
149         test_export_="$test_export_\\|$1"
150         shift
151         test_export "$@"
152 }
153 '"$1"'
154 ret=$?
155 set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars
156 exit $ret' >&3 2>&4
157         eval_ret=$?
158
159         if test $eval_ret = 0 || test -n "$expecting_failure"
160         then
161                 test_eval_ "$test_cleanup"
162                 . ./test_vars || error "failed to load updated environment"
163         fi
164         if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
165                 echo ""
166         fi
167         return "$eval_ret"
168 }
169
170 test_wrapper_ () {
171         test_wrapper_func_=$1; shift
172         test_start_
173         test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
174         test "$#" = 2 ||
175         BUG "not 2 or 3 parameters to test-expect-success"
176         export test_prereq
177         if ! test_skip "$@"
178         then
179                 base=$(basename "$0" .sh)
180                 echo "$test_count" >>"$perf_results_dir"/$base.subtests
181                 echo "$1" >"$perf_results_dir"/$base.$test_count.descr
182                 base="$perf_results_dir"/"$perf_results_prefix$(basename "$0" .sh)"."$test_count"
183                 "$test_wrapper_func_" "$@"
184         fi
185
186         test_finish_
187 }
188
189 test_perf_ () {
190         if test -z "$verbose"; then
191                 printf "%s" "perf $test_count - $1:"
192         else
193                 echo "perf $test_count - $1:"
194         fi
195         for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
196                 say >&3 "running: $2"
197                 if test_run_perf_ "$2"
198                 then
199                         if test -z "$verbose"; then
200                                 printf " %s" "$i"
201                         else
202                                 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
203                         fi
204                 else
205                         test -z "$verbose" && echo
206                         test_failure_ "$@"
207                         break
208                 fi
209         done
210         if test -z "$verbose"; then
211                 echo " ok"
212         else
213                 test_ok_ "$1"
214         fi
215         "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times
216 }
217
218 test_perf () {
219         test_wrapper_ test_perf_ "$@"
220 }
221
222 test_size_ () {
223         say >&3 "running: $2"
224         if test_eval_ "$2" 3>"$base".size; then
225                 test_ok_ "$1"
226         else
227                 test_failure_ "$@"
228         fi
229 }
230
231 test_size () {
232         test_wrapper_ test_size_ "$@"
233 }
234
235 # We extend test_done to print timings at the end (./run disables this
236 # and does it after running everything)
237 test_at_end_hook_ () {
238         if test -z "$GIT_PERF_AGGREGATING_LATER"; then
239                 ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") )
240         fi
241 }
242
243 test_export () {
244         export "$@"
245 }