]> git.ipfire.org Git - thirdparty/git.git/blame - t/perf/perf-lib.sh
Merge branch 'ea/blame-use-oideq'
[thirdparty/git.git] / t / perf / perf-lib.sh
CommitLineData
c74c7203
JN
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.
342e9ef2
TR
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
0baf78e7
JK
20# These variables must be set before the inclusion of test-lib.sh below,
21# because it will change our working directory.
342e9ef2
TR
22TEST_DIRECTORY=$(pwd)/..
23TEST_OUTPUT_DIRECTORY=$(pwd)
342e9ef2
TR
24
25TEST_NO_CREATE_REPO=t
ee1431bf 26TEST_NO_MALLOC_CHECK=t
342e9ef2
TR
27
28. ../test-lib.sh
29
82b7eb23
ÆAB
30if test -n "$GIT_TEST_INSTALLED" -a -z "$PERF_SET_GIT_TEST_INSTALLED"
31then
32 error "Do not use GIT_TEST_INSTALLED with the perf tests.
33
34Instead use:
35
36 ./run <path-to-git> -- <tests>
37
38See t/perf/README for details."
39fi
40
561ae067
TR
41# Variables from test-lib that are normally internal to the tests; we
42# need to export them for test_perf subshells
43export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
44
1a0962de
JK
45MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
46export MODERN_GIT
47
342e9ef2 48perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results
5d445f34 49test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
342e9ef2
TR
50mkdir -p "$perf_results_dir"
51rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
52
342e9ef2
TR
53die_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"
57 fi
58}
59
60if test -z "$GIT_PERF_REPO"; then
61 die_if_build_dir_not_repo '$GIT_PERF_REPO'
62 GIT_PERF_REPO=$TEST_DIRECTORY/..
63fi
64if 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/..
67fi
68
91de27c5
ÆAB
69test_perf_do_repo_symlink_config_ () {
70 test_have_prereq SYMLINKS || git config core.symlinks false
71}
72
342e9ef2
TR
73test_perf_create_repo_from () {
74 test "$#" = 2 ||
165293af 75 BUG "not 2 parameters to test-create-repo"
342e9ef2
TR
76 repo="$1"
77 source="$2"
83d4a409 78 source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
1a0962de 79 objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
342e9ef2
TR
80 mkdir -p "$repo/.git"
81 (
e2522f2a 82 cd "$source" &&
7501b592
JS
83 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
84 cp -R "$objects_dir" "$repo/.git/"; } &&
342e9ef2
TR
85 for stuff in "$source_git"/*; do
86 case "$stuff" in
7501b592 87 */objects|*/hooks|*/config|*/commondir)
342e9ef2
TR
88 ;;
89 *)
7501b592 90 cp -R "$stuff" "$repo/.git/" || exit 1
342e9ef2
TR
91 ;;
92 esac
e2522f2a
RS
93 done
94 ) &&
95 (
7501b592 96 cd "$repo" &&
91de27c5
ÆAB
97 "$MODERN_GIT" init -q &&
98 test_perf_do_repo_symlink_config_ &&
154ffeec
ÆAB
99 mv .git/hooks .git/hooks-disabled 2>/dev/null &&
100 if test -f .git/index.lock
101 then
102 # We may be copying a repo that can't run "git
103 # status" due to a locked index. Since we have
104 # a copy it's fine to remove the lock.
105 rm .git/index.lock
106 fi
342e9ef2
TR
107 ) || error "failed to copy repository '$source' to '$repo'"
108}
109
110# call at least one of these to establish an appropriately-sized repository
91de27c5
ÆAB
111test_perf_fresh_repo () {
112 repo="${1:-$TRASH_DIRECTORY}"
113 "$MODERN_GIT" init -q "$repo" &&
114 (
115 cd "$repo" &&
116 test_perf_do_repo_symlink_config_
117 )
118}
119
342e9ef2
TR
120test_perf_default_repo () {
121 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
122}
123test_perf_large_repo () {
124 if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
125 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
126 echo "warning: This will work, but may not be a sufficiently large repo" >&2
127 echo "warning: for representative measurements." >&2
128 fi
129 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
130}
131test_checkout_worktree () {
132 git checkout-index -u -a ||
133 error "git checkout-index failed"
134}
135
136# Performance tests should never fail. If they do, stop immediately
137immediate=t
138
e3efa94b
JS
139# Perf tests require GNU time
140case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
141GTIME="${GTIME:-/usr/bin/time}"
142
342e9ef2
TR
143test_run_perf_ () {
144 test_cleanup=:
145 test_export_="test_cleanup"
146 export test_cleanup test_export_
e3efa94b 147 "$GTIME" -f "%E %U %S" -o test_time.$i "$SHELL" -c '
1cbc3240 148. '"$TEST_DIRECTORY"/test-lib-functions.sh'
342e9ef2
TR
149test_export () {
150 [ $# != 0 ] || return 0
151 test_export_="$test_export_\\|$1"
152 shift
153 test_export "$@"
154}
155'"$1"'
156ret=$?
157set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars
158exit $ret' >&3 2>&4
159 eval_ret=$?
160
161 if test $eval_ret = 0 || test -n "$expecting_failure"
162 then
163 test_eval_ "$test_cleanup"
164 . ./test_vars || error "failed to load updated environment"
165 fi
166 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
167 echo ""
168 fi
169 return "$eval_ret"
170}
171
968e77a5
JK
172test_wrapper_ () {
173 test_wrapper_func_=$1; shift
62a23c9f 174 test_start_
342e9ef2
TR
175 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
176 test "$#" = 2 ||
165293af 177 BUG "not 2 or 3 parameters to test-expect-success"
342e9ef2
TR
178 export test_prereq
179 if ! test_skip "$@"
180 then
181 base=$(basename "$0" .sh)
182 echo "$test_count" >>"$perf_results_dir"/$base.subtests
183 echo "$1" >"$perf_results_dir"/$base.$test_count.descr
df0f5021 184 base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count"
968e77a5
JK
185 "$test_wrapper_func_" "$@"
186 fi
187
188 test_finish_
189}
190
191test_perf_ () {
192 if test -z "$verbose"; then
193 printf "%s" "perf $test_count - $1:"
194 else
195 echo "perf $test_count - $1:"
196 fi
197 for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
198 say >&3 "running: $2"
199 if test_run_perf_ "$2"
200 then
201 if test -z "$verbose"; then
202 printf " %s" "$i"
342e9ef2 203 else
968e77a5 204 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
342e9ef2 205 fi
342e9ef2 206 else
968e77a5
JK
207 test -z "$verbose" && echo
208 test_failure_ "$@"
209 break
342e9ef2 210 fi
968e77a5
JK
211 done
212 if test -z "$verbose"; then
213 echo " ok"
214 else
215 test_ok_ "$1"
342e9ef2 216 fi
b8dcc453 217 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result
968e77a5
JK
218}
219
220test_perf () {
221 test_wrapper_ test_perf_ "$@"
342e9ef2
TR
222}
223
22bec79d
JK
224test_size_ () {
225 say >&3 "running: $2"
b8dcc453 226 if test_eval_ "$2" 3>"$base".result; then
22bec79d
JK
227 test_ok_ "$1"
228 else
229 test_failure_ "$@"
230 fi
231}
232
233test_size () {
234 test_wrapper_ test_size_ "$@"
235}
236
342e9ef2
TR
237# We extend test_done to print timings at the end (./run disables this
238# and does it after running everything)
239test_at_end_hook_ () {
240 if test -z "$GIT_PERF_AGGREGATING_LATER"; then
241 ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") )
242 fi
243}
244
245test_export () {
246 export "$@"
247}
47274251
JK
248
249test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false'