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. 22TEST_DIRECTORY=$(pwd)/.. 23TEST_OUTPUT_DIRECTORY=$(pwd) 24 25TEST_NO_CREATE_REPO=t 26TEST_NO_MALLOC_CHECK=t 27 28. ../test-lib.sh 29 30# Variables from test-lib that are normally internal to the tests; we 31# need to export them for test_perf subshells 32export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP 33 34MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git 35export MODERN_GIT 36 37perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results 38test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION" 39mkdir -p "$perf_results_dir" 40rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests 41 42die_if_build_dir_not_repo () { 43 if ! ( cd "$TEST_DIRECTORY/.." && 44 git rev-parse --build-dir >/dev/null 2>&1 ); then 45 error "No $1 defined, and your build directory is not a repo" 46 fi 47} 48 49if test -z "$GIT_PERF_REPO"; then 50 die_if_build_dir_not_repo '$GIT_PERF_REPO' 51 GIT_PERF_REPO=$TEST_DIRECTORY/.. 52fi 53if test -z "$GIT_PERF_LARGE_REPO"; then 54 die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO' 55 GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/.. 56fi 57 58test_perf_do_repo_symlink_config_ () { 59 test_have_prereq SYMLINKS || git config core.symlinks false 60} 61 62test_perf_create_repo_from () { 63 test "$#" = 2 || 64 BUG "not 2 parameters to test-create-repo" 65 repo="$1" 66 source="$2" 67 source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)" 68 objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)" 69 mkdir -p "$repo/.git" 70 ( 71 cd "$source" && 72 { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null || 73 cp -R "$objects_dir" "$repo/.git/"; } && 74 for stuff in "$source_git"/*; do 75 case "$stuff" in 76 */objects|*/hooks|*/config|*/commondir) 77 ;; 78 *) 79 cp -R "$stuff" "$repo/.git/" || exit 1 80 ;; 81 esac 82 done 83 ) && 84 ( 85 cd "$repo" && 86 "$MODERN_GIT" init -q && 87 test_perf_do_repo_symlink_config_ && 88 mv .git/hooks .git/hooks-disabled 2>/dev/null && 89 if test -f .git/index.lock 90 then 91 # We may be copying a repo that can't run "git 92 # status" due to a locked index. Since we have 93 # a copy it's fine to remove the lock. 94 rm .git/index.lock 95 fi 96 ) || error "failed to copy repository '$source' to '$repo'" 97} 98 99# call at least one of these to establish an appropriately-sized repository 100test_perf_fresh_repo () { 101 repo="${1:-$TRASH_DIRECTORY}" 102 "$MODERN_GIT" init -q "$repo" && 103 ( 104 cd "$repo" && 105 test_perf_do_repo_symlink_config_ 106 ) 107} 108 109test_perf_default_repo () { 110 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO" 111} 112test_perf_large_repo () { 113 if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then 114 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2 115 echo "warning: This will work, but may not be a sufficiently large repo" >&2 116 echo "warning: for representative measurements." >&2 117 fi 118 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO" 119} 120test_checkout_worktree () { 121 git checkout-index -u -a || 122 error "git checkout-index failed" 123} 124 125# Performance tests should never fail. If they do, stop immediately 126immediate=t 127 128# Perf tests require GNU time 129case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac 130GTIME="${GTIME:-/usr/bin/time}" 131 132test_run_perf_ () { 133 test_cleanup=: 134 test_export_="test_cleanup" 135 export test_cleanup test_export_ 136 "$GTIME" -f "%E %U %S" -o test_time.$i "$SHELL" -c ' 137. '"$TEST_DIRECTORY"/test-lib-functions.sh' 138test_export () { 139 [ $# != 0 ] || return 0 140 test_export_="$test_export_\\|$1" 141 shift 142 test_export "$@" 143} 144'"$1"' 145ret=$? 146set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars 147exit $ret' >&3 2>&4 148 eval_ret=$? 149 150 if test $eval_ret = 0 || test -n "$expecting_failure" 151 then 152 test_eval_ "$test_cleanup" 153 . ./test_vars || error "failed to load updated environment" 154 fi 155 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then 156 echo "" 157 fi 158 return "$eval_ret" 159} 160 161test_wrapper_ () { 162 test_wrapper_func_=$1; shift 163 test_start_ 164 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= 165 test "$#" = 2 || 166 BUG "not 2 or 3 parameters to test-expect-success" 167 export test_prereq 168 if ! test_skip "$@" 169 then 170 base=$(basename "$0" .sh) 171 echo "$test_count" >>"$perf_results_dir"/$base.subtests 172 echo "$1" >"$perf_results_dir"/$base.$test_count.descr 173 base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count" 174 "$test_wrapper_func_" "$@" 175 fi 176 177 test_finish_ 178} 179 180test_perf_ () { 181 if test -z "$verbose"; then 182 printf "%s" "perf $test_count - $1:" 183 else 184 echo "perf $test_count - $1:" 185 fi 186 for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do 187 say >&3 "running: $2" 188 if test_run_perf_ "$2" 189 then 190 if test -z "$verbose"; then 191 printf " %s" "$i" 192 else 193 echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:" 194 fi 195 else 196 test -z "$verbose" && echo 197 test_failure_ "$@" 198 break 199 fi 200 done 201 if test -z "$verbose"; then 202 echo " ok" 203 else 204 test_ok_ "$1" 205 fi 206 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times 207} 208 209test_perf () { 210 test_wrapper_ test_perf_ "$@" 211} 212 213test_size_ () { 214 say >&3 "running: $2" 215 if test_eval_ "$2" 3>"$base".size; then 216 test_ok_ "$1" 217 else 218 test_failure_ "$@" 219 fi 220} 221 222test_size () { 223 test_wrapper_ test_size_ "$@" 224} 225 226# We extend test_done to print timings at the end (./run disables this 227# and does it after running everything) 228test_at_end_hook_ () { 229 if test -z "$GIT_PERF_AGGREGATING_LATER"; then 230 ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") ) 231 fi 232} 233 234test_export () { 235 export "$@" 236}