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