Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/test_massops.py
blob: b14d7ac0e4919e9d09a74577523b61026a72c599 (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
#!/usr/bin/env python
"""Large number of operations intended for measuring performance."""

import dbus
import decorator
import os
import tempfile
import time
import unittest


DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore'
DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
DS_DBUS_PATH = '/org/laptop/sugar/DataStore'
NUM_RUNS = int(os.environ.get('MASSOPS_RUNS', '100'))
IGNORE_PROPERTIES = [
    'checksum',
    'creation_time',
    'number',
    'timestamp',
    'uid',
    'version_id',
]


@decorator.decorator
def repeat(func, *args, **kwargs):
    """Run the decorated function NUM_RUNS times."""
    for i_ in range(NUM_RUNS):
        func(*args, **kwargs)


class MassOpsTestCase(unittest.TestCase):
    """Large number of operations intended for measuring performance."""

    def setUp(self):
        # pylint: disable=C0103
        self._bus = dbus.SessionBus()
        self._datastore = dbus.Interface(self._bus.get_object(DS_DBUS_SERVICE,
                                         DS_DBUS_PATH), DS_DBUS_INTERFACE)

    _create_properties = {
        'title': 'DS test object',
        'mime_type': 'text/plain',
        'activity': 'org.sugarlabs.DataStoreTest1',
    }
    _create_content = 'Foo bar\n' * 1000

    def test_create(self):
        """Run create() lots of times to create new objects."""
        for i in range(NUM_RUNS):
            content_file = tempfile.NamedTemporaryFile()
            content_file.write(self._create_content)
            content_file.flush()
            properties = self._create_properties.copy()
            properties['number'] = str(i)
            properties['timestamp'] = time.time()
            self._datastore.create(properties, content_file.name, False)
            content_file.close()

    @repeat
    def test_find_all(self):
        """Run find() to list all entries."""
        entries, total_count = self._datastore.find({}, ['number'],
                                                    byte_arrays=True)
        self.assertEquals(total_count, NUM_RUNS)
        self.assertEquals(total_count, len(entries))
        for position, entry in enumerate(entries):
            self.assertEquals(int(entry['number']), NUM_RUNS - position - 1)

    @repeat
    def test_find_all_reverse_time(self):
        """Run find() to list all entries in reverse chronological order."""
        entries, total_count = \
            self._datastore.find({'order_by': ['-timestamp']}, ['number'],
                                 byte_arrays=True)
        self.assertEquals(total_count, NUM_RUNS)
        self.assertEquals(total_count, len(entries))
        for position, entry in enumerate(entries):
            self.assertEquals(int(entry['number']), position)

    @repeat
    def test_find_all_title(self):
        """Run find() to list all entries ordered by title."""
        entries, total_count = \
            self._datastore.find({'order_by': ['+title']}, ['tree_id'],
                                 byte_arrays=True)
        self.assertEquals(total_count, NUM_RUNS)
        self.assertEquals(total_count, len(entries))

    @repeat
    def test_find_all_reverse_title(self):
        """Run find() to list all entries ordered by title (reversed)."""
        entries, total_count = \
            self._datastore.find({'order_by': ['-title']}, ['tree_id'],
                                 byte_arrays=True)
        self.assertEquals(total_count, NUM_RUNS)
        self.assertEquals(total_count, len(entries))

    @repeat
    def test_find_all_chunked(self):
        """Run find() to list all entries in small chunks."""
        chunk_size = 30
        for chunk_start in range(0, NUM_RUNS, 30):
            entries, total_count = \
                self._datastore.find({'offset': chunk_start,
                                      'limit': chunk_size}, ['number'],
                                     byte_arrays=True)
            self.assertEquals(len(entries),
                              min(chunk_size, NUM_RUNS - chunk_start))
            self.assertEquals(total_count, NUM_RUNS)
            for position, entry in enumerate(entries):
                self.assertEquals(int(entry['number']),
                                  NUM_RUNS - (chunk_start + position) - 1)

    def test_get_properties(self):
        """Run get_properties() on all entries and verify result."""
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            properties = \
                self._datastore.get_properties(entry['uid'], byte_arrays=True)
            self.assertEquals(properties.pop('filesize'),
                              str(len(self._create_content)))
            self._filter_properties(properties)
            self.assertEquals(properties, self._create_properties)

    def test_get_filename(self):
        """Run get_filename() on all entries and verify content."""
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            filename = self._datastore.get_filename(entry['uid'],
                                                    byte_arrays=True)
            try:
                self.assertEquals(file(filename).read(), self._create_content)
            finally:
                os.remove(filename)

    _update_properties = {
        'title': 'DS test object (updated)',
        'mime_type': 'text/plain',
        'activity': 'org.sugarlabs.DataStoreTest1',
    }
    _update_content = 'Foo bar baz\n' * 1000

    def test_update(self):
        """Update the content of all existing entries"""
        content_file = tempfile.NamedTemporaryFile()
        content_file.write(self._update_content)
        content_file.flush()
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            self._datastore.update(entry['uid'], self._update_properties,
                                   content_file.name, False)

    def test_update_verify(self):
        """Verify test_update() has changed content and metadata of all
        entries.
        """
        for entry in self._datastore.find({}, [], byte_arrays=True)[0]:
            filename = self._datastore.get_filename(entry['uid'],
                                                    byte_arrays=True)
            self.assertEquals(entry.pop('filesize'),
                              str(len(self._update_content)))
            self._filter_properties(entry)
            try:
                self.assertEquals(entry, self._update_properties)
                self.assertEquals(file(filename).read(), self._update_content)
            finally:
                os.remove(filename)

    def _filter_properties(self, properties):
        for key in IGNORE_PROPERTIES:
            properties.pop(key, None)


def suite():
    test_suite = unittest.TestLoader().loadTestsFromTestCase(MassOpsTestCase)
    test_suite.__doc__ = MassOpsTestCase.__doc__
    return test_suite