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