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