t / perf / perf-lib.shon commit t5510: prepare test refs more straightforwardly (d0d06e8)
   1#!/bin/sh
   2#
   3# Copyright (c) 2011 Thomas Rast
   4#
   5# This program is free software: you can redistribute it and/or modify
   6# it under the terms of the GNU General Public License as published by
   7# the Free Software Foundation, either version 2 of the License, or
   8# (at your option) any later version.
   9#
  10# This program is distributed in the hope that it will be useful,
  11# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13# GNU General Public License for more details.
  14#
  15# You should have received a copy of the GNU General Public License
  16# along with this program.  If not, see http://www.gnu.org/licenses/ .
  17
  18# do the --tee work early; it otherwise confuses our careful
  19# GIT_BUILD_DIR mangling
  20case "$GIT_TEST_TEE_STARTED, $* " in
  21done,*)
  22        # do not redirect again
  23        ;;
  24*' --tee '*|*' --va'*)
  25        mkdir -p test-results
  26        BASE=test-results/$(basename "$0" .sh)
  27        (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
  28         echo $? > $BASE.exit) | tee $BASE.out
  29        test "$(cat $BASE.exit)" = 0
  30        exit
  31        ;;
  32esac
  33
  34TEST_DIRECTORY=$(pwd)/..
  35TEST_OUTPUT_DIRECTORY=$(pwd)
  36if test -z "$GIT_TEST_INSTALLED"; then
  37        perf_results_prefix=
  38else
  39        perf_results_prefix=$(printf "%s" "${GIT_TEST_INSTALLED%/bin-wrappers}" | tr -c "[a-zA-Z0-9]" "[_*]")"."
  40        # make the tested dir absolute
  41        GIT_TEST_INSTALLED=$(cd "$GIT_TEST_INSTALLED" && pwd)
  42fi
  43
  44TEST_NO_CREATE_REPO=t
  45TEST_NO_MALLOC_CHECK=t
  46
  47. ../test-lib.sh
  48
  49# Variables from test-lib that are normally internal to the tests; we
  50# need to export them for test_perf subshells
  51export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
  52
  53perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results
  54mkdir -p "$perf_results_dir"
  55rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
  56
  57if test -z "$GIT_PERF_REPEAT_COUNT"; then
  58        GIT_PERF_REPEAT_COUNT=3
  59fi
  60die_if_build_dir_not_repo () {
  61        if ! ( cd "$TEST_DIRECTORY/.." &&
  62                    git rev-parse --build-dir >/dev/null 2>&1 ); then
  63                error "No $1 defined, and your build directory is not a repo"
  64        fi
  65}
  66
  67if test -z "$GIT_PERF_REPO"; then
  68        die_if_build_dir_not_repo '$GIT_PERF_REPO'
  69        GIT_PERF_REPO=$TEST_DIRECTORY/..
  70fi
  71if test -z "$GIT_PERF_LARGE_REPO"; then
  72        die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
  73        GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
  74fi
  75
  76test_perf_create_repo_from () {
  77        test "$#" = 2 ||
  78        error "bug in the test script: not 2 parameters to test-create-repo"
  79        repo="$1"
  80        source="$2"
  81        source_git=$source/$(cd "$source" && git rev-parse --git-dir)
  82        mkdir -p "$repo/.git"
  83        (
  84                cd "$repo/.git" &&
  85                { cp -Rl "$source_git/objects" . 2>/dev/null ||
  86                        cp -R "$source_git/objects" .; } &&
  87                for stuff in "$source_git"/*; do
  88                        case "$stuff" in
  89                                */objects|*/hooks|*/config)
  90                                        ;;
  91                                *)
  92                                        cp -R "$stuff" . || break
  93                                        ;;
  94                        esac
  95                done &&
  96                cd .. &&
  97                git init -q &&
  98                mv .git/hooks .git/hooks-disabled 2>/dev/null
  99        ) || error "failed to copy repository '$source' to '$repo'"
 100}
 101
 102# call at least one of these to establish an appropriately-sized repository
 103test_perf_default_repo () {
 104        test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
 105}
 106test_perf_large_repo () {
 107        if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
 108                echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
 109                echo "warning: This will work, but may not be a sufficiently large repo" >&2
 110                echo "warning: for representative measurements." >&2
 111        fi
 112        test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
 113}
 114test_checkout_worktree () {
 115        git checkout-index -u -a ||
 116        error "git checkout-index failed"
 117}
 118
 119# Performance tests should never fail.  If they do, stop immediately
 120immediate=t
 121
 122test_run_perf_ () {
 123        test_cleanup=:
 124        test_export_="test_cleanup"
 125        export test_cleanup test_export_
 126        /usr/bin/time -f "%E %U %S" -o test_time.$i "$SHELL" -c '
 127. '"$TEST_DIRECTORY"/test-lib-functions.sh'
 128test_export () {
 129        [ $# != 0 ] || return 0
 130        test_export_="$test_export_\\|$1"
 131        shift
 132        test_export "$@"
 133}
 134'"$1"'
 135ret=$?
 136set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars
 137exit $ret' >&3 2>&4
 138        eval_ret=$?
 139
 140        if test $eval_ret = 0 || test -n "$expecting_failure"
 141        then
 142                test_eval_ "$test_cleanup"
 143                . ./test_vars || error "failed to load updated environment"
 144        fi
 145        if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
 146                echo ""
 147        fi
 148        return "$eval_ret"
 149}
 150
 151
 152test_perf () {
 153        test_start_
 154        test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 155        test "$#" = 2 ||
 156        error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 157        export test_prereq
 158        if ! test_skip "$@"
 159        then
 160                base=$(basename "$0" .sh)
 161                echo "$test_count" >>"$perf_results_dir"/$base.subtests
 162                echo "$1" >"$perf_results_dir"/$base.$test_count.descr
 163                if test -z "$verbose"; then
 164                        printf "%s" "perf $test_count - $1:"
 165                else
 166                        echo "perf $test_count - $1:"
 167                fi
 168                for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
 169                        say >&3 "running: $2"
 170                        if test_run_perf_ "$2"
 171                        then
 172                                if test -z "$verbose"; then
 173                                        printf " %s" "$i"
 174                                else
 175                                        echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
 176                                fi
 177                        else
 178                                test -z "$verbose" && echo
 179                                test_failure_ "$@"
 180                                break
 181                        fi
 182                done
 183                if test -z "$verbose"; then
 184                        echo " ok"
 185                else
 186                        test_ok_ "$1"
 187                fi
 188                base="$perf_results_dir"/"$perf_results_prefix$(basename "$0" .sh)"."$test_count"
 189                "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times
 190        fi
 191        test_finish_
 192}
 193
 194# We extend test_done to print timings at the end (./run disables this
 195# and does it after running everything)
 196test_at_end_hook_ () {
 197        if test -z "$GIT_PERF_AGGREGATING_LATER"; then
 198                ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") )
 199        fi
 200}
 201
 202test_export () {
 203        export "$@"
 204}