summaryrefslogtreecommitdiff
path: root/tests/benchmarks/test_legacy_vs_blobs.py
blob: 47d6482ca492b62b1423abbd43f98df846d10e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# "Legacy" versus "Incoming blobs" pipeline comparison
# ====================================================
#
# This benchmarking aims to compare the legacy and new mail incoming pipeline,
# to asses performance improvements brought by the introduction of blobs.
#
# We use the following sizes in these tests:
#
#   - headers:  4   KB
#   - metadata: 0.1 KB
#   - flags:    0.5 KB
#   - content:  variable
#
# "Legacy" incoming mail pipeline:
#
#   - email arrives at MX.
#   - MX encrypts to public key and puts into couch.
#   - pubkey encrypted doc is synced to soledad client as "incoming".
#   - bitmask mail processes "incoming" and generates 3 metadocs + 1 payload
#     doc per message.
#   - soledad client syncs 4 documents back to server.
#
# "Incoming blobs" mail pipeline:
#
#   - email arrives at MX.
#   - MX encyrpts to public key and puts into soledad server.
#   - soledad server writes a blob to filesystem.
#   - soledad client gets the incoming blob from server and generates 3
#     metadocs + 1 blob.
#   - soledad client syncs 3 meta documents and 1 blob back to server.
#
# Some notes about the tests in this file:
#
#   - This is a simulation of the legacy and new incoming mail pipelines.
#     There is no actual mail processing operation done (i.e. no pubkey crypto,
#     no mail parsing), only usual soledad document manipulation and sync (with
#     local 1network and crypto).
#
#   - Each test simulates a whole incoming mail pipeline, including get new
#     incoming messages from server, create new documents that represent the
#     parsed message, and synchronize those back to the server.
#
#   - These tests are disabled by default because it doesn't make much sense to
#     have them run automatically for all commits in the repository. Instead,
#     we will run them manually for specific releases and store results and
#     analisys in a subfolder.

import base64
import pytest
import random
import sys
import treq
import uuid

from io import BytesIO

from twisted.internet.defer import gatherResults
from twisted.internet.defer import returnValue
from twisted.internet.defer import DeferredSemaphore

from leap.soledad.common.blobs import Flags
from leap.soledad.client._db.blobs import BlobDoc


def payload(size):
    random.seed(1337)  # same seed to avoid different bench results
    payload_bytes = bytearray(random.getrandbits(8) for _ in xrange(size))
    # encode as base64 to avoid ascii encode/decode errors
    return base64.b64encode(payload_bytes)[:size]  # remove b64 overhead


PARTS = {
    'headers': payload(4000),
    'metadata': payload(100),
    'flags': payload(500),
}


#
# "Legacy" incoming mail pipeline.
#

@pytest.inlineCallbacks
def load_up_legacy(client, amount, content):
    # make sure there are no document from previous runs
    yield client.sync()
    _, docs = yield client.get_all_docs()
    deferreds = []
    for doc in docs:
        d = client.delete_doc(doc)
        deferreds.append(d)
    yield gatherResults(deferreds)
    yield client.sync()

    # create a bunch of local documents representing email messages
    deferreds = []
    for i in xrange(amount):
        deferreds.append(client.create_doc(content))
    yield gatherResults(deferreds)
    yield client.sync()


@pytest.inlineCallbacks
def process_incoming_docs(client, docs):
    deferreds = []
    for doc in docs:

        # create fake documents that represent message
        for name in PARTS.keys():
            d = client.create_doc({name: doc.content[name]})
            deferreds.append(d)

        # create one document with content
        key = 'content'
        d = client.create_doc({key: doc.content[key]})
        deferreds.append(d)

        # delete the old incoming document
        d = client.delete_doc(doc)
        deferreds.append(d)

    # wait for all operatios to succeed
    yield gatherResults(deferreds)


def create_legacy_test(amount, size):
    group = 'test_legacy_vs_blobs_%d_%dk' % (amount, (size / 1000))

    @pytest.inlineCallbacks
    @pytest.mark.skip(reason="avoid running for all commits")
    @pytest.mark.benchmark(group=group)
    def test(soledad_client, txbenchmark_with_setup):
        client = soledad_client()

        # setup the content of initial documents representing incoming emails
        content = {'content': payload(size), 'incoming': True}
        for name, data in PARTS.items():
            content[name] = data

        @pytest.inlineCallbacks
        def setup():
            yield load_up_legacy(client, amount, content)
            clean_client = soledad_client(force_fresh_db=True)
            yield clean_client.create_index('incoming', 'bool(incoming)')
            returnValue(clean_client)

        @pytest.inlineCallbacks
        def legacy_pipeline(client):
            yield client.sync()
            docs = yield client.get_from_index('incoming', '1')
            yield process_incoming_docs(client, docs)
            yield client.sync()

        yield txbenchmark_with_setup(setup, legacy_pipeline)
    return test


# ATTENTION: update the documentation in ../docs/benchmarks.rst if you change
# the number of docs or the doc sizes for the tests below.
test_legacy_10_1000k = create_legacy_test(10, 1000 * 1000)
test_legacy_100_100k = create_legacy_test(100, 100 * 1000)
test_legacy_1000_10k = create_legacy_test(1000, 10 * 1000)


#
# "Incoming blobs" mail pipeline:
#

# used to limit the amount of concurrent accesses to the blob manager
semaphore = DeferredSemaphore(2)


# deliver data to a user by using the incoming api at given url.
def deliver_using_incoming_api(url, user_uuid, token, data):
    auth = 'Token %s' % base64.b64encode('%s:%s' % (user_uuid, token))
    uri = "%s/incoming/%s/%s?namespace=MX" % (url, user_uuid, uuid.uuid4().hex)
    return treq.put(uri, headers={'Authorization': auth}, data=BytesIO(data))


# deliver data to a user by faking incoming using blobs
@pytest.inlineCallbacks
def deliver_using_blobs(client, fd):
    # put
    blob_id = uuid.uuid4().hex
    doc = BlobDoc(fd, blob_id=blob_id)
    size = sys.getsizeof(fd)
    yield client.blobmanager.put(doc, size, namespace='MX')
    # and flag
    flags = [Flags.PENDING]
    yield client.blobmanager.set_flags(blob_id, flags, namespace='MX')


def reclaim_free_space(client):
    return client.blobmanager.local.dbpool.runQuery("VACUUM")


@pytest.inlineCallbacks
def load_up_blobs(client, amount, data):
    # make sure there are no document from previous runs
    yield client.sync()
    _, docs = yield client.get_all_docs()
    deferreds = []
    for doc in docs:
        d = client.delete_doc(doc)
        deferreds.append(d)
    yield gatherResults(deferreds)
    yield client.sync()

    # delete all payload from blobs db and server
    for namespace in ['MX', 'payload']:
        ids = yield client.blobmanager.remote_list(namespace=namespace)
        deferreds = []
        for blob_id in ids:
            d = semaphore.run(
                client.blobmanager.delete, blob_id, namespace=namespace)
            deferreds.append(d)
    yield gatherResults(deferreds)

    # create a bunch of incoming blobs
    deferreds = []
    for i in xrange(amount):
        # choose method of delivery based in test being local or remote
        if '127.0.0.1' in client.server_url:
            fun = deliver_using_incoming_api
            args = (client.server_url, client.uuid, client.token, data)
        else:
            fun = deliver_using_blobs
            args = (client, BytesIO(data))
        d = semaphore.run(fun, *args)
        deferreds.append(d)
    yield gatherResults(deferreds)

    # empty local blobs db
    yield client.blobmanager.local.dbpool.runQuery(
        "DELETE FROM blobs WHERE 1;")
    yield reclaim_free_space(client)


@pytest.inlineCallbacks
def process_incoming_blobs(client, pending):
    # process items
    deferreds = []
    for item in pending:
        d = process_one_incoming_blob(client, item)
        deferreds.append(d)
    yield gatherResults(deferreds)


@pytest.inlineCallbacks
def process_one_incoming_blob(client, item):
    fd = yield semaphore.run(
        client.blobmanager.get, item, namespace='MX')

    # create metadata docs
    deferreds = []
    for name, data in PARTS.items():
        d = client.create_doc({name: data})
        deferreds.append(d)

    # put the incoming blob as it would be done after mail processing
    doc = BlobDoc(fd, blob_id=uuid.uuid4().hex)
    size = sys.getsizeof(fd)
    d = semaphore.run(
        client.blobmanager.put, doc, size, namespace='payload')
    deferreds.append(d)
    yield gatherResults(deferreds)

    # delete incoming blob
    yield semaphore.run(
        client.blobmanager.delete, item, namespace='MX')


def create_blobs_test(amount, size):
    group = 'test_legacy_vs_blobs_%d_%dk' % (amount, (size / 1000))

    @pytest.inlineCallbacks
    @pytest.mark.skip(reason="avoid running for all commits")
    @pytest.mark.benchmark(group=group)
    def test(soledad_client, txbenchmark_with_setup):
        client = soledad_client()
        blob_payload = payload(size)

        @pytest.inlineCallbacks
        def setup():
            yield load_up_blobs(client, amount, blob_payload)
            returnValue(soledad_client(force_fresh_db=True))

        @pytest.inlineCallbacks
        def blobs_pipeline(client):
            pending = yield client.blobmanager.remote_list(
                namespace='MX', filter_flags=Flags.PENDING)
            yield process_incoming_blobs(client, pending)
            # reclaim_free_space(client)
            yield client.sync()
            yield client.blobmanager.send_missing(namespace='payload')

        yield txbenchmark_with_setup(setup, blobs_pipeline)
    return test


# ATTENTION: update the documentation in ../docs/benchmarks.rst if you change
# the number of docs or the doc sizes for the tests below.
test_blobs_10_1000k = create_blobs_test(10, 1000 * 1000)
test_blobs_100_100k = create_blobs_test(100, 100 * 1000)
test_blobs_1000_10k = create_blobs_test(1000, 10 * 1000)