perf: fix when running with TEST_OUTPUT_DIRECTORY
[alt-git.git] / t / perf / perf-lib.sh
blobf5ed092ee591ca3c350e274495acf1e37ce868c6
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.
5 # Copyright (c) 2011 Thomas Rast
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.
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.
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/ .
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)
25 TEST_NO_CREATE_REPO=t
26 TEST_NO_MALLOC_CHECK=t
28 . ../test-lib.sh
30 if test -n "$GIT_TEST_INSTALLED" -a -z "$PERF_SET_GIT_TEST_INSTALLED"
31 then
32 error "Do not use GIT_TEST_INSTALLED with the perf tests.
34 Instead use:
36 ./run <path-to-git> -- <tests>
38 See t/perf/README for details."
41 # Variables from test-lib that are normally internal to the tests; we
42 # need to export them for test_perf subshells
43 export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
45 MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
46 export MODERN_GIT
48 perf_results_dir=$TEST_RESULTS_DIR
49 test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
50 mkdir -p "$perf_results_dir"
51 rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
53 die_if_build_dir_not_repo () {
54 if ! ( cd "$TEST_DIRECTORY/.." &&
55 git rev-parse --build-dir >/dev/null 2>&1 ); then
56 error "No $1 defined, and your build directory is not a repo"
60 if test -z "$GIT_PERF_REPO"; then
61 die_if_build_dir_not_repo '$GIT_PERF_REPO'
62 GIT_PERF_REPO=$TEST_DIRECTORY/..
64 if test -z "$GIT_PERF_LARGE_REPO"; then
65 die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
66 GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
69 test_perf_do_repo_symlink_config_ () {
70 test_have_prereq SYMLINKS || git config core.symlinks false
73 test_perf_copy_repo_contents () {
74 for stuff in "$1"/*
76 case "$stuff" in
77 */objects|*/hooks|*/config|*/commondir|*/gitdir|*/worktrees)
80 cp -R "$stuff" "$repo/.git/" || exit 1
82 esac
83 done
86 test_perf_create_repo_from () {
87 test "$#" = 2 ||
88 BUG "not 2 parameters to test-create-repo"
89 repo="$1"
90 source="$2"
91 source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
92 objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
93 common_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-common-dir)"
94 mkdir -p "$repo/.git"
96 cd "$source" &&
97 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
98 cp -R "$objects_dir" "$repo/.git/"; } &&
100 # common_dir must come first here, since we want source_git to
101 # take precedence and overwrite any overlapping files
102 test_perf_copy_repo_contents "$common_dir"
103 if test "$source_git" != "$common_dir"
104 then
105 test_perf_copy_repo_contents "$source_git"
107 ) &&
109 cd "$repo" &&
110 "$MODERN_GIT" init -q &&
111 test_perf_do_repo_symlink_config_ &&
112 mv .git/hooks .git/hooks-disabled 2>/dev/null &&
113 if test -f .git/index.lock
114 then
115 # We may be copying a repo that can't run "git
116 # status" due to a locked index. Since we have
117 # a copy it's fine to remove the lock.
118 rm .git/index.lock
120 ) || error "failed to copy repository '$source' to '$repo'"
123 # call at least one of these to establish an appropriately-sized repository
124 test_perf_fresh_repo () {
125 repo="${1:-$TRASH_DIRECTORY}"
126 "$MODERN_GIT" init -q "$repo" &&
128 cd "$repo" &&
129 test_perf_do_repo_symlink_config_
133 test_perf_default_repo () {
134 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
136 test_perf_large_repo () {
137 if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
138 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
139 echo "warning: This will work, but may not be a sufficiently large repo" >&2
140 echo "warning: for representative measurements." >&2
142 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
144 test_checkout_worktree () {
145 git checkout-index -u -a ||
146 error "git checkout-index failed"
149 # Performance tests should never fail. If they do, stop immediately
150 immediate=t
152 # Perf tests require GNU time
153 case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
154 GTIME="${GTIME:-/usr/bin/time}"
156 test_run_perf_ () {
157 test_cleanup=:
158 test_export_="test_cleanup"
159 export test_cleanup test_export_
160 "$GTIME" -f "%E %U %S" -o test_time.$i "$SHELL" -c '
161 . '"$TEST_DIRECTORY"/test-lib-functions.sh'
162 test_export () {
163 test_export_="$test_export_ $*"
165 '"$1"'
166 ret=$?
167 needles=
168 for v in $test_export_
170 needles="$needles;s/^$v=/export $v=/p"
171 done
172 set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars
173 exit $ret' >&3 2>&4
174 eval_ret=$?
176 if test $eval_ret = 0 || test -n "$expecting_failure"
177 then
178 test_eval_ "$test_cleanup"
179 . ./test_vars || error "failed to load updated environment"
181 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
182 echo ""
184 return "$eval_ret"
187 test_wrapper_ () {
188 test_wrapper_func_=$1; shift
189 test_start_
190 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
191 test "$#" = 2 ||
192 BUG "not 2 or 3 parameters to test-expect-success"
193 export test_prereq
194 if ! test_skip "$@"
195 then
196 base=$(basename "$0" .sh)
197 echo "$test_count" >>"$perf_results_dir"/$base.subtests
198 echo "$1" >"$perf_results_dir"/$base.$test_count.descr
199 base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count"
200 "$test_wrapper_func_" "$@"
203 test_finish_
206 test_perf_ () {
207 if test -z "$verbose"; then
208 printf "%s" "perf $test_count - $1:"
209 else
210 echo "perf $test_count - $1:"
212 for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
213 say >&3 "running: $2"
214 if test_run_perf_ "$2"
215 then
216 if test -z "$verbose"; then
217 printf " %s" "$i"
218 else
219 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
221 else
222 test -z "$verbose" && echo
223 test_failure_ "$@"
224 break
226 done
227 if test -z "$verbose"; then
228 echo " ok"
229 else
230 test_ok_ "$1"
232 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result
235 test_perf () {
236 test_wrapper_ test_perf_ "$@"
239 test_size_ () {
240 say >&3 "running: $2"
241 if test_eval_ "$2" 3>"$base".result; then
242 test_ok_ "$1"
243 else
244 test_failure_ "$@"
248 test_size () {
249 test_wrapper_ test_size_ "$@"
252 # We extend test_done to print timings at the end (./run disables this
253 # and does it after running everything)
254 test_at_end_hook_ () {
255 if test -z "$GIT_PERF_AGGREGATING_LATER"; then
257 cd "$TEST_DIRECTORY"/perf &&
258 ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $(basename "$0")
263 test_export () {
264 export "$@"
267 test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false'