Is it worthwhile using IMAP COMPRESS (DEFLATE)?

2019-07-15 11:53发布

Gmail supports the IMAP COMPRESS Extension (RFC4978), specifically the DEFLATE algorithm (RFC1951) aka zlib/gzip.

I'm not normally a Python programmer but I threw a quick test script together using Piers Lauder's imaplib2 to determine performance with or without compression enabled.

from time import time
import imaplib2, string

def cb((response, cb_arg, error)):
        typ, data = response
        #print 'Message %s\n%s\n' % (cb_arg, data[0][5])

IMAP_SERVER='imap.gmail.com'
IMAP_PORT=993
IMAP_USERNAME='*********'
IMAP_PASSWORD='*********'

def gogmail(compress):
    start = time()
    M = imaplib2.IMAP4_SSL(IMAP_SERVER, IMAP_PORT, debug=0)
    M.login(IMAP_USERNAME, IMAP_PASSWORD)
    if(compress):
        M.enable_compression()
    M.SELECT(readonly=True)
    typ, data = M.SEARCH(None, 'ALL')
    fetch_list = string.split(data[0])[-100:]
    for num in fetch_list:
        M.FETCH(num, '(RFC822)', callback=cb, cb_arg=num)   
    M.LOGOUT()
    end = time()
    print end - start

print 'Compressed  '
print '------------'

for x in range(0, 50):
    gogmail(1)

print 'Uncompressed'
print '------------'

for x in range(0, 50):
    gogmail(0)

If I have made a glaring newbie error in my Python code please correct me.

I've run this test script a couple of times. Sometimes the mean average of compressed accesses is faster, sometimes not. There is never very much difference in mean average and a great deal of variation in access times (a single inbox access of 100 messages can take anywhere between 4 and 17 seconds). Consistent results would make my decision easier! Access is via SSL I was thinking that maybe there is some inherent compression in that (I don't know).

Do you think it is worthwhile using compression when accessing Gmail IMAP?

Incidentally, I would like to use JavaMail (rather than Python) but I understand I would need to customise JavaMail significantly to support compression (maybe using Jessie). Has somebody done this already? Would it be worthwhile?

I appreciate the feedback. Many Thanks.

1条回答
beautiful°
2楼-- · 2019-07-15 12:27

I don't think enabling compression will make a great difference, simply because 100 messages is a tiny amount of data. Suppose one message is 1KB (a long plaintext message), so that your inbox is 100KB. Let's assume a compression ratio of 5:1 (which is unlikely at best), so that the compressed download is now 20KB.

Any sane internet connection these days runs at least at 1Mbps, or 125KB/s. So under these way-overestimated assumptions, you'd save somewhere under a second of data transmission costs. This is being dwarfed by the connection lag and the processing on gmail's side.

On the other hand, what do you lose by enabling compression?

查看更多
登录 后发表回答