]> git.notmuchmail.org Git - notmuch/blob - performance-test/perf-test-lib.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / performance-test / perf-test-lib.sh
1 . $(dirname "$0")/version.sh || exit 1
2
3 debug=""
4 corpus_size=large
5 perf_callgraph=lbr
6 use_perf=0
7
8 while test "$#" -ne 0
9 do
10         case "$1" in
11         -d|--debug)
12                 debug=t;
13                 shift
14                 ;;
15         -p|--perf)
16                 use_perf=1;
17                 shift
18                 ;;
19         -c|--call-graph)
20                 shift
21                 perf_callgraph=$1
22                 shift
23                 ;;
24         -s|--small)
25                 corpus_size=small;
26                 shift
27                 ;;
28         -m|--medium)
29                 corpus_size=medium;
30                 shift
31                 ;;
32         -l|--large)
33                 corpus_size=large;
34                 shift
35                 ;;
36         *)
37                 echo "error: unknown performance test option '$1'" >&2; exit 1 ;;
38         esac
39 done
40
41 # Ensure NOTMUCH_SRCDIR and NOTMUCH_BUILDDIR are set.
42 . $(dirname "$0")/../test/export-dirs.sh || exit 1
43
44 # Where to run the tests
45 TEST_DIRECTORY=$NOTMUCH_BUILDDIR/performance-test
46
47 . "$NOTMUCH_SRCDIR/test/test-lib-common.sh" || exit 1
48
49 set -e
50
51 # It appears that people try to run tests without building...
52 if [[ ! -x "$NOTMUCH_BUILDDIR/notmuch" ]]; then
53         echo >&2 'You do not seem to have built notmuch yet.'
54         exit 1
55 fi
56
57 DB_CACHE_DIR=${TEST_DIRECTORY}/notmuch.cache.$corpus_size
58
59 add_email_corpus ()
60 {
61     rm -rf ${MAIL_DIR}
62
63     CORPUS_DIR=${TEST_DIRECTORY}/corpus
64     mkdir -p "${CORPUS_DIR}"
65
66     MAIL_CORPUS="${CORPUS_DIR}/mail.${corpus_size}"
67     TAG_CORPUS="${CORPUS_DIR}/tags"
68
69     if command -v pixz > /dev/null; then
70         XZ=pixz
71     else
72         XZ=xz
73     fi
74
75     if [ ! -d "${CORPUS_DIR}/manifest" ]; then
76
77         printf "Unpacking manifests\n"
78         tar --extract --use-compress-program ${XZ} --strip-components=1 \
79             --directory ${TEST_DIRECTORY}/corpus \
80             --wildcards --file ../download/notmuch-email-corpus-${PERFTEST_VERSION}.tar.xz \
81             'notmuch-email-corpus/manifest/*'
82     fi
83
84     file_list=$(mktemp file_listXXXXXX)
85     declare -a extract_dirs
86     if [ ! -d "$TAG_CORPUS" ] ; then
87         extract_dirs=("${extract_dirs[@]}" notmuch-email-corpus/tags)
88     fi
89
90     if [ ! -d "$MAIL_CORPUS" ] ; then
91         if [[ "$corpus_size" != "large" ]]; then
92             sed s,^,notmuch-email-corpus/, < \
93                 ${TEST_DIRECTORY}/corpus/manifest/MANIFEST.${corpus_size} >> $file_list
94         else
95             extract_dirs=("${extract_dirs[@]}" notmuch-email-corpus/mail)
96         fi
97     fi
98
99     if [[ -s $file_list || -n "${extract_dirs[*]}" ]]; then
100
101         printf "Unpacking corpus\n"
102         tar --checkpoint=.5000 --extract --strip-components=1 \
103             --directory ${TEST_DIRECTORY}/corpus \
104             --use-compress-program ${XZ} \
105             --file ../download/notmuch-email-corpus-${PERFTEST_VERSION}.tar.xz \
106             --anchored --recursion \
107             --files-from $file_list "${extract_dirs[@]}"
108
109         printf "\n"
110
111         if [[ ! -d ${MAIL_CORPUS} ]]; then
112             printf "creating link farm\n"
113
114             if [[ "$corpus_size" = large ]]; then
115                 cp -rl ${TEST_DIRECTORY}/corpus/mail ${MAIL_CORPUS}
116             else
117                 while read -r file; do
118                     tdir=${MAIL_CORPUS}/$(dirname $file)
119                     mkdir -p $tdir
120                     ln ${TEST_DIRECTORY}/corpus/$file $tdir
121                 done <${TEST_DIRECTORY}/corpus/manifest/MANIFEST.${corpus_size}
122             fi
123         fi
124
125     fi
126
127     rm $file_list
128     cp -lr $TAG_CORPUS $TMP_DIRECTORY/corpus.tags
129     cp -lr $MAIL_CORPUS $MAIL_DIR
130 }
131
132 notmuch_new_with_cache ()
133 {
134     if [ -d $DB_CACHE_DIR ]; then
135         cp -r $DB_CACHE_DIR ${MAIL_DIR}/.notmuch
136     else
137         "$1" 'Initial notmuch new' "notmuch new"
138         cache_database
139     fi
140 }
141
142 make_log_dir () {
143     local timestamp=$(date +%Y%m%dT%H%M%S)
144     log_dir=${TEST_DIRECTORY}/log.$(basename $0)-$corpus_size-${timestamp}
145     mkdir -p "${log_dir}"
146 }
147
148 time_start ()
149 {
150     add_email_corpus
151
152     if [[ "$use_perf" = 1 ]]; then
153         make_log_dir
154     fi
155
156     print_header
157
158     notmuch_new_with_cache time_run
159 }
160
161 memory_start ()
162 {
163     add_email_corpus
164
165     make_log_dir
166
167     notmuch_new_with_cache memory_run
168 }
169
170 memory_run ()
171 {
172     test_count=$(($test_count+1))
173
174     log_file=$log_dir/$test_count.log
175     talloc_log=$log_dir/$test_count.talloc
176
177     printf "[ %d ]\t%s\n" $test_count "$1"
178
179     NOTMUCH_TALLOC_REPORT="$talloc_log" eval "valgrind --leak-check=full --log-file='$log_file' $2"
180
181     awk '/LEAK SUMMARY/,/suppressed/ { sub(/^==[0-9]*==/," "); print }' "$log_file"
182     echo
183     sed -n -e 's/.*[(]total *\([^)]*\)[)]/talloced at exit: \1/p' $talloc_log
184     echo
185 }
186
187 memory_done ()
188 {
189     time_done
190 }
191
192 cache_database ()
193 {
194     if [ -d $MAIL_DIR/.notmuch ]; then
195         cp -r $MAIL_DIR/.notmuch $DB_CACHE_DIR
196     else
197         echo "Warning: No database found to cache"
198     fi
199 }
200
201 uncache_database ()
202 {
203     rm -rf $DB_CACHE_DIR
204 }
205
206 print_header ()
207 {
208     printf "\t\t\tWall(s)\tUsr(s)\tSys(s)\tRes(K)\tIn/Out(512B)\n"
209 }
210
211 time_run ()
212 {
213     printf "  %-22s" "$1"
214     test_count=$(($test_count+1))
215     if test "$verbose" != "t"; then exec 4>test.output 3>&4; else exec 3>&1; fi
216     if [[ "$use_perf" = 1 ]]; then
217         command_str="perf record --call-graph=${perf_callgraph} -o ${log_dir}/${test_count}.perf $2"
218     else
219         command_str="/usr/bin/time -f '%e\t%U\t%S\t%M\t%I/%O' $2"
220     fi
221
222     if ! eval >&3 "$command_str" ; then
223         test_failure=$(($test_failure + 1))
224         return 1
225     fi
226     return 0
227 }
228
229 time_done ()
230 {
231     if [ "$test_failure" = "0" ]; then
232         rm -rf "$remove_tmp"
233         exit 0
234     else
235         exit 1
236     fi
237 }
238
239 cd -P "$test" || error "Cannot set up test environment"
240 test_failure=0
241 test_count=0
242
243 printf "\n%-55s [%s %s]\n"  \
244     "$(basename "$0"): Testing ${test_description:-notmuch performance}" \
245     "${PERFTEST_VERSION}"  "${corpus_size}"