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 55perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results 56mkdir-p"$perf_results_dir" 57rm-f"$perf_results_dir"/$(basename "$0" .sh).subtests 58 59iftest -z"$GIT_PERF_REPEAT_COUNT";then 60 GIT_PERF_REPEAT_COUNT=3 61fi 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_create_repo_from () { 79test"$#"=2|| 80 error "bug in the test script: not 2 parameters to test-create-repo" 81 repo="$1" 82source="$2" 83 source_git="$(git -C "$source" rev-parse --git-dir)" 84 objects_dir="$(git -C "$source" rev-parse --git-path objects)" 85mkdir-p"$repo/.git" 86( 87cd"$source"&& 88{cp-Rl"$objects_dir""$repo/.git/"2>/dev/null || 89cp-R"$objects_dir""$repo/.git/"; } && 90for stuff in"$source_git"/*;do 91case"$stuff"in 92*/objects|*/hooks|*/config|*/commondir) 93;; 94*) 95cp-R"$stuff""$repo/.git/"||exit1 96;; 97esac 98done 99) && 100( 101cd"$repo"&& 102 git init -q&& { 103 test_have_prereq SYMLINKS || 104 git config core.symlinks false 105} && 106mv .git/hooks .git/hooks-disabled2>/dev/null 107) || error "failed to copy repository '$source' to '$repo'" 108} 109 110# call at least one of these to establish an appropriately-sized repository 111test_perf_default_repo () { 112 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}""$GIT_PERF_REPO" 113} 114test_perf_large_repo () { 115iftest"$GIT_PERF_LARGE_REPO"="$GIT_BUILD_DIR";then 116echo"warning: \$GIT_PERF_LARGE_REPOis \$GIT_BUILD_DIR.">&2 117echo"warning: This will work, but may not be a sufficiently large repo">&2 118echo"warning: for representative measurements.">&2 119fi 120 test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}""$GIT_PERF_LARGE_REPO" 121} 122test_checkout_worktree () { 123 git checkout-index -u -a|| 124 error "git checkout-index failed" 125} 126 127# Performance tests should never fail. If they do, stop immediately 128immediate=t 129 130# Perf tests require GNU time 131case"$(uname -s)"in Darwin) GTIME="${GTIME:-gtime}";;esac 132GTIME="${GTIME:-/usr/bin/time}" 133 134test_run_perf_ () { 135 test_cleanup=: 136 test_export_="test_cleanup" 137export test_cleanup test_export_ 138"$GTIME"-f"%E %U %S"-o test_time.$i"$SHELL"-c' 139. '"$TEST_DIRECTORY"/test-lib-functions.sh' 140test_export () { 141 [$#!= 0 ] || return 0 142 test_export_="$test_export_\\|$1" 143 shift 144 test_export "$@" 145} 146'"$1"' 147ret=$? 148set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars 149exit$ret'>&3 2>&4 150 eval_ret=$? 151 152iftest$eval_ret=0||test -n"$expecting_failure" 153then 154 test_eval_ "$test_cleanup" 155 . ./test_vars || error "failed to load updated environment" 156fi 157iftest"$verbose"="t"&&test -n"$HARNESS_ACTIVE";then 158echo"" 159fi 160return"$eval_ret" 161} 162 163 164test_perf () { 165 test_start_ 166test"$#"=3&& { test_prereq=$1;shift; } || test_prereq= 167test"$#"=2|| 168 error "bug in the test script: not 2 or 3 parameters to test-expect-success" 169export test_prereq 170if! test_skip "$@" 171then 172 base=$(basename "$0" .sh) 173echo"$test_count">>"$perf_results_dir"/$base.subtests 174echo"$1">"$perf_results_dir"/$base.$test_count.descr 175iftest -z"$verbose";then 176printf"%s""perf$test_count-$1:" 177else 178echo"perf$test_count-$1:" 179fi 180for i in$(test_seq 1 $GIT_PERF_REPEAT_COUNT);do 181 say >&3"running:$2" 182if test_run_perf_ "$2" 183then 184iftest -z"$verbose";then 185printf" %s""$i" 186else 187echo"* timing run$i/$GIT_PERF_REPEAT_COUNT:" 188fi 189else 190test -z"$verbose"&&echo 191 test_failure_ "$@" 192break 193fi 194done 195iftest -z"$verbose";then 196echo" ok" 197else 198 test_ok_ "$1" 199fi 200 base="$perf_results_dir"/"$perf_results_prefix$(basename "$0" .sh)"."$test_count" 201"$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times 202fi 203 test_finish_ 204} 205 206# We extend test_done to print timings at the end (./run disables this 207# and does it after running everything) 208test_at_end_hook_ () { 209iftest -z"$GIT_PERF_AGGREGATING_LATER";then 210(cd"$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0")) 211fi 212} 213 214test_export () { 215export"$@" 216}