Merge branch 'mt/worktree-error-message-fix'
[git] / t / perf / p7519-fsmonitor.sh
1 #!/bin/sh
2
3 test_description="Test core.fsmonitor"
4
5 . ./perf-lib.sh
6
7 #
8 # Performance test for the fsmonitor feature which enables git to talk to a
9 # file system change monitor and avoid having to scan the working directory
10 # for new or modified files.
11 #
12 # By default, the performance test will utilize the Watchman file system
13 # monitor if it is installed.  If Watchman is not installed, it will use a
14 # dummy integration script that does not report any new or modified files.
15 # The dummy script has very little overhead which provides optimistic results.
16 #
17 # The performance test will also use the untracked cache feature if it is
18 # available as fsmonitor uses it to speed up scanning for untracked files.
19 #
20 # There are 3 environment variables that can be used to alter the default
21 # behavior of the performance test:
22 #
23 # GIT_PERF_7519_UNTRACKED_CACHE: used to configure core.untrackedCache
24 # GIT_PERF_7519_SPLIT_INDEX: used to configure core.splitIndex
25 # GIT_PERF_7519_FSMONITOR: used to configure core.fsMonitor. May be an
26 #   absolute path to an integration. May be a space delimited list of
27 #   absolute paths to integrations.
28 #
29 # The big win for using fsmonitor is the elimination of the need to scan the
30 # working directory looking for changed and untracked files. If the file
31 # information is all cached in RAM, the benefits are reduced.
32 #
33 # GIT_PERF_7519_DROP_CACHE: if set, the OS caches are dropped between tests
34 #
35
36 test_perf_large_repo
37 test_checkout_worktree
38
39 test_lazy_prereq UNTRACKED_CACHE '
40         { git update-index --test-untracked-cache; ret=$?; } &&
41         test $ret -ne 1
42 '
43
44 test_lazy_prereq WATCHMAN '
45         command -v watchman
46 '
47
48 if test_have_prereq WATCHMAN
49 then
50         # Convert unix style paths to escaped Windows style paths for Watchman
51         case "$(uname -s)" in
52         MSYS_NT*)
53           GIT_WORK_TREE="$(cygpath -aw "$PWD" | sed 's,\\,/,g')"
54           ;;
55         *)
56           GIT_WORK_TREE="$PWD"
57           ;;
58         esac
59 fi
60
61 if test -n "$GIT_PERF_7519_DROP_CACHE"
62 then
63         # When using GIT_PERF_7519_DROP_CACHE, GIT_PERF_REPEAT_COUNT must be 1 to
64         # generate valid results. Otherwise the caching that happens for the nth
65         # run will negate the validity of the comparisons.
66         if test "$GIT_PERF_REPEAT_COUNT" -ne 1
67         then
68                 echo "warning: Setting GIT_PERF_REPEAT_COUNT=1" >&2
69                 GIT_PERF_REPEAT_COUNT=1
70         fi
71 fi
72
73 test_expect_success "one time repo setup" '
74         # set untrackedCache depending on the environment
75         if test -n "$GIT_PERF_7519_UNTRACKED_CACHE"
76         then
77                 git config core.untrackedCache "$GIT_PERF_7519_UNTRACKED_CACHE"
78         else
79                 if test_have_prereq UNTRACKED_CACHE
80                 then
81                         git config core.untrackedCache true
82                 else
83                         git config core.untrackedCache false
84                 fi
85         fi &&
86
87         # set core.splitindex depending on the environment
88         if test -n "$GIT_PERF_7519_SPLIT_INDEX"
89         then
90                 git config core.splitIndex "$GIT_PERF_7519_SPLIT_INDEX"
91         fi &&
92
93         mkdir 1_file 10_files 100_files 1000_files 10000_files &&
94         for i in $(test_seq 1 10); do touch 10_files/$i; done &&
95         for i in $(test_seq 1 100); do touch 100_files/$i; done &&
96         for i in $(test_seq 1 1000); do touch 1000_files/$i; done &&
97         for i in $(test_seq 1 10000); do touch 10000_files/$i; done &&
98         git add 1_file 10_files 100_files 1000_files 10000_files &&
99         git commit -qm "Add files" &&
100
101         # If Watchman exists, watch the work tree and attempt a query.
102         if test_have_prereq WATCHMAN; then
103                 watchman watch "$GIT_WORK_TREE" &&
104                 watchman watch-list | grep -q -F "$GIT_WORK_TREE"
105         fi
106 '
107
108 setup_for_fsmonitor() {
109         # set INTEGRATION_SCRIPT depending on the environment
110         if test -n "$INTEGRATION_PATH"
111         then
112                 INTEGRATION_SCRIPT="$INTEGRATION_PATH"
113         else
114                 #
115                 # Choose integration script based on existence of Watchman.
116                 # Fall back to an empty integration script.
117                 #
118                 mkdir .git/hooks &&
119                 if test_have_prereq WATCHMAN
120                 then
121                         INTEGRATION_SCRIPT=".git/hooks/fsmonitor-watchman" &&
122                         cp "$TEST_DIRECTORY/../templates/hooks--fsmonitor-watchman.sample" "$INTEGRATION_SCRIPT"
123                 else
124                         INTEGRATION_SCRIPT=".git/hooks/fsmonitor-empty" &&
125                         write_script "$INTEGRATION_SCRIPT"<<-\EOF
126                         EOF
127                 fi
128         fi &&
129
130         git config core.fsmonitor "$INTEGRATION_SCRIPT" &&
131         git update-index --fsmonitor 2>error &&
132         cat error &&
133         [ ! -s error ] # ensure no silent error
134 }
135
136 test_perf_w_drop_caches () {
137         if test -n "$GIT_PERF_7519_DROP_CACHE"; then
138                 test-tool drop-caches
139         fi
140
141         test_perf "$@"
142 }
143
144 test_fsmonitor_suite() {
145         if test -n "$INTEGRATION_SCRIPT"; then
146                 DESC="fsmonitor=$(basename $INTEGRATION_SCRIPT)"
147         else
148                 DESC="fsmonitor=disabled"
149         fi
150
151         test_expect_success "test_initialization" '
152                 git reset --hard &&
153                 git status  # Warm caches
154         '
155
156         test_perf_w_drop_caches "status ($DESC)" '
157                 git status
158         '
159
160         test_perf_w_drop_caches "status -uno ($DESC)" '
161                 git status -uno
162         '
163
164         test_perf_w_drop_caches "status -uall ($DESC)" '
165                 git status -uall
166         '
167
168         test_perf_w_drop_caches "status (dirty) ($DESC)" '
169                 git ls-files | head -100000 | xargs -d "\n" touch -h &&
170                 git status
171         '
172
173         test_perf_w_drop_caches "diff ($DESC)" '
174                 git diff
175         '
176
177         test_perf_w_drop_caches "diff -- 0_files ($DESC)" '
178                 git diff -- 1_file
179         '
180
181         test_perf_w_drop_caches "diff -- 10_files ($DESC)" '
182                 git diff -- 10_files
183         '
184
185         test_perf_w_drop_caches "diff -- 100_files ($DESC)" '
186                 git diff -- 100_files
187         '
188
189         test_perf_w_drop_caches "diff -- 1000_files ($DESC)" '
190                 git diff -- 1000_files
191         '
192
193         test_perf_w_drop_caches "diff -- 10000_files ($DESC)" '
194                 git diff -- 10000_files
195         '
196
197         test_perf_w_drop_caches "add ($DESC)" '
198                 git add  --all
199         '
200 }
201
202 if test -n "$GIT_PERF_7519_FSMONITOR"; then
203         for INTEGRATION_PATH in $GIT_PERF_7519_FSMONITOR; do
204                 test_expect_success "setup for fsmonitor $INTEGRATION_PATH" 'setup_for_fsmonitor'
205                 test_fsmonitor_suite
206         done
207 else
208         test_expect_success "setup for fsmonitor" 'setup_for_fsmonitor'
209         test_fsmonitor_suite
210 fi
211
212 test_expect_success "setup without fsmonitor" '
213         unset INTEGRATION_SCRIPT &&
214         git config --unset core.fsmonitor &&
215         git update-index --no-fsmonitor
216 '
217
218 test_fsmonitor_suite
219
220 if test_have_prereq WATCHMAN
221 then
222         watchman watch-del "$GIT_WORK_TREE" >/dev/null 2>&1 &&
223
224         # Work around Watchman bug on Windows where it holds on to handles
225         # preventing the removal of the trash directory
226         watchman shutdown-server >/dev/null 2>&1
227 fi
228
229 test_done