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