Merge branch 'maint'
[git] / t / perf / perf-lib.sh
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
20 case "$GIT_TEST_TEE_STARTED, $* " in
21 done,*)
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         ;;
32 esac
33
34 TEST_DIRECTORY=$(pwd)/..
35 TEST_OUTPUT_DIRECTORY=$(pwd)
36 if test -z "$GIT_TEST_INSTALLED"; then
37         perf_results_prefix=
38 else
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)
42 fi
43
44 TEST_NO_CREATE_REPO=t
45
46 . ../test-lib.sh
47
48 perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results
49 mkdir -p "$perf_results_dir"
50 rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
51
52 if test -z "$GIT_PERF_REPEAT_COUNT"; then
53         GIT_PERF_REPEAT_COUNT=3
54 fi
55 die_if_build_dir_not_repo () {
56         if ! ( cd "$TEST_DIRECTORY/.." &&
57                     git rev-parse --build-dir >/dev/null 2>&1 ); then
58                 error "No $1 defined, and your build directory is not a repo"
59         fi
60 }
61
62 if test -z "$GIT_PERF_REPO"; then
63         die_if_build_dir_not_repo '$GIT_PERF_REPO'
64         GIT_PERF_REPO=$TEST_DIRECTORY/..
65 fi
66 if test -z "$GIT_PERF_LARGE_REPO"; then
67         die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
68         GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
69 fi
70
71 test_perf_create_repo_from () {
72         test "$#" = 2 ||
73         error "bug in the test script: not 2 parameters to test-create-repo"
74         repo="$1"
75         source="$2"
76         source_git=$source/$(cd "$source" && git rev-parse --git-dir)
77         mkdir -p "$repo/.git"
78         (
79                 cd "$repo/.git" &&
80                 { cp -Rl "$source_git/objects" . 2>/dev/null ||
81                         cp -R "$source_git/objects" .; } &&
82                 for stuff in "$source_git"/*; do
83                         case "$stuff" in
84                                 */objects|*/hooks|*/config)
85                                         ;;
86                                 *)
87                                         cp -R "$stuff" . || break
88                                         ;;
89                         esac
90                 done &&
91                 cd .. &&
92                 git init -q &&
93                 mv .git/hooks .git/hooks-disabled 2>/dev/null
94         ) || error "failed to copy repository '$source' to '$repo'"
95 }
96
97 # call at least one of these to establish an appropriately-sized repository
98 test_perf_default_repo () {
99         test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
100 }
101 test_perf_large_repo () {
102         if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
103                 echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
104                 echo "warning: This will work, but may not be a sufficiently large repo" >&2
105                 echo "warning: for representative measurements." >&2
106         fi
107         test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
108 }
109 test_checkout_worktree () {
110         git checkout-index -u -a ||
111         error "git checkout-index failed"
112 }
113
114 # Performance tests should never fail.  If they do, stop immediately
115 immediate=t
116
117 test_run_perf_ () {
118         test_cleanup=:
119         test_export_="test_cleanup"
120         export test_cleanup test_export_
121         /usr/bin/time -f "%E %U %S" -o test_time.$i "$SHELL" -c '
122 . '"$TEST_DIRECTORY"/../test-lib-functions.sh'
123 test_export () {
124         [ $# != 0 ] || return 0
125         test_export_="$test_export_\\|$1"
126         shift
127         test_export "$@"
128 }
129 '"$1"'
130 ret=$?
131 set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars
132 exit $ret' >&3 2>&4
133         eval_ret=$?
134
135         if test $eval_ret = 0 || test -n "$expecting_failure"
136         then
137                 test_eval_ "$test_cleanup"
138                 . ./test_vars || error "failed to load updated environment"
139         fi
140         if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
141                 echo ""
142         fi
143         return "$eval_ret"
144 }
145
146
147 test_perf () {
148         test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
149         test "$#" = 2 ||
150         error "bug in the test script: not 2 or 3 parameters to test-expect-success"
151         export test_prereq
152         if ! test_skip "$@"
153         then
154                 base=$(basename "$0" .sh)
155                 echo "$test_count" >>"$perf_results_dir"/$base.subtests
156                 echo "$1" >"$perf_results_dir"/$base.$test_count.descr
157                 if test -z "$verbose"; then
158                         echo -n "perf $test_count - $1:"
159                 else
160                         echo "perf $test_count - $1:"
161                 fi
162                 for i in $(seq 1 $GIT_PERF_REPEAT_COUNT); do
163                         say >&3 "running: $2"
164                         if test_run_perf_ "$2"
165                         then
166                                 if test -z "$verbose"; then
167                                         echo -n " $i"
168                                 else
169                                         echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
170                                 fi
171                         else
172                                 test -z "$verbose" && echo
173                                 test_failure_ "$@"
174                                 break
175                         fi
176                 done
177                 if test -z "$verbose"; then
178                         echo " ok"
179                 else
180                         test_ok_ "$1"
181                 fi
182                 base="$perf_results_dir"/"$perf_results_prefix$(basename "$0" .sh)"."$test_count"
183                 "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".times
184         fi
185         echo >&3 ""
186 }
187
188 # We extend test_done to print timings at the end (./run disables this
189 # and does it after running everything)
190 test_at_end_hook_ () {
191         if test -z "$GIT_PERF_AGGREGATING_LATER"; then
192                 ( cd "$TEST_DIRECTORY"/perf && ./aggregate.perl $(basename "$0") )
193         fi
194 }
195
196 test_export () {
197         export "$@"
198 }