Merge branch 'ab/config-based-hooks-base' into seen
[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.  (This hook or list of hooks does not
28 #   include the built-in fsmonitor--daemon.)
29 #
30 # The big win for using fsmonitor is the elimination of the need to scan the
31 # working directory looking for changed and untracked files. If the file
32 # information is all cached in RAM, the benefits are reduced.
33 #
34 # GIT_PERF_7519_DROP_CACHE: if set, the OS caches are dropped between tests
35 #
36 # GIT_PERF_7519_TRACE: if set, enable trace logging during the test.
37 #   Trace logs will be grouped by fsmonitor provider.
38
39 test_perf_large_repo
40 test_checkout_worktree
41
42 test_lazy_prereq UNTRACKED_CACHE '
43         { git update-index --test-untracked-cache; ret=$?; } &&
44         test $ret -ne 1
45 '
46
47 test_lazy_prereq WATCHMAN '
48         command -v watchman
49 '
50
51 if test_have_prereq WATCHMAN
52 then
53         # Convert unix style paths to escaped Windows style paths for Watchman
54         case "$(uname -s)" in
55         MSYS_NT*)
56           GIT_WORK_TREE="$(cygpath -aw "$PWD" | sed 's,\\,/,g')"
57           ;;
58         *)
59           GIT_WORK_TREE="$PWD"
60           ;;
61         esac
62 fi
63
64 if test -n "$GIT_PERF_7519_DROP_CACHE"
65 then
66         # When using GIT_PERF_7519_DROP_CACHE, GIT_PERF_REPEAT_COUNT must be 1 to
67         # generate valid results. Otherwise the caching that happens for the nth
68         # run will negate the validity of the comparisons.
69         if test "$GIT_PERF_REPEAT_COUNT" -ne 1
70         then
71                 echo "warning: Setting GIT_PERF_REPEAT_COUNT=1" >&2
72                 GIT_PERF_REPEAT_COUNT=1
73         fi
74 fi
75
76 trace_start() {
77         if test -n "$GIT_PERF_7519_TRACE"
78         then
79                 name="$1"
80                 TEST_TRACE_DIR="$TEST_OUTPUT_DIRECTORY/test-trace/p7519/"
81                 echo "Writing trace logging to $TEST_TRACE_DIR"
82
83                 mkdir -p "$TEST_TRACE_DIR"
84
85                 # Start Trace2 logging and any other GIT_TRACE_* logs that you
86                 # want for this named test case.
87
88                 GIT_TRACE2_PERF="$TEST_TRACE_DIR/$name.trace2perf"
89                 export GIT_TRACE2_PERF
90
91                 >"$GIT_TRACE2_PERF"
92         fi
93 }
94
95 trace_stop() {
96         if test -n "$GIT_PERF_7519_TRACE"
97         then
98                 unset GIT_TRACE2_PERF
99         fi
100 }
101
102 test_expect_success "one time repo setup" '
103         # set untrackedCache depending on the environment
104         if test -n "$GIT_PERF_7519_UNTRACKED_CACHE"
105         then
106                 git config core.untrackedCache "$GIT_PERF_7519_UNTRACKED_CACHE"
107         else
108                 if test_have_prereq UNTRACKED_CACHE
109                 then
110                         git config core.untrackedCache true
111                 else
112                         git config core.untrackedCache false
113                 fi
114         fi &&
115
116         # set core.splitindex depending on the environment
117         if test -n "$GIT_PERF_7519_SPLIT_INDEX"
118         then
119                 git config core.splitIndex "$GIT_PERF_7519_SPLIT_INDEX"
120         fi &&
121
122         mkdir 1_file 10_files 100_files 1000_files 10000_files &&
123         for i in $(test_seq 1 10); do touch 10_files/$i; done &&
124         for i in $(test_seq 1 100); do touch 100_files/$i; done &&
125         for i in $(test_seq 1 1000); do touch 1000_files/$i; done &&
126         for i in $(test_seq 1 10000); do touch 10000_files/$i; done &&
127         git add 1_file 10_files 100_files 1000_files 10000_files &&
128         git commit -qm "Add files" &&
129
130         # If Watchman exists, watch the work tree and attempt a query.
131         if test_have_prereq WATCHMAN; then
132                 watchman watch "$GIT_WORK_TREE" &&
133                 watchman watch-list | grep -q -F "p7519-fsmonitor"
134         fi
135 '
136
137 setup_for_fsmonitor() {
138         # set INTEGRATION_SCRIPT depending on the environment
139         if test -n "$USE_FSMONITOR_DAEMON"
140         then
141                 git config core.useBuiltinFSMonitor true &&
142                 INTEGRATION_SCRIPT=false
143         elif test -n "$INTEGRATION_PATH"
144         then
145                 git config core.useBuiltinFSMonitor false &&
146                 INTEGRATION_SCRIPT="$INTEGRATION_PATH"
147         else
148                 git config core.useBuiltinFSMonitor false &&
149                 #
150                 # Choose integration script based on existence of Watchman.
151                 # Fall back to an empty integration script.
152                 #
153                 mkdir .git/hooks &&
154                 if test_have_prereq WATCHMAN
155                 then
156                         INTEGRATION_SCRIPT=".git/hooks/fsmonitor-watchman" &&
157                         cp "$TEST_DIRECTORY/../templates/hooks--fsmonitor-watchman.sample" "$INTEGRATION_SCRIPT"
158                 else
159                         INTEGRATION_SCRIPT=".git/hooks/fsmonitor-empty" &&
160                         write_script "$INTEGRATION_SCRIPT"<<-\EOF
161                         EOF
162                 fi
163         fi &&
164
165         git config core.fsmonitor "$INTEGRATION_SCRIPT" &&
166         git update-index --fsmonitor 2>error &&
167         if test_have_prereq WATCHMAN
168         then
169                 test_must_be_empty error  # ensure no silent error
170         else
171                 grep "Empty last update token" error
172         fi
173 }
174
175 test_perf_w_drop_caches () {
176         if test -n "$GIT_PERF_7519_DROP_CACHE"; then
177                 test-tool drop-caches
178         fi
179
180         test_perf "$@"
181 }
182
183 test_fsmonitor_suite() {
184         if test -n "$USE_FSMONITOR_DAEMON"
185         then
186                 DESC="builtin fsmonitor--daemon"
187         elif test -n "$INTEGRATION_SCRIPT"; then
188                 DESC="fsmonitor=$(basename $INTEGRATION_SCRIPT)"
189         else
190                 DESC="fsmonitor=disabled"
191         fi
192
193         test_expect_success "test_initialization" '
194                 git reset --hard &&
195                 git status  # Warm caches
196         '
197
198         test_perf_w_drop_caches "status ($DESC)" '
199                 git status
200         '
201
202         test_perf_w_drop_caches "status -uno ($DESC)" '
203                 git status -uno
204         '
205
206         test_perf_w_drop_caches "status -uall ($DESC)" '
207                 git status -uall
208         '
209
210         # Update the mtimes on upto 100k files to make status think
211         # that they are dirty.  For simplicity, omit any files with
212         # LFs (i.e. anything that ls-files thinks it needs to dquote).
213         # Then fully backslash-quote the paths to capture any
214         # whitespace so that they pass thru xargs properly.
215         #
216         test_perf_w_drop_caches "status (dirty) ($DESC)" '
217                 git ls-files | \
218                         head -100000 | \
219                         grep -v \" | \
220                         sed '\''s/\(.\)/\\\1/g'\'' | \
221                         xargs test-tool chmtime -300 &&
222                 git status
223         '
224
225         test_perf_w_drop_caches "diff ($DESC)" '
226                 git diff
227         '
228
229         test_perf_w_drop_caches "diff HEAD ($DESC)" '
230                 git diff HEAD
231         '
232
233         test_perf_w_drop_caches "diff -- 0_files ($DESC)" '
234                 git diff -- 1_file
235         '
236
237         test_perf_w_drop_caches "diff -- 10_files ($DESC)" '
238                 git diff -- 10_files
239         '
240
241         test_perf_w_drop_caches "diff -- 100_files ($DESC)" '
242                 git diff -- 100_files
243         '
244
245         test_perf_w_drop_caches "diff -- 1000_files ($DESC)" '
246                 git diff -- 1000_files
247         '
248
249         test_perf_w_drop_caches "diff -- 10000_files ($DESC)" '
250                 git diff -- 10000_files
251         '
252
253         test_perf_w_drop_caches "add ($DESC)" '
254                 git add  --all
255         '
256 }
257
258 #
259 # Run a full set of perf tests using each Hook-based fsmonitor provider,
260 # such as Watchman.
261 #
262
263 trace_start fsmonitor-watchman
264 if test -n "$GIT_PERF_7519_FSMONITOR"; then
265         for INTEGRATION_PATH in $GIT_PERF_7519_FSMONITOR; do
266                 test_expect_success "setup for fsmonitor $INTEGRATION_PATH" 'setup_for_fsmonitor'
267                 test_fsmonitor_suite
268         done
269 else
270         test_expect_success "setup for fsmonitor" 'setup_for_fsmonitor'
271         test_fsmonitor_suite
272 fi
273
274 if test_have_prereq WATCHMAN
275 then
276         watchman watch-del "$GIT_WORK_TREE" >/dev/null 2>&1 &&
277
278         # Work around Watchman bug on Windows where it holds on to handles
279         # preventing the removal of the trash directory
280         watchman shutdown-server >/dev/null 2>&1
281 fi
282 trace_stop
283
284 #
285 # Run a full set of perf tests with the fsmonitor feature disabled.
286 #
287
288 trace_start fsmonitor-disabled
289 test_expect_success "setup without fsmonitor" '
290         unset INTEGRATION_SCRIPT &&
291         git config --unset core.fsmonitor &&
292         git update-index --no-fsmonitor
293 '
294
295 test_fsmonitor_suite
296 trace_stop
297
298 #
299 # Run a full set of perf tests using the built-in fsmonitor--daemon.
300 # It does not use the Hook API, so it has a different setup.
301 # Explicitly start the daemon here and before we start client commands
302 # so that we can later add custom tracing.
303 #
304
305 test_lazy_prereq HAVE_FSMONITOR_DAEMON '
306         git version --build-options | grep "feature:" | grep "fsmonitor--daemon"
307 '
308
309 if test_have_prereq HAVE_FSMONITOR_DAEMON
310 then
311         USE_FSMONITOR_DAEMON=t
312
313         trace_start fsmonitor--daemon--server
314         git fsmonitor--daemon start
315
316         trace_start fsmonitor--daemon--client
317         test_expect_success "setup for fsmonitor--daemon" 'setup_for_fsmonitor'
318         test_fsmonitor_suite
319
320         git fsmonitor--daemon stop
321         trace_stop
322 fi
323
324 test_done