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