blob: e7786775a9016151e063335047256bfea93d2f63 [file] [log] [blame]
Jonathan Niederc74c7202013-11-25 13:03:06 -08001# 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.
Thomas Rast342e9ef2012-02-17 11:25:09 +01004#
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
Jeff King0baf78e2019-03-15 02:14:17 -040020# These variables must be set before the inclusion of test-lib.sh below,
21# because it will change our working directory.
Thomas Rast342e9ef2012-02-17 11:25:09 +010022TEST_DIRECTORY=$(pwd)/..
23TEST_OUTPUT_DIRECTORY=$(pwd)
Thomas Rast342e9ef2012-02-17 11:25:09 +010024
25TEST_NO_CREATE_REPO=t
René Scharfeee1431b2012-09-26 22:16:39 +020026TEST_NO_MALLOC_CHECK=t
Thomas Rast342e9ef2012-02-17 11:25:09 +010027
28. ../test-lib.sh
29
René Scharfebe791312021-10-09 16:39:24 +020030unset GIT_CONFIG_NOSYSTEM
31GIT_CONFIG_SYSTEM="$TEST_DIRECTORY/perf/config"
32export GIT_CONFIG_SYSTEM
33
Ævar Arnfjörð Bjarmason82b7eb22019-05-07 12:54:34 +020034if 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
Thomas Rast561ae062012-03-08 09:54:55 +010045# 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
Jeff King1a0962d2016-06-22 15:40:13 -040049MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
50export MODERN_GIT
51
Victoria Dyeba1b1172022-09-02 15:56:49 +000052MODERN_SCALAR=$GIT_BUILD_DIR/bin-wrappers/scalar
53export MODERN_SCALAR
54
Patrick Steinhardt3663e592021-06-18 15:56:08 +020055perf_results_dir=$TEST_RESULTS_DIR
Christian Couder5d445f32017-09-23 19:55:56 +000056test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
Thomas Rast342e9ef2012-02-17 11:25:09 +010057mkdir -p "$perf_results_dir"
58rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
59
Thomas Rast342e9ef2012-02-17 11:25:09 +010060die_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
Ævar Arnfjörð Bjarmason91de27c2017-05-11 09:41:07 +000076test_perf_do_repo_symlink_config_ () {
77 test_have_prereq SYMLINKS || git config core.symlinks false
78}
79
Jeff King85b87a52021-02-26 02:11:39 -050080test_perf_copy_repo_contents () {
81 for stuff in "$1"/*
82 do
83 case "$stuff" in
Jeff Hostetler08894d32022-03-25 18:03:05 +000084 */objects|*/hooks|*/config|*/commondir|*/gitdir|*/worktrees|*/fsmonitor--daemon*)
Jeff King85b87a52021-02-26 02:11:39 -050085 ;;
86 *)
87 cp -R "$stuff" "$repo/.git/" || exit 1
88 ;;
89 esac
90 done
91}
92
Thomas Rast342e9ef2012-02-17 11:25:09 +010093test_perf_create_repo_from () {
94 test "$#" = 2 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +010095 BUG "not 2 parameters to test-create-repo"
Thomas Rast342e9ef2012-02-17 11:25:09 +010096 repo="$1"
97 source="$2"
Jeff King83d4a402017-03-03 02:14:03 -050098 source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
Jeff King1a0962d2016-06-22 15:40:13 -040099 objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
Jeff King85b87a52021-02-26 02:11:39 -0500100 common_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-common-dir)"
Thomas Rast342e9ef2012-02-17 11:25:09 +0100101 mkdir -p "$repo/.git"
102 (
René Scharfee2522f22016-05-29 18:43:41 +0200103 cd "$source" &&
Johannes Schindelin7501b592016-05-13 15:25:58 +0200104 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
105 cp -R "$objects_dir" "$repo/.git/"; } &&
Jeff King85b87a52021-02-26 02:11:39 -0500106
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
René Scharfee2522f22016-05-29 18:43:41 +0200114 ) &&
115 (
Johannes Schindelin7501b592016-05-13 15:25:58 +0200116 cd "$repo" &&
Ævar Arnfjörð Bjarmason91de27c2017-05-11 09:41:07 +0000117 "$MODERN_GIT" init -q &&
118 test_perf_do_repo_symlink_config_ &&
Ævar Arnfjörð Bjarmason154ffee2017-06-02 10:33:30 +0000119 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
Victoria Dyeba1b1172022-09-02 15:56:49 +0000126 fi &&
127 if test_bool_env GIT_PERF_USE_SCALAR false
128 then
129 "$MODERN_SCALAR" register
Ævar Arnfjörð Bjarmason154ffee2017-06-02 10:33:30 +0000130 fi
Thomas Rast342e9ef2012-02-17 11:25:09 +0100131 ) || error "failed to copy repository '$source' to '$repo'"
132}
133
134# call at least one of these to establish an appropriately-sized repository
Ævar Arnfjörð Bjarmason91de27c2017-05-11 09:41:07 +0000135test_perf_fresh_repo () {
136 repo="${1:-$TRASH_DIRECTORY}"
137 "$MODERN_GIT" init -q "$repo" &&
138 (
139 cd "$repo" &&
Victoria Dyeba1b1172022-09-02 15:56:49 +0000140 test_perf_do_repo_symlink_config_ &&
141 if test_bool_env GIT_PERF_USE_SCALAR false
142 then
143 "$MODERN_SCALAR" register
144 fi
Ævar Arnfjörð Bjarmason91de27c2017-05-11 09:41:07 +0000145 )
146}
147
Thomas Rast342e9ef2012-02-17 11:25:09 +0100148test_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
Johannes Schindeline3efa942016-06-21 15:53:43 +0200167# Perf tests require GNU time
168case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
169GTIME="${GTIME:-/usr/bin/time}"
170
Thomas Rast342e9ef2012-02-17 11:25:09 +0100171test_run_perf_ () {
172 test_cleanup=:
173 test_export_="test_cleanup"
174 export test_cleanup test_export_
Johannes Altmanninger9ccab752021-12-25 09:16:58 +0100175 "$GTIME" -f "%E %U %S" -o test_time.$i "$TEST_SHELL_PATH" -c '
Thomas Rast1cbc3242012-03-08 09:54:54 +0100176. '"$TEST_DIRECTORY"/test-lib-functions.sh'
Thomas Rast342e9ef2012-02-17 11:25:09 +0100177test_export () {
Eric Sunshine5bc12c12020-12-20 16:27:40 -0500178 test_export_="$test_export_ $*"
Thomas Rast342e9ef2012-02-17 11:25:09 +0100179}
180'"$1"'
181ret=$?
Eric Sunshinef4698732020-12-16 02:39:07 -0500182needles=
183for v in $test_export_
184do
185 needles="$needles;s/^$v=/export $v=/p"
186done
187set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars
Thomas Rast342e9ef2012-02-17 11:25:09 +0100188exit $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
Jeff King968e77a2018-08-17 16:55:06 -0400202test_wrapper_ () {
Neeraj Singh5dccd912022-04-04 22:20:17 -0700203 local test_wrapper_func_="$1"; shift
204 local test_title_="$1"; shift
Thomas Gummerer62a23c92013-06-29 15:38:39 +0200205 test_start_
Neeraj Singh5dccd912022-04-04 22:20:17 -0700206 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"
Thomas Rast342e9ef2012-02-17 11:25:09 +0100226 export test_prereq
Neeraj Singh5dccd912022-04-04 22:20:17 -0700227 export test_perf_setup_
228
229 if ! test_skip "$test_title_" "$@"
Thomas Rast342e9ef2012-02-17 11:25:09 +0100230 then
231 base=$(basename "$0" .sh)
232 echo "$test_count" >>"$perf_results_dir"/$base.subtests
Jeff King55d9d4b2022-06-16 03:09:32 -0400233 echo "$test_title_" >"$perf_results_dir"/$base.$test_count.descr
Ævar Arnfjörð Bjarmasondf0f5022019-05-07 12:54:32 +0200234 base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count"
Neeraj Singh5dccd912022-04-04 22:20:17 -0700235 "$test_wrapper_func_" "$test_title_" "$@"
Thomas Rast342e9ef2012-02-17 11:25:09 +0100236 fi
Jeff King968e77a2018-08-17 16:55:06 -0400237
Thomas Gummerer62a23c92013-06-29 15:38:39 +0200238 test_finish_
Thomas Rast342e9ef2012-02-17 11:25:09 +0100239}
240
Jeff King968e77a2018-08-17 16:55:06 -0400241test_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
Neeraj Singh5dccd912022-04-04 22:20:17 -0700248 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
Jeff King968e77a2018-08-17 16:55:06 -0400258 say >&3 "running: $2"
259 if test_run_perf_ "$2"
260 then
261 if test -z "$verbose"; then
262 printf " %s" "$i"
263 else
264 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
265 fi
266 else
267 test -z "$verbose" && echo
268 test_failure_ "$@"
269 break
270 fi
271 done
272 if test -z "$verbose"; then
273 echo " ok"
274 else
275 test_ok_ "$1"
276 fi
Jeff Kingb8dcc452019-11-25 09:09:25 -0500277 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result
Jeff Hostetlerb9e4d842021-10-04 22:29:03 +0000278 rm test_time.*
Jeff King968e77a2018-08-17 16:55:06 -0400279}
280
Neeraj Singh5dccd912022-04-04 22:20:17 -0700281# 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#
Jeff King968e77a2018-08-17 16:55:06 -0400288test_perf () {
289 test_wrapper_ test_perf_ "$@"
290}
291
Jeff King22bec792018-08-17 16:56:37 -0400292test_size_ () {
Neeraj Singh5dccd912022-04-04 22:20:17 -0700293 if test -n "$test_perf_setup_"
294 then
295 say >&3 "setup: $test_perf_setup_"
296 test_eval_ $test_perf_setup_
297 fi
298
Jeff King22bec792018-08-17 16:56:37 -0400299 say >&3 "running: $2"
Jeff Kingb8dcc452019-11-25 09:09:25 -0500300 if test_eval_ "$2" 3>"$base".result; then
Jeff King22bec792018-08-17 16:56:37 -0400301 test_ok_ "$1"
302 else
303 test_failure_ "$@"
304 fi
305}
306
Neeraj Singh5dccd912022-04-04 22:20:17 -0700307# 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
Jeff King22bec792018-08-17 16:56:37 -0400315test_size () {
316 test_wrapper_ test_size_ "$@"
317}
318
Thomas Rast342e9ef2012-02-17 11:25:09 +0100319# 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
Patrick Steinhardt3663e592021-06-18 15:56:08 +0200323 (
324 cd "$TEST_DIRECTORY"/perf &&
325 ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $(basename "$0")
326 )
Thomas Rast342e9ef2012-02-17 11:25:09 +0100327 fi
328}
329
330test_export () {
331 export "$@"
332}
Jeff King47274252020-08-21 13:53:39 -0400333
334test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false'