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