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