]> git.ipfire.org Git - thirdparty/git.git/blame - t/perf/perf-lib.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[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
be79131a
RS
30unset GIT_CONFIG_NOSYSTEM
31GIT_CONFIG_SYSTEM="$TEST_DIRECTORY/perf/config"
32export GIT_CONFIG_SYSTEM
33
82b7eb23
ÆAB
34if test -n "$GIT_TEST_INSTALLED" -a -z "$PERF_SET_GIT_TEST_INSTALLED"
35then
36 error "Do not use GIT_TEST_INSTALLED with the perf tests.
37
38Instead use:
39
40 ./run <path-to-git> -- <tests>
41
42See t/perf/README for details."
43fi
44
561ae067
TR
45# Variables from test-lib that are normally internal to the tests; we
46# need to export them for test_perf subshells
47export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
48
1a0962de
JK
49MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
50export MODERN_GIT
51
ba1b117e
VD
52MODERN_SCALAR=$GIT_BUILD_DIR/bin-wrappers/scalar
53export MODERN_SCALAR
54
3663e590 55perf_results_dir=$TEST_RESULTS_DIR
5d445f34 56test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
342e9ef2
TR
57mkdir -p "$perf_results_dir"
58rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
59
342e9ef2
TR
60die_if_build_dir_not_repo () {
61 if ! ( cd "$TEST_DIRECTORY/.." &&
62 git rev-parse --build-dir >/dev/null 2>&1 ); then
63 error "No $1 defined, and your build directory is not a repo"
64 fi
65}
66
67if test -z "$GIT_PERF_REPO"; then
68 die_if_build_dir_not_repo '$GIT_PERF_REPO'
69 GIT_PERF_REPO=$TEST_DIRECTORY/..
70fi
71if test -z "$GIT_PERF_LARGE_REPO"; then
72 die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
73 GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
74fi
75
91de27c5
ÆAB
76test_perf_do_repo_symlink_config_ () {
77 test_have_prereq SYMLINKS || git config core.symlinks false
78}
79
85b87a53
JK
80test_perf_copy_repo_contents () {
81 for stuff in "$1"/*
82 do
83 case "$stuff" in
08894d33 84 */objects|*/hooks|*/config|*/commondir|*/gitdir|*/worktrees|*/fsmonitor--daemon*)
85b87a53
JK
85 ;;
86 *)
87 cp -R "$stuff" "$repo/.git/" || exit 1
88 ;;
89 esac
90 done
91}
92
342e9ef2
TR
93test_perf_create_repo_from () {
94 test "$#" = 2 ||
165293af 95 BUG "not 2 parameters to test-create-repo"
342e9ef2
TR
96 repo="$1"
97 source="$2"
83d4a409 98 source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
1a0962de 99 objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
85b87a53 100 common_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-common-dir)"
342e9ef2
TR
101 mkdir -p "$repo/.git"
102 (
e2522f2a 103 cd "$source" &&
7501b592
JS
104 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
105 cp -R "$objects_dir" "$repo/.git/"; } &&
85b87a53
JK
106
107 # common_dir must come first here, since we want source_git to
108 # take precedence and overwrite any overlapping files
109 test_perf_copy_repo_contents "$common_dir"
110 if test "$source_git" != "$common_dir"
111 then
112 test_perf_copy_repo_contents "$source_git"
113 fi
e2522f2a
RS
114 ) &&
115 (
7501b592 116 cd "$repo" &&
91de27c5
ÆAB
117 "$MODERN_GIT" init -q &&
118 test_perf_do_repo_symlink_config_ &&
154ffeec
ÆAB
119 mv .git/hooks .git/hooks-disabled 2>/dev/null &&
120 if test -f .git/index.lock
121 then
122 # We may be copying a repo that can't run "git
123 # status" due to a locked index. Since we have
124 # a copy it's fine to remove the lock.
125 rm .git/index.lock
ba1b117e
VD
126 fi &&
127 if test_bool_env GIT_PERF_USE_SCALAR false
128 then
129 "$MODERN_SCALAR" register
154ffeec 130 fi
342e9ef2
TR
131 ) || error "failed to copy repository '$source' to '$repo'"
132}
133
134# call at least one of these to establish an appropriately-sized repository
91de27c5
ÆAB
135test_perf_fresh_repo () {
136 repo="${1:-$TRASH_DIRECTORY}"
137 "$MODERN_GIT" init -q "$repo" &&
138 (
139 cd "$repo" &&
ba1b117e
VD
140 test_perf_do_repo_symlink_config_ &&
141 if test_bool_env GIT_PERF_USE_SCALAR false
142 then
143 "$MODERN_SCALAR" register
144 fi
91de27c5
ÆAB
145 )
146}
147
342e9ef2
TR
148test_perf_default_repo () {
149 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
150}
151test_perf_large_repo () {
152 if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
153 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
154 echo "warning: This will work, but may not be a sufficiently large repo" >&2
155 echo "warning: for representative measurements." >&2
156 fi
157 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
158}
159test_checkout_worktree () {
160 git checkout-index -u -a ||
161 error "git checkout-index failed"
162}
163
164# Performance tests should never fail. If they do, stop immediately
165immediate=t
166
e3efa94b
JS
167# Perf tests require GNU time
168case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
169GTIME="${GTIME:-/usr/bin/time}"
170
342e9ef2
TR
171test_run_perf_ () {
172 test_cleanup=:
173 test_export_="test_cleanup"
174 export test_cleanup test_export_
9ccab756 175 "$GTIME" -f "%E %U %S" -o test_time.$i "$TEST_SHELL_PATH" -c '
1cbc3240 176. '"$TEST_DIRECTORY"/test-lib-functions.sh'
342e9ef2 177test_export () {
5bc12c11 178 test_export_="$test_export_ $*"
342e9ef2
TR
179}
180'"$1"'
181ret=$?
f4698738
ES
182needles=
183for v in $test_export_
184do
185 needles="$needles;s/^$v=/export $v=/p"
186done
187set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars
342e9ef2
TR
188exit $ret' >&3 2>&4
189 eval_ret=$?
190
191 if test $eval_ret = 0 || test -n "$expecting_failure"
192 then
193 test_eval_ "$test_cleanup"
194 . ./test_vars || error "failed to load updated environment"
195 fi
196 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
197 echo ""
198 fi
199 return "$eval_ret"
200}
201
968e77a5 202test_wrapper_ () {
5dccd915
NS
203 local test_wrapper_func_="$1"; shift
204 local test_title_="$1"; shift
62a23c9f 205 test_start_
5dccd915
NS
206 test_prereq=
207 test_perf_setup_=
208 while test $# != 0
209 do
210 case $1 in
211 --prereq)
212 test_prereq=$2
213 shift
214 ;;
215 --setup)
216 test_perf_setup_=$2
217 shift
218 ;;
219 *)
220 break
221 ;;
222 esac
223 shift
224 done
225 test "$#" = 1 || BUG "test_wrapper_ needs 2 positional parameters"
342e9ef2 226 export test_prereq
5dccd915
NS
227 export test_perf_setup_
228
229 if ! test_skip "$test_title_" "$@"
342e9ef2
TR
230 then
231 base=$(basename "$0" .sh)
232 echo "$test_count" >>"$perf_results_dir"/$base.subtests
55d9d4bb 233 echo "$test_title_" >"$perf_results_dir"/$base.$test_count.descr
df0f5021 234 base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count"
5dccd915 235 "$test_wrapper_func_" "$test_title_" "$@"
968e77a5
JK
236 fi
237
238 test_finish_
239}
240
241test_perf_ () {
242 if test -z "$verbose"; then
243 printf "%s" "perf $test_count - $1:"
244 else
245 echo "perf $test_count - $1:"
246 fi
247 for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
5dccd915
NS
248 if test -n "$test_perf_setup_"
249 then
250 say >&3 "setup: $test_perf_setup_"
251 if ! test_eval_ $test_perf_setup_
252 then
253 test_failure_ "$test_perf_setup_"
254 break
255 fi
256
257 fi
968e77a5
JK
258 say >&3 "running: $2"
259 if test_run_perf_ "$2"
260 then
261 if test -z "$verbose"; then
262 printf " %s" "$i"
342e9ef2 263 else
968e77a5 264 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
342e9ef2 265 fi
342e9ef2 266 else
968e77a5
JK
267 test -z "$verbose" && echo
268 test_failure_ "$@"
269 break
342e9ef2 270 fi
968e77a5
JK
271 done
272 if test -z "$verbose"; then
273 echo " ok"
274 else
275 test_ok_ "$1"
342e9ef2 276 fi
b8dcc453 277 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result
b9e4d848 278 rm test_time.*
968e77a5
JK
279}
280
5dccd915
NS
281# Usage: test_perf 'title' [options] 'perf-test'
282# Run the performance test script specified in perf-test with
283# optional prerequisite and setup steps.
284# Options:
285# --prereq prerequisites: Skip the test if prequisites aren't met
286# --setup "setup-steps": Run setup steps prior to each measured iteration
287#
968e77a5
JK
288test_perf () {
289 test_wrapper_ test_perf_ "$@"
342e9ef2
TR
290}
291
22bec79d 292test_size_ () {
5dccd915
NS
293 if test -n "$test_perf_setup_"
294 then
295 say >&3 "setup: $test_perf_setup_"
296 test_eval_ $test_perf_setup_
297 fi
298
22bec79d 299 say >&3 "running: $2"
b8dcc453 300 if test_eval_ "$2" 3>"$base".result; then
22bec79d
JK
301 test_ok_ "$1"
302 else
303 test_failure_ "$@"
304 fi
305}
306
5dccd915
NS
307# Usage: test_size 'title' [options] 'size-test'
308# Run the size test script specified in size-test with optional
309# prerequisites and setup steps. Returns the numeric value
310# returned by size-test.
311# Options:
312# --prereq prerequisites: Skip the test if prequisites aren't met
313# --setup "setup-steps": Run setup steps prior to the size measurement
314
22bec79d
JK
315test_size () {
316 test_wrapper_ test_size_ "$@"
317}
318
342e9ef2
TR
319# We extend test_done to print timings at the end (./run disables this
320# and does it after running everything)
321test_at_end_hook_ () {
322 if test -z "$GIT_PERF_AGGREGATING_LATER"; then
3663e590
PS
323 (
324 cd "$TEST_DIRECTORY"/perf &&
325 ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $(basename "$0")
326 )
342e9ef2
TR
327 fi
328}
329
330test_export () {
331 export "$@"
332}
47274251
JK
333
334test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false'