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 | |
| 20 | # do the --tee work early; it otherwise confuses our careful |
| 21 | # GIT_BUILD_DIR mangling |
| 22 | case "$GIT_TEST_TEE_STARTED, $* " in |
| 23 | done,*) |
| 24 | # do not redirect again |
| 25 | ;; |
| 26 | *' --tee '*|*' --va'*) |
| 27 | mkdir -p test-results |
| 28 | BASE=test-results/$(basename "$0" .sh) |
| 29 | (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1; |
| 30 | echo $? > $BASE.exit) | tee $BASE.out |
| 31 | test "$(cat $BASE.exit)" = 0 |
| 32 | exit |
| 33 | ;; |
| 34 | esac |
| 35 | |
| 36 | TEST_DIRECTORY=$(pwd)/.. |
| 37 | TEST_OUTPUT_DIRECTORY=$(pwd) |
| 38 | if test -z "$GIT_TEST_INSTALLED"; then |
| 39 | perf_results_prefix= |
| 40 | else |
| 41 | perf_results_prefix=$(printf "%s" "${GIT_TEST_INSTALLED%/bin-wrappers}" | tr -c "[a-zA-Z0-9]" "[_*]")"." |
| 42 | # make the tested dir absolute |
| 43 | GIT_TEST_INSTALLED=$(cd "$GIT_TEST_INSTALLED" && pwd) |
| 44 | fi |
| 45 | |
| 46 | TEST_NO_CREATE_REPO=t |
René Scharfe | ee1431b | 2012-09-26 22:16:39 +0200 | [diff] [blame] | 47 | TEST_NO_MALLOC_CHECK=t |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 48 | |
| 49 | . ../test-lib.sh |
| 50 | |
Thomas Rast | 561ae06 | 2012-03-08 09:54:55 +0100 | [diff] [blame] | 51 | # Variables from test-lib that are normally internal to the tests; we |
| 52 | # need to export them for test_perf subshells |
| 53 | export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP |
| 54 | |
Jeff King | 1a0962d | 2016-06-22 15:40:13 -0400 | [diff] [blame] | 55 | MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git |
| 56 | export MODERN_GIT |
| 57 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 58 | perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results |
Christian Couder | 5d445f3 | 2017-09-23 19:55:56 +0000 | [diff] [blame^] | 59 | 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] | 60 | mkdir -p "$perf_results_dir" |
| 61 | rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests |
| 62 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 63 | die_if_build_dir_not_repo () { |
| 64 | if ! ( cd "$TEST_DIRECTORY/.." && |
| 65 | git rev-parse --build-dir >/dev/null 2>&1 ); then |
| 66 | error "No $1 defined, and your build directory is not a repo" |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | if test -z "$GIT_PERF_REPO"; then |
| 71 | die_if_build_dir_not_repo '$GIT_PERF_REPO' |
| 72 | GIT_PERF_REPO=$TEST_DIRECTORY/.. |
| 73 | fi |
| 74 | if test -z "$GIT_PERF_LARGE_REPO"; then |
| 75 | die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO' |
| 76 | GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/.. |
| 77 | fi |
| 78 | |
Ævar Arnfjörð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 79 | test_perf_do_repo_symlink_config_ () { |
| 80 | test_have_prereq SYMLINKS || git config core.symlinks false |
| 81 | } |
| 82 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 83 | test_perf_create_repo_from () { |
| 84 | test "$#" = 2 || |
| 85 | error "bug in the test script: not 2 parameters to test-create-repo" |
| 86 | repo="$1" |
| 87 | source="$2" |
Jeff King | 83d4a40 | 2017-03-03 02:14:03 -0500 | [diff] [blame] | 88 | source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)" |
Jeff King | 1a0962d | 2016-06-22 15:40:13 -0400 | [diff] [blame] | 89 | objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 90 | mkdir -p "$repo/.git" |
| 91 | ( |
René Scharfe | e2522f2 | 2016-05-29 18:43:41 +0200 | [diff] [blame] | 92 | cd "$source" && |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 93 | { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null || |
| 94 | cp -R "$objects_dir" "$repo/.git/"; } && |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 95 | for stuff in "$source_git"/*; do |
| 96 | case "$stuff" in |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 97 | */objects|*/hooks|*/config|*/commondir) |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 98 | ;; |
| 99 | *) |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 100 | cp -R "$stuff" "$repo/.git/" || exit 1 |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 101 | ;; |
| 102 | esac |
René Scharfe | e2522f2 | 2016-05-29 18:43:41 +0200 | [diff] [blame] | 103 | done |
| 104 | ) && |
| 105 | ( |
Johannes Schindelin | 7501b59 | 2016-05-13 15:25:58 +0200 | [diff] [blame] | 106 | cd "$repo" && |
Ævar Arnfjörð Bjarmason | 91de27c | 2017-05-11 09:41:07 +0000 | [diff] [blame] | 107 | "$MODERN_GIT" init -q && |
| 108 | test_perf_do_repo_symlink_config_ && |
Ævar Arnfjörð Bjarmason | 154ffee | 2017-06-02 10:33:30 +0000 | [diff] [blame] | 109 | mv .git/hooks .git/hooks-disabled 2>/dev/null && |
| 110 | if test -f .git/index.lock |
| 111 | then |
| 112 | # We may be copying a repo that can't run "git |
| 113 | # status" due to a locked index. Since we have |
| 114 | # a copy it's fine to remove the lock. |
| 115 | rm .git/index.lock |
| 116 | fi |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 117 | ) || error "failed to copy repository '$source' to '$repo'" |
| 118 | } |
| 119 | |
| 120 | # 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] | 121 | test_perf_fresh_repo () { |
| 122 | repo="${1:-$TRASH_DIRECTORY}" |
| 123 | "$MODERN_GIT" init -q "$repo" && |
| 124 | ( |
| 125 | cd "$repo" && |
| 126 | test_perf_do_repo_symlink_config_ |
| 127 | ) |
| 128 | } |
| 129 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 130 | test_perf_default_repo () { |
| 131 | test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO" |
| 132 | } |
| 133 | test_perf_large_repo () { |
| 134 | if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then |
| 135 | echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2 |
| 136 | echo "warning: This will work, but may not be a sufficiently large repo" >&2 |
| 137 | echo "warning: for representative measurements." >&2 |
| 138 | fi |
| 139 | test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO" |
| 140 | } |
| 141 | test_checkout_worktree () { |
| 142 | git checkout-index -u -a || |
| 143 | error "git checkout-index failed" |
| 144 | } |
| 145 | |
| 146 | # Performance tests should never fail. If they do, stop immediately |
| 147 | immediate=t |
| 148 | |
Johannes Schindelin | e3efa94 | 2016-06-21 15:53:43 +0200 | [diff] [blame] | 149 | # Perf tests require GNU time |
| 150 | case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac |
| 151 | GTIME="${GTIME:-/usr/bin/time}" |
| 152 | |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 153 | test_run_perf_ () { |
| 154 | test_cleanup=: |
| 155 | test_export_="test_cleanup" |
| 156 | export test_cleanup test_export_ |
Johannes Schindelin | e3efa94 | 2016-06-21 15:53:43 +0200 | [diff] [blame] | 157 | "$GTIME" -f "%E %U %S" -o test_time.$i "$SHELL" -c ' |
Thomas Rast | 1cbc324 | 2012-03-08 09:54:54 +0100 | [diff] [blame] | 158 | . '"$TEST_DIRECTORY"/test-lib-functions.sh' |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 159 | test_export () { |
| 160 | [ $# != 0 ] || return 0 |
| 161 | test_export_="$test_export_\\|$1" |
| 162 | shift |
| 163 | test_export "$@" |
| 164 | } |
| 165 | '"$1"' |
| 166 | ret=$? |
| 167 | set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars |
| 168 | exit $ret' >&3 2>&4 |
| 169 | eval_ret=$? |
| 170 | |
| 171 | if test $eval_ret = 0 || test -n "$expecting_failure" |
| 172 | then |
| 173 | test_eval_ "$test_cleanup" |
| 174 | . ./test_vars || error "failed to load updated environment" |
| 175 | fi |
| 176 | if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then |
| 177 | echo "" |
| 178 | fi |
| 179 | return "$eval_ret" |
| 180 | } |
| 181 | |
| 182 | |
| 183 | test_perf () { |
Thomas Gummerer | 62a23c9 | 2013-06-29 15:38:39 +0200 | [diff] [blame] | 184 | test_start_ |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 185 | test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= |
| 186 | test "$#" = 2 || |
| 187 | error "bug in the test script: not 2 or 3 parameters to test-expect-success" |
| 188 | export test_prereq |
| 189 | if ! test_skip "$@" |
| 190 | then |
| 191 | base=$(basename "$0" .sh) |
| 192 | echo "$test_count" >>"$perf_results_dir"/$base.subtests |
| 193 | echo "$1" >"$perf_results_dir"/$base.$test_count.descr |
| 194 | if test -z "$verbose"; then |
Lukas Fleischer | 19c3c5f | 2013-07-27 14:11:33 +0200 | [diff] [blame] | 195 | printf "%s" "perf $test_count - $1:" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 196 | else |
| 197 | echo "perf $test_count - $1:" |
| 198 | fi |
Michał Kiedrowicz | d17cf5f | 2012-08-04 00:21:04 +0200 | [diff] [blame] | 199 | for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 200 | say >&3 "running: $2" |
| 201 | if test_run_perf_ "$2" |
| 202 | then |
| 203 | if test -z "$verbose"; then |
Lukas Fleischer | 19c3c5f | 2013-07-27 14:11:33 +0200 | [diff] [blame] | 204 | printf " %s" "$i" |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 205 | else |
| 206 | echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:" |
| 207 | fi |
| 208 | else |
| 209 | test -z "$verbose" && echo |
| 210 | test_failure_ "$@" |
| 211 | break |
| 212 | fi |
| 213 | done |
| 214 | if test -z "$verbose"; then |
| 215 | echo " ok" |
| 216 | else |
| 217 | test_ok_ "$1" |
| 218 | fi |
| 219 | base="$perf_results_dir"/"$perf_results_prefix$(basename "$0" .sh)"."$test_count" |
| 220 | "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times |
| 221 | fi |
Thomas Gummerer | 62a23c9 | 2013-06-29 15:38:39 +0200 | [diff] [blame] | 222 | test_finish_ |
Thomas Rast | 342e9ef | 2012-02-17 11:25:09 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | # We extend test_done to print timings at the end (./run disables this |
| 226 | # and does it after running everything) |
| 227 | test_at_end_hook_ () { |
| 228 | if test -z "$GIT_PERF_AGGREGATING_LATER"; then |
| 229 | ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") ) |
| 230 | fi |
| 231 | } |
| 232 | |
| 233 | test_export () { |
| 234 | export "$@" |
| 235 | } |