version: bump to 0.15.1
[notmuch] / performance-test / perf-test-lib.sh
1 . ./version.sh
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 . ../test/test-lib-common.sh
29
30 set -e
31
32 if ! test -x ../notmuch
33 then
34         echo >&2 'You do not seem to have built notmuch yet.'
35         exit 1
36 fi
37
38 DB_CACHE_DIR=${TEST_DIRECTORY}/notmuch.cache.$corpus_size
39
40 add_email_corpus ()
41 {
42     rm -rf ${MAIL_DIR}
43
44     case "$corpus_size" in
45         small)
46             mail_subdir="mail/enron/bailey-s"
47             check_for="${TEST_DIRECTORY}/corpus/$mail_subdir"
48             ;;
49         medium)
50             mail_subdir="mail/notmuch-archive"
51             check_for="${TEST_DIRECTORY}/corpus/$mail_subdir"
52             ;;
53         *)
54             mail_subdir=mail
55             check_for="${TEST_DIRECTORY}/corpus/$mail_subdir/enron/wolfe-j"
56     esac
57
58     MAIL_CORPUS="${TEST_DIRECTORY}/corpus/$mail_subdir"
59     TAG_CORPUS="${TEST_DIRECTORY}/corpus/tags"
60
61     args=()
62     if [ ! -d "$TAG_CORPUS" ] ; then
63         args+=("notmuch-email-corpus/tags")
64     fi
65
66     if [ ! -d "$check_for" ] ; then
67         args+=("notmuch-email-corpus/$mail_subdir")
68     fi
69
70     if [[ ${#args[@]} > 0 ]]; then
71         if command -v pixz > /dev/null; then
72             XZ=pixz
73         else
74             XZ=xz
75         fi
76
77         printf "Unpacking corpus\n"
78         mkdir -p "${TEST_DIRECTORY}/corpus"
79
80         tar --checkpoint=.5000 --extract --strip-components=1 \
81             --directory ${TEST_DIRECTORY}/corpus \
82             --use-compress-program ${XZ} \
83             --file ../download/notmuch-email-corpus-${PERFTEST_VERSION}.tar.xz \
84             "${args[@]}"
85
86         printf "\n"
87
88     fi
89
90     cp -lr $TAG_CORPUS $TMP_DIRECTORY/corpus.tags
91     cp -lr $MAIL_CORPUS $MAIL_DIR
92 }
93
94 notmuch_new_with_cache ()
95 {
96     if [ -d $DB_CACHE_DIR ]; then
97         cp -r $DB_CACHE_DIR ${MAIL_DIR}/.notmuch
98     else
99         "$1" 'Initial notmuch new' "notmuch new"
100         cache_database
101     fi
102 }
103
104 time_start ()
105 {
106     add_email_corpus
107
108     print_header
109
110     notmuch_new_with_cache time_run
111 }
112
113 memory_start ()
114 {
115     add_email_corpus
116
117     local timestamp=$(date +%Y%m%dT%H%M%S)
118     log_dir="${TEST_DIRECTORY}/log.$(basename $0)-$corpus_size-${timestamp}"
119     mkdir -p ${log_dir}
120
121     notmuch_new_with_cache memory_run
122 }
123
124 memory_run ()
125 {
126     test_count=$(($test_count+1))
127
128     log_file=$log_dir/$test_count.log
129     talloc_log=$log_dir/$test_count.talloc
130
131     printf "[ %d ]\t%s\n" $test_count "$1"
132
133     NOTMUCH_TALLOC_REPORT="$talloc_log" valgrind --leak-check=full --log-file="$log_file" $2
134
135     awk '/LEAK SUMMARY/,/suppressed/ { sub(/^==[0-9]*==/," "); print }' "$log_file"
136     echo
137     sed -n -e 's/.*[(]total *\([^)]*\)[)]/talloced at exit: \1/p' $talloc_log
138     echo
139 }
140
141 memory_done ()
142 {
143     time_done
144 }
145
146 cache_database ()
147 {
148     if [ -d $MAIL_DIR/.notmuch ]; then
149         cp -r $MAIL_DIR/.notmuch $DB_CACHE_DIR
150     else
151         echo "Warning: No database found to cache"
152     fi
153 }
154
155 uncache_database ()
156 {
157     rm -rf $DB_CACHE_DIR
158 }
159
160 print_header ()
161 {
162     printf "\t\t\tWall(s)\tUsr(s)\tSys(s)\tRes(K)\tIn/Out(512B)\n"
163 }
164
165 time_run ()
166 {
167     printf "  %-22s" "$1"
168     test_count=$(($test_count+1))
169     if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
170     if ! eval >&3 "/usr/bin/time -f '%e\t%U\t%S\t%M\t%I/%O' $2" ; then
171         test_failure=$(($test_failure + 1))
172         return 1
173     fi
174     return 0
175 }
176
177 time_done ()
178 {
179     if [ "$test_failure" = "0" ]; then
180         rm -rf "$remove_tmp"
181         exit 0
182     else
183         exit 1
184     fi
185 }
186
187 cd -P "$test" || error "Cannot setup test environment"
188 test_failure=0
189 test_count=0
190
191 printf "\n%-55s [%s %s]\n"  \
192     "$(basename "$0"): Testing ${test_description:-notmuch performance}" \
193     "${PERFTEST_VERSION}"  "${corpus_size}"