Debian Codegraph @

benchmark/run

raw
1 #!/bin/bash
2
3 set -ue
4 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
5 export PATH
6
7 unset CHROOT NETNS
8 cleanup() {
9 if [ "${CHROOT+x}" ]; then
10 schroot --end-session --chroot="$CHROOT"
11 fi
12 if [ "${NETNS+x}" ]; then
13 ip netns del "$NETNS"
14 fi
15 }
16 trap cleanup EXIT INT TERM
17
18 # create new CHROOT
19 DEB_BUILD_ARCH="$(dpkg-architecture -qDEB_BUILD_ARCH)"
20 CHROOT="$(schroot -c "unstable-$DEB_BUILD_ARCH-sbuild" -b)"
21 ROOTDIR="/run/schroot/mount/$CHROOT"
22
23 # create new network namespace and place counters to measure network usage
24 ip netns add "${NETNS:="interimap-benchmark"}"
25 ip netns exec "$NETNS" nft -f- <<- EOF
26 flush ruleset
27 table inet filter {
28 counter interimap-in { }
29 counter interimap-out { }
30 counter offlineimap-in { }
31 counter offlineimap-out { }
32 chain input {
33 type filter hook input priority 0
34 iif "lo" ip daddr 127.0.0.1 tcp dport 10143 counter name interimap-in
35 iif "lo" ip daddr 127.0.0.1 tcp dport 10144 counter name offlineimap-in
36 }
37 chain output {
38 type filter hook output priority 0
39 oif "lo" ip saddr 127.0.0.1 tcp sport 10143 counter name interimap-out
40 oif "lo" ip saddr 127.0.0.1 tcp sport 10144 counter name offlineimap-out
41 }
42 }
43 EOF
44 ip netns exec "$NETNS" ip addr add "127.0.0.1" dev "lo"
45 ip netns exec "$NETNS" ip link set "lo" up
46
47 # resize the partition so it can hold the mail stores (you may want to
48 # turn swap off too)
49 mount -o"remount,size=15G" "/run/schroot/mount/$CHROOT/dev/shm"
50
51 # install dependencies
52 schroot --directory="/" --chroot="$CHROOT" -r -- \
53 env DEBIAN_FRONTEND="noninteractive" apt-get install \
54 --no-install-recommends --assume-yes \
55 time dovecot-imapd offlineimap \
56 libconfig-tiny-perl libdbd-sqlite3-perl libnet-ssleay-perl \
57 libcrypt-urandom-perl procps
58
59 # run a command in the chroot
60 jail() {
61 local user="" home=""
62 case "${u:-local}" in
63 local) user="nobody"; home="/dev/shm/nobody";;
64 remote) user="user"; home="/dev/shm/vmail/user";;
65 esac
66 ip netns exec "$NETNS" \
67 schroot --directory="/dev/shm/nobody" --user="nobody" --chroot="$CHROOT" -r \
68 -- env -i PATH="/usr/bin:/bin" USER="$user" HOME="$home" "$@"
69 }
70
71 # run a command in the chroot in a monitored fashion
72 jail_stat() {
73 local e U S M P u="local"
74 local counters="$ROOTDIR/tmp/counters.$1.json"
75 ip netns exec "$NETNS" nft reset counter inet filter "$1-in" >/dev/null
76 ip netns exec "$NETNS" nft reset counter inet filter "$1-out" >/dev/null
77
78 jail time --format="%e\\t%U\\t%S\\t%M\\t%P" --output="/tmp/time.$1" \
79 -- "$@" >/dev/null || true
80 IFS=$'\t' read e U S M P <"$ROOTDIR/tmp/time.$1"
81
82 local i="${NAME:-$1}"
83 local a="${i#* }"
84 [ "$a" = "$i" ] && a=" " || a=" $a"
85
86 printf "%11s%s" "${i%% *}" "$a"
87 printf " %5.2fs %6.2fs" "$U" "$S"
88 if [ "${IDLE:-n}" = "n" ]; then
89 printf " %5.2fs %4s" "$e" "$P"
90 fi
91 printf " %8s" "${M}k"
92
93 ip netns exec "$NETNS" nft -j list counters | jq ".nftables
94 | map(select(.counter) | .counter | { key: .name, value: {packets, bytes} })
95 | from_entries" >"$counters"
96
97 local ib ip ob op
98 ib="$(bytes "$(jq ".\"$1-in\".bytes" <"$counters")")"
99 ob="$(bytes "$(jq ".\"$1-out\".bytes" <"$counters")")"
100 ip="$(_units "$(jq ".\"$1-in\".packets" <"$counters")" 1000)"
101 op="$(_units "$(jq ".\"$1-out\".packets" <"$counters")" 1000)"
102 printf " %8s / %-7s" "$ob" "$ib" # inverse for the client's perspective
103 printf " %8s / %-7s" "$op" "$ip"
104 printf "\\n"
105 }
106
107 # display metrics headers
108 headers() {
109 declare -a h=(" user" " system")
110 if [ "${IDLE:-n}" = "n" ]; then
111 h+=( " real" " CPU" )
112 fi
113 h+=( " max RSS" " traffic (in/out) " " packets (in/out) " )
114 local x="offlineimap -q" i
115
116 printf "%s" "${x//?/ }"
117 for i in "${h[@]}"; do
118 printf " %s" "$i"
119 done
120 printf "\\n"
121
122 printf "%s" "${x//?/-}"
123 for i in "${h[@]}"; do
124 printf " %s" "${i//?/-}"
125 done
126 printf "\\n"
127 }
128
129 # install Dovecot's "system" configuration and start the server
130 install -onobody -gnogroup -m0700 --directory \
131 "$ROOTDIR/dev/shm/dovecot" \
132 "$ROOTDIR/dev/shm/vmail" \
133 "$ROOTDIR/dev/shm/nobody"
134
135 install -onobody -gnogroup -m0644 \
136 "./benchmark/dovecot.conf" \
137 "$ROOTDIR/dev/shm/dovecot/config"
138 jail /usr/sbin/dovecot -c"/dev/shm/dovecot/config"
139
140 install -onobody -gnogroup -m0600 /dev/null \
141 "$ROOTDIR/dev/shm/dovecot/users"
142 PASSWORD="$(xxd -l16 -p </dev/urandom)"
143 printf "%s:%s::::%s:\\n" "user" "$PASSWORD" "/dev/shm/vmail/user" \
144 >"$ROOTDIR/dev/shm/dovecot/users"
145
146 # install user configuration for Dovecot, interimap, and offlineimap
147 cat >"$ROOTDIR/dev/shm/nobody/.dovecot.conf" <<-EOF
148 dovecot_config_version = 2.4.0
149 dovecot_storage_version = 2.4.0
150 log_path = /dev/null
151 ssl = no
152 mail_home = /dev/shm/nobody
153 mail_driver = maildir
154 mail_path = ~/Maildir
155 mailbox_list_index = yes
156 EOF
157
158 install -onobody -gnogroup -Dm0700 --directory \
159 "$ROOTDIR/dev/shm/nobody/.config/interimap" \
160 "$ROOTDIR/dev/shm/nobody/.local/share"
161
162 cat >"$ROOTDIR/dev/shm/nobody/.config/interimap/config" <<-EOF
163 database = bench.db
164
165 [local]
166 type = tunnel
167 command = doveadm -c/dev/shm/nobody/.dovecot.conf exec imap
168 null-stderr = YES
169
170 [remote]
171 type = imap
172 host = 127.0.0.1
173 port = 10143
174 STARTTLS = no
175 username = user
176 password = $PASSWORD
177 EOF
178
179 cat >"$ROOTDIR/dev/shm/nobody/.offlineimaprc" <<-EOF
180 [general]
181 accounts = bench
182
183 [Account bench]
184 localrepository = local
185 remoterepository = remote
186
187 [Repository local]
188 type = Maildir
189 localfolders = ~/Maildir2
190
191 [Repository remote]
192 type = IMAP
193 remotehost = 127.0.0.1
194 remotepass = $PASSWORD
195 remoteport = 10144
196 remoteuser = user
197 ssl = no
198 starttls = no
199 # keep the default (no) as it doesn't seem to work with large mailboxes, perhaps
200 # due to https://dovecot.org/pipermail/dovecot/2019-November/117522.html
201 #usecompression = yes
202 EOF
203
204 # install interimap's development version
205 install -oroot -groot -m0755 -Dt "$ROOTDIR/usr/bin" \
206 ./interimap ./benchmark/random_maildir.pl
207 sed -ri "0,/^(use\\s+\lib\\s+)([\"'])[^\"']*\\2\\s*;/ s||#&|" -- "$ROOTDIR/usr/bin/interimap"
208 install -oroot -groot -m0644 -DT \
209 ./lib/Net/IMAP/InterIMAP.pm "$ROOTDIR/usr/share/perl5/Net/IMAP/InterIMAP.pm"
210
211 # create a random mail store at mdbox:~/mail.back
212 prepare() {
213 local u="remote" d m n seqs
214 local maildir="/dev/shm/vmail/user/maildir"
215
216 clear
217 jail rm -rf -- "$maildir" "/dev/shm/vmail/user/mail.back"
218
219 for m in "${!MAILBOXES[@]}"; do
220 [ "${m^^[a-z]}" = "INBOX" ] && d="$maildir" || d="$maildir/.$m"
221 jail mkdir -p -- "$d"
222 # create 20% more; will be deleted afterwards (having only
223 # contiguous UIDs might bias the metrics)
224 n="${MAILBOXES["$m"]}"
225 jail random_maildir.pl "$d" $((n+n/5))
226 done
227
228 # convert to mdbox
229 jail doveadm -c"/dev/shm/dovecot/config" -omail_driver="maildir" -omail_path="~/maildir" \
230 sync --no-userdb-lookup "mdbox:~/mail.back"
231 jail rm -rf -- "$maildir"
232
233 # expunge 20% and purge
234 for m in "${!MAILBOXES[@]}"; do
235 n="${MAILBOXES["$m"]}"
236 seqs="$(shuf -n $((n/5)) -i"1-$n")"
237 jail doveadm -c"/dev/shm/dovecot/config" -omail_driver="mdbox" -omail_path="~/mail.back" \
238 expunge --no-userdb-lookup mailbox "$m" "${seqs//$'\n'/,}"
239 done
240 jail doveadm -c"/dev/shm/dovecot/config" -omail_driver="mdbox" -omail_path="~/mail.back" \
241 purge --no-userdb-lookup
242 }
243
244 # populate a clientn from backup mailstore mdbox:~/mail.back (copied to
245 # avoid recreating / conversion)
246 populate() {
247 local m u="remote" cmd
248 clear
249
250 if [ "${KEEP_BACKUP:-y}" = "n" ]; then
251 jail mv -T "/dev/shm/vmail/user/mail.back" "/dev/shm/vmail/user/mail"
252 else
253 jail cp -aT "/dev/shm/vmail/user/mail.back" "/dev/shm/vmail/user/mail"
254 fi
255
256 # force dovecot to index and compute the state, otherwise the first
257 # thing to query might be disadvantaged
258 jail doveadm -c"/dev/shm/dovecot/config" index --no-userdb-lookup "INBOX" 2>/dev/null
259 jail doveadm -c"/dev/shm/dovecot/config" mailbox status --no-userdb-lookup "all" "*" >/dev/null
260
261 u="local"
262 # initial configuration
263 for cmd in "$@"; do
264 case "$cmd" in
265 interimap) jail interimap --quiet 2>/dev/null;;
266 offlineimap) jail offlineimap -u quiet 2>/dev/null;;
267 *) exit 1;;
268 esac
269 done
270 }
271
272 # remove interimap / offlineimap database and mail store (but keep
273 # mdbox:~/mail.back)
274 clear() {
275 jail rm -rf -- \
276 "/dev/shm/vmail/user/mail" \
277 "/dev/shm/nobody/.local/share/interimap/bench.db" \
278 "/dev/shm/nobody/.offlineimap" \
279 "/dev/shm/nobody/Maildir" \
280 "/dev/shm/nobody/Maildir2"
281 }
282
283 # pretty print a number in k/M/G/T etc
284 _units() {
285 local n=$(( $1 )) b="$2" s u=""
286 [ $b -eq 1024 ] && s="i" || s=""
287 while [ ${#n} -gt 4 ]; do
288 case "$u" in
289 "") u="k";;
290 k) u="M";;
291 M) u="G";;
292 G) u="T";;
293 *) break;;
294 esac
295 n=$((n/b))
296 done
297 printf "%d%s" "$n" "${u:+$u$s}"
298 }
299 bytes() { printf "%sB" "$(_units "$1" 1024)"; }
300
301 # generate and deliver a random message
302 sample_message() {
303 local date="$(date +"%s.%N")"
304 cat <<-EOF
305 From: <[email protected]>
306 To: <[email protected]>
307 Date: $(date -R -d@"$date")
308 Message-ID: <[email protected]>
309
310 EOF
311 xxd -ps -c30 -l2048 /dev/urandom # 4165 bytes
312 }
313 deliver() {
314 local m="$1" u="remote"
315 jail doveadm -c"/dev/shm/dovecot/config" exec dovecot-lda -e -m "$m"
316 }
317
318 # write down markdown title
319 title() {
320 local x="$1" h="$2"
321 printf "\\n%s\\n%s\\n" "$h" "${h//?/$x}"
322 }
323
324 # run benchmark for `interimap` / `offlineimap -q` / `offlineimap`:
325 # populate, run optional actions (such as delivery), then sync again in
326 # a monitored fashion
327 run-all() {
328 local a cmd u q="" NAME=""
329 for cmd in "interimap" "offlineimap -q" "offlineimap"; do
330 populate "${cmd%% *}"
331 case "${cmd%% *}" in
332 interimap) q="--quiet";;
333 offlineimap) q="-u quiet";;
334 *) exit 1;
335 esac
336 for a in "$@"; do "$a"; done
337 NAME="$cmd" jail_stat $cmd $q 2>/dev/null
338 done
339 }
340
341
342 echo; echo
343 title "=" "Single mailbox"
344
345 cat <<-EOF
346
347 We create a mailbox on the remote server, populate it with a number of
348 messages, and synchronize it locally. We then collect metrics for no-op
349 synchronization (i.e., of mailboxes that are already in sync), and
350 reconciliation after receiving a *single* message on the remote server.
351 EOF
352
353 # generate a message to be used in *all* "Single mailbox" tests
354 sample_message >"$ROOTDIR/tmp/msg1"
355 activity1() {
356 deliver "inbox" <"$ROOTDIR/tmp/msg1"
357 }
358
359
360 declare -A MAILBOXES
361 for n in 100 1000 10000 100000; do
362 title "-" "$n messages"
363 MAILBOXES=( ["inbox"]="$n" )
364 prepare
365
366 printf "\\n### %s ###\\n\\n" "No-op (in sync)"
367 headers
368 run-all
369
370 printf "\\n### %s ###\\n\\n" "Reconciliation"
371 headers
372 run-all activity1
373 done
374
375
376 m=75
377 echo; echo
378 title "=" "$m mailboxes"
379
380 cat <<-EOF
381
382 We create $m mailboxes on the remote server, populate them with an equal
383 number of messages, and synchronize them locally. We then collect
384 metrics for no-op synchronization (i.e., of mailboxes that are already
385 in sync), and reconciliation after the following changes are being
386 applied to the remote server:
387
388 - 3 *new* messages (two on mailbox #2, one on mailbox #3); and
389 - 5 existing messages *EXPUNGEd* (two on mailboxes #3 and #4, one on
390 mailbox #5).
391 EOF
392
393 # generate more messages to be used in *all* "$m mailboxes" tests
394 sample_message >"$ROOTDIR/tmp/msg2"
395 sample_message >"$ROOTDIR/tmp/msg3"
396
397 activity2() {
398 local u="remote"
399 deliver "mailbox2" <"$ROOTDIR/tmp/msg1"
400 deliver "mailbox2" <"$ROOTDIR/tmp/msg2"
401 deliver "mailbox3" <"$ROOTDIR/tmp/msg3"
402 # intentionally modify the remote only because not all local backend speak IMAP
403 jail doveadm -c"/dev/shm/dovecot/config" expunge --no-userdb-lookup mailbox "mailbox3" "1:2"
404 jail doveadm -c"/dev/shm/dovecot/config" expunge --no-userdb-lookup mailbox "mailbox4" "1,3"
405 jail doveadm -c"/dev/shm/dovecot/config" expunge --no-userdb-lookup mailbox "mailbox5" "*"
406 }
407
408 for n in 100 1000 10000; do
409 title "-" "$n messages per mailbox"
410
411 MAILBOXES=( ["inbox"]="$n" )
412 for ((i=2; i<=$m; i++)); do
413 MAILBOXES["mailbox$i"]="$n"
414 done
415 prepare
416
417 printf "\\n### %s ###\\n\\n" "No-op (in sync)"
418 headers
419 run-all
420
421 printf "\\n### %s ###\\n\\n" "Reconciliation"
422 headers
423 run-all activity2
424 done
425
426
427 title "=" "Live synchronization"
428 timeout=$((6 * 3600))
429 step=5
430
431 MAILBOXES=( ["inbox"]=100000 ["xlarge"]=100000 )
432 for ((i=0; i<10; i++)); do
433 MAILBOXES["large$i"]=10000
434 done
435 for ((i=0; i<20; i++)); do
436 MAILBOXES["medium$i"]=5000
437 done
438 for ((i=0; i<45; i++)); do
439 MAILBOXES["small$i"]=2000
440 done
441 for ((i=0; i<20; i++)); do
442 MAILBOXES["xsmall$i"]=500
443 done
444
445 n=0
446 for i in "${MAILBOXES[@]}"; do
447 n=$(( n + i ))
448 done
449
450 cat <<-EOF
451
452 ${#MAILBOXES[@]} mailboxes, $n messages in total:
453
454 - 2 with 100000 messages;
455 - 10 with 10000 messages;
456 - 20 with 5000 messages;
457 - 45 with 2000 messages; and
458 - 20 with 500 messages.
459
460 The two local mail stores (respectively for [InterIMAP] and
461 [OfflineIMAP]) are initially in sync with the remote server, and we keep
462 long-running “autorefresh” synchronization processes alive for 6h, with
463 updates being regularly applied to the remote server: every $step seconds,
464
465 - a new message is delivered to a random mailbox with 5% probability
466 (once every $((20*step))s on average);
467 - a random message is EXPUNGEd with 5% probability (once every $((20*step))s on
468 average); and
469 - a random message is marked as seen with 10% probability (once every
470 $((10*step))s on average).
471
472 \`interimap\` is configured to sync every *30s*. \`offlineimap\` is
473 configured to quick sync very *30s*, with a regular sync every *1h*.
474
475 EOF
476
477 IDLE="y" headers
478 prepare
479 KEEP_BACKUP="n" populate "interimap" "offlineimap"
480
481 IDLE="y" jail_stat interimap --quiet --watch=30 2>/dev/null &
482 IDLE="y" jail_stat offlineimap -u quiet -k "Account_bench:autorefresh=0.5" \
483 -k "Account_bench:quick=120" 2>/dev/null &
484
485 u="remote"
486 timeout=$(( $(date +%s) + timeout ))
487 while [ $(date +%s) -lt $timeout ]; do
488 n="$(shuf -n1 -i1-100)"
489 if [ $n -le 5 ]; then
490 # deliver to a random mailbox on the remote
491 m="$(shuf -n1 -e -- "${!MAILBOXES[@]}")"
492 sample_message | deliver "$m"
493 fi
494 n="$(shuf -n1 -i1-100)"
495 if [ $n -le 5 ]; then
496 # expunge a random message on the remote
497 read guid uid < <(jail doveadm -c"/dev/shm/dovecot/config" search --no-userdb-lookup all | shuf -n1)
498 jail doveadm -c"/dev/shm/dovecot/config" expunge --no-userdb-lookup mailbox-guid "$guid" uid "$uid"
499 fi
500 n="$(shuf -n1 -i1-100)"
501 if [ $n -le 10 ]; then
502 # mark a random message as seen
503 read guid uid < <(jail doveadm -c"/dev/shm/dovecot/config" search --no-userdb-lookup all | shuf -n1)
504 jail doveadm -c"/dev/shm/dovecot/config" flags add --no-userdb-lookup "\\Seen" mailbox-guid "$guid" uid "$uid"
505 fi
506 sleep $step
507 done
508
509 jail pkill -TERM -u"nobody" -s0 interimap
510 sleep 0.2 # give a chance to print the stats
511 jail pkill -SIGABRT -u"nobody" -s0 offlineimap
512 wait