Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 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. |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 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 | |
Jeff King | 0baf78e | 2019-03-15 02:14:17 -0400 | [diff] [blame] | 20 | # These variables must be set before the inclusion of test-lib.sh below, |
| 21 | # because it will change our working directory. |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 22 | TEST_DIRECTORY=$(pwd)/.. |
| 23 | TEST_OUTPUT_DIRECTORY=$(pwd) |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 24 | |
| 25 | TEST_NO_CREATE_REPO=t |
René Scharfe | ee1431b | 2012-09-26 22:16:39 +0200 | [diff] [blame] | 26 | TEST_NO_MALLOC_CHECK=t |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 27 | |
| 28 | . ../test-lib.sh |
| 29 | |
René Scharfe | be79131 | 2021-10-09 16:39:24 +0200 | [diff] [blame] | 30 | unset GIT_CONFIG_NOSYSTEM |
| 31 | GIT_CONFIG_SYSTEM="$TEST_DIRECTORY/perf/config" |
| 32 | export GIT_CONFIG_SYSTEM |
| 33 | |
Ævar Arnfjörð Bjarmason | 82b7eb2 | 2019-05-07 12:54:34 +0200 | [diff] [blame] | 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 | |
Thomas Rast | 561ae06 | 2012-03-08 09:54:55 +0100 | [diff] [blame] | 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 | |
Jeff King | 1a0962d | 2016-06-22 15:40:13 -0400 | [diff] [blame] | 49 | MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git |
| 50 | export MODERN_GIT |
| 51 | |
Victoria Dye | ba1b117 | 2022-09-02 15:56:49 +0000 | [diff] [blame] | 52 | MODERN_SCALAR=$GIT_BUILD_DIR/bin-wrappers/scalar |
| 53 | export MODERN_SCALAR |
| 54 | |
Patrick Steinhardt | 3663e59 | 2021-06-18 15:56:08 +0200 | [diff] [blame] | 55 | perf_results_dir=$TEST_RESULTS_DIR |
Christian Couder | 5d445f3 | 2017-09-23 19:55:56 +0000 | [diff] [blame] | 56 | test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 57 | mkdir -p "$perf_results_dir" |
| 58 | rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests |
| 59 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 60 | die_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 | |
| 67 | if test -z "$GIT_PERF_REPO"; then |
| 68 | die_if_build_dir_not_repo '$GIT_PERF_REPO' |
| 69 | GIT_PERF_REPO=$TEST_DIRECTORY/.. |
| 70 | fi |
| 71 | if 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/.. |
| 74 | fi |
| 75 | |
Ævar Arnfjörð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 76 | test_perf_do_repo_symlink_config_ () { |
| 77 | test_have_prereq SYMLINKS || git config core.symlinks false |
| 78 | } |
| 79 | |
Jeff King | 85b87a5 | 2021-02-26 02:11:39 -0500 | [diff] [blame] | 80 | test_perf_copy_repo_contents () { |
| 81 | for stuff in "$1"/* |
| 82 | do |
| 83 | case "$stuff" in |
Jeff Hostetler | 08894d3 | 2022-03-25 18:03:05 +0000 | [diff] [blame] | 84 | */objects|*/hooks|*/config|*/commondir|*/gitdir|*/worktrees|*/fsmonitor--daemon*) |
Jeff King | 85b87a5 | 2021-02-26 02:11:39 -0500 | [diff] [blame] | 85 | ;; |
| 86 | *) |
| 87 | cp -R "$stuff" "$repo/.git/" || exit 1 |
| 88 | ;; |
| 89 | esac |
| 90 | done |
| 91 | } |
| 92 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 93 | test_perf_create_repo_from () { |
| 94 | test "$#" = 2 || |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 95 | BUG "not 2 parameters to test-create-repo" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 96 | repo="$1" |
| 97 | source="$2" |
Jeff King | 83d4a40 | 2017-03-03 02:14:03 -0500 | [diff] [blame] | 98 | source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)" |
Jeff King | 1a0962d | 2016-06-22 15:40:13 -0400 | [diff] [blame] | 99 | objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)" |
Jeff King | 85b87a5 | 2021-02-26 02:11:39 -0500 | [diff] [blame] | 100 | common_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-common-dir)" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 101 | mkdir -p "$repo/.git" |
| 102 | ( |
René Scharfe | e2522f2 | 2016-05-29 18:43:41 +0200 | [diff] [blame] | 103 | cd "$source" && |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 104 | { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null || |
| 105 | cp -R "$objects_dir" "$repo/.git/"; } && |
Jeff King | 85b87a5 | 2021-02-26 02:11:39 -0500 | [diff] [blame] | 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 |
René Scharfe | e2522f2 | 2016-05-29 18:43:41 +0200 | [diff] [blame] | 114 | ) && |
| 115 | ( |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 116 | cd "$repo" && |
Ævar Arnfjörð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 117 | "$MODERN_GIT" init -q && |
| 118 | test_perf_do_repo_symlink_config_ && |
Ævar Arnfjörð Bjarmason | 154ffee | 2017-06-02 10:33:30 +0000 | [diff] [blame] | 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 |
Victoria Dye | ba1b117 | 2022-09-02 15:56:49 +0000 | [diff] [blame] | 126 | fi && |
| 127 | if test_bool_env GIT_PERF_USE_SCALAR false |
| 128 | then |
| 129 | "$MODERN_SCALAR" register |
Ævar Arnfjörð Bjarmason | 154ffee | 2017-06-02 10:33:30 +0000 | [diff] [blame] | 130 | fi |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 131 | ) || 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ð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 135 | test_perf_fresh_repo () { |
| 136 | repo="${1:-$TRASH_DIRECTORY}" |
| 137 | "$MODERN_GIT" init -q "$repo" && |
| 138 | ( |
| 139 | cd "$repo" && |
Victoria Dye | ba1b117 | 2022-09-02 15:56:49 +0000 | [diff] [blame] | 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 |
Ævar Arnfjörð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 145 | ) |
| 146 | } |
| 147 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 148 | test_perf_default_repo () { |
| 149 | test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO" |
| 150 | } |
| 151 | test_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 | } |
| 159 | test_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 |
| 165 | immediate=t |
| 166 | |
Johannes Schindelin | e3efa94 | 2016-06-21 15:53:43 +0200 | [diff] [blame] | 167 | # Perf tests require GNU time |
| 168 | case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac |
| 169 | GTIME="${GTIME:-/usr/bin/time}" |
| 170 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 171 | test_run_perf_ () { |
| 172 | test_cleanup=: |
| 173 | test_export_="test_cleanup" |
| 174 | export test_cleanup test_export_ |
Johannes Altmanninger | 9ccab75 | 2021-12-25 09:16:58 +0100 | [diff] [blame] | 175 | "$GTIME" -f "%E %U %S" -o test_time.$i "$TEST_SHELL_PATH" -c ' |
Thomas Rast | 1cbc324 | 2012-03-08 09:54:54 +0100 | [diff] [blame] | 176 | . '"$TEST_DIRECTORY"/test-lib-functions.sh' |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 177 | test_export () { |
Eric Sunshine | 5bc12c1 | 2020-12-20 16:27:40 -0500 | [diff] [blame] | 178 | test_export_="$test_export_ $*" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 179 | } |
| 180 | '"$1"' |
| 181 | ret=$? |
Eric Sunshine | f469873 | 2020-12-16 02:39:07 -0500 | [diff] [blame] | 182 | needles= |
| 183 | for v in $test_export_ |
| 184 | do |
| 185 | needles="$needles;s/^$v=/export $v=/p" |
| 186 | done |
| 187 | set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 188 | exit $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 King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 202 | test_wrapper_ () { |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 203 | local test_wrapper_func_="$1"; shift |
| 204 | local test_title_="$1"; shift |
Thomas Gummerer | 62a23c9 | 2013-06-29 15:38:39 +0200 | [diff] [blame] | 205 | test_start_ |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 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" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 226 | export test_prereq |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 227 | export test_perf_setup_ |
| 228 | |
| 229 | if ! test_skip "$test_title_" "$@" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 230 | then |
| 231 | base=$(basename "$0" .sh) |
| 232 | echo "$test_count" >>"$perf_results_dir"/$base.subtests |
Jeff King | 55d9d4b | 2022-06-16 03:09:32 -0400 | [diff] [blame] | 233 | echo "$test_title_" >"$perf_results_dir"/$base.$test_count.descr |
Ævar Arnfjörð Bjarmason | df0f502 | 2019-05-07 12:54:32 +0200 | [diff] [blame] | 234 | base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count" |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 235 | "$test_wrapper_func_" "$test_title_" "$@" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 236 | fi |
Jeff King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 237 | |
Thomas Gummerer | 62a23c9 | 2013-06-29 15:38:39 +0200 | [diff] [blame] | 238 | test_finish_ |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Jeff King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 241 | test_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 Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 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 |
Jeff King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 258 | 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 King | b8dcc45 | 2019-11-25 09:09:25 -0500 | [diff] [blame] | 277 | "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result |
Jeff Hostetler | b9e4d84 | 2021-10-04 22:29:03 +0000 | [diff] [blame] | 278 | rm test_time.* |
Jeff King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 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 | # |
Jeff King | 968e77a | 2018-08-17 16:55:06 -0400 | [diff] [blame] | 288 | test_perf () { |
| 289 | test_wrapper_ test_perf_ "$@" |
| 290 | } |
| 291 | |
Jeff King | 22bec79 | 2018-08-17 16:56:37 -0400 | [diff] [blame] | 292 | test_size_ () { |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 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 | |
Jeff King | 22bec79 | 2018-08-17 16:56:37 -0400 | [diff] [blame] | 299 | say >&3 "running: $2" |
Jeff King | b8dcc45 | 2019-11-25 09:09:25 -0500 | [diff] [blame] | 300 | if test_eval_ "$2" 3>"$base".result; then |
Jeff King | 22bec79 | 2018-08-17 16:56:37 -0400 | [diff] [blame] | 301 | test_ok_ "$1" |
| 302 | else |
| 303 | test_failure_ "$@" |
| 304 | fi |
| 305 | } |
| 306 | |
Neeraj Singh | 5dccd91 | 2022-04-04 22:20:17 -0700 | [diff] [blame] | 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 | |
Jeff King | 22bec79 | 2018-08-17 16:56:37 -0400 | [diff] [blame] | 315 | test_size () { |
| 316 | test_wrapper_ test_size_ "$@" |
| 317 | } |
| 318 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 319 | # We extend test_done to print timings at the end (./run disables this |
| 320 | # and does it after running everything) |
| 321 | test_at_end_hook_ () { |
| 322 | if test -z "$GIT_PERF_AGGREGATING_LATER"; then |
Patrick Steinhardt | 3663e59 | 2021-06-18 15:56:08 +0200 | [diff] [blame] | 323 | ( |
| 324 | cd "$TEST_DIRECTORY"/perf && |
| 325 | ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $(basename "$0") |
| 326 | ) |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 327 | fi |
| 328 | } |
| 329 | |
| 330 | test_export () { |
| 331 | export "$@" |
| 332 | } |
Jeff King | 4727425 | 2020-08-21 13:53:39 -0400 | [diff] [blame] | 333 | |
| 334 | test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false' |