File: test_performance.py

package info (click to toggle)
xandikos 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,552 kB
  • sloc: python: 17,496; xml: 583; sh: 343; makefile: 64
file content (288 lines) | stat: -rw-r--r-- 10,975 bytes parent folder | download
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
# Xandikos
# Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>, et al.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3
# of the License or (at your option) any later version of
# the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.

"""Performance tests for large collections (RFC 4918, RFC 4791, RFC 6352).

These tests ensure that Xandikos performs adequately with large collections
of calendar events and contacts.
"""

import time
import unittest
from datetime import datetime, timedelta, timezone

from xandikos.icalendar import CalendarFilter, ICalendarFile
from xandikos.vcard import CardDAVFilter, VCardFile


class LargeCollectionPerformanceTests(unittest.TestCase):
    """Performance tests for large collections."""

    def setUp(self):
        """Set up large test collections."""
        # Create a large CalDAV calendar (500 events)
        self.large_calendar_events = []
        base_date = datetime(2025, 1, 1, 10, 0, 0, tzinfo=timezone.utc)

        for i in range(500):
            event_date = base_date + timedelta(days=i)
            event = ICalendarFile(
                [
                    b"BEGIN:VCALENDAR\r\n",
                    b"VERSION:2.0\r\n",
                    b"PRODID:-//Test//Test//EN\r\n",
                    b"BEGIN:VEVENT\r\n",
                    f"UID:event-{i}@test.example.com\r\n".encode(),
                    f"DTSTART:{event_date.strftime('%Y%m%dT%H%M%SZ')}\r\n".encode(),
                    f"DTEND:{(event_date + timedelta(hours=1)).strftime('%Y%m%dT%H%M%SZ')}\r\n".encode(),
                    f"SUMMARY:Event {i}\r\n".encode(),
                    b"END:VEVENT\r\n",
                    b"END:VCALENDAR\r\n",
                ],
                "text/calendar",
            )
            self.large_calendar_events.append((f"event-{i}.ics", event))

        # Create a large CardDAV addressbook (500 contacts)
        self.large_addressbook_contacts = []
        for i in range(500):
            vcard = VCardFile(
                [
                    b"BEGIN:VCARD\r\n",
                    b"VERSION:3.0\r\n",
                    f"FN:Contact {i}\r\n".encode(),
                    f"N:Contact{i};Test;;;\r\n".encode(),
                    f"EMAIL:contact{i}@example.com\r\n".encode(),
                    f"TEL:+1-555-{i:04d}\r\n".encode(),
                    b"END:VCARD\r\n",
                ],
                "text/vcard",
            )
            self.large_addressbook_contacts.append((f"contact-{i}.vcf", vcard))

    def test_caldav_filter_performance_no_match(self):
        """Test CalDAV filter performance on large calendar (no matches).

        This tests worst-case performance where the filter must check
        all events but finds no matches.
        """
        filter_obj = CalendarFilter(timezone.utc)
        comp_filter = filter_obj.filter_subcomponent("VCALENDAR").filter_subcomponent(
            "VEVENT"
        )
        comp_filter.filter_property("SUMMARY").filter_text_match(
            "NonexistentEvent", collation="i;unicode-casemap"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, event in self.large_calendar_events
            if filter_obj.check(name, event)
        ]
        elapsed = time.perf_counter() - start_time

        self.assertEqual(0, len(matches))
        # Should complete in under 5 seconds for 500 events
        self.assertLess(elapsed, 5.0, f"Filter took {elapsed:.2f}s, expected < 5s")

    def test_caldav_filter_performance_some_matches(self):
        """Test CalDAV filter performance with partial matches.

        This tests typical performance where some events match the filter.
        """
        filter_obj = CalendarFilter(timezone.utc)
        comp_filter = filter_obj.filter_subcomponent("VCALENDAR").filter_subcomponent(
            "VEVENT"
        )
        # Match events with "Event 1" in summary (Event 1, Event 10-19, Event 100-199)
        comp_filter.filter_property("SUMMARY").filter_text_match(
            "Event 1", collation="i;unicode-casemap"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, event in self.large_calendar_events
            if filter_obj.check(name, event)
        ]
        elapsed = time.perf_counter() - start_time

        # Should find "Event 1", "Event 10"-"Event 19", "Event 100"-"Event 199"
        self.assertGreater(len(matches), 100)
        self.assertLess(elapsed, 5.0, f"Filter took {elapsed:.2f}s, expected < 5s")

    def test_caldav_time_range_filter_performance(self):
        """Test CalDAV time-range filter performance.

        This tests performance of time-range queries, which are common
        in calendar applications.
        """
        filter_obj = CalendarFilter(timezone.utc)
        comp_filter = filter_obj.filter_subcomponent("VCALENDAR").filter_subcomponent(
            "VEVENT"
        )
        # Query for events in January 2025 (first 31 events)
        start = datetime(2025, 1, 1, tzinfo=timezone.utc)
        end = datetime(2025, 2, 1, tzinfo=timezone.utc)
        comp_filter.filter_time_range(start, end)

        start_time = time.perf_counter()
        matches = [
            name
            for name, event in self.large_calendar_events
            if filter_obj.check(name, event)
        ]
        elapsed = time.perf_counter() - start_time

        # Should find 31 events (January has 31 days)
        self.assertEqual(31, len(matches))
        self.assertLess(elapsed, 5.0, f"Filter took {elapsed:.2f}s, expected < 5s")

    def test_carddav_filter_performance_no_match(self):
        """Test CardDAV filter performance on large addressbook (no matches).

        This tests worst-case performance where the filter must check
        all contacts but finds no matches.
        """
        filter_obj = CardDAVFilter()
        filter_obj.add_property_filter("FN").add_text_match(
            "NonexistentContact", match_type="contains"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, vcard in self.large_addressbook_contacts
            if filter_obj.check(name, vcard)
        ]
        elapsed = time.perf_counter() - start_time

        self.assertEqual(0, len(matches))
        # Should complete in under 2 seconds for 500 contacts
        self.assertLess(elapsed, 2.0, f"Filter took {elapsed:.2f}s, expected < 2s")

    def test_carddav_filter_performance_some_matches(self):
        """Test CardDAV filter performance with partial matches.

        This tests typical performance where some contacts match the filter.
        """
        filter_obj = CardDAVFilter()
        # Match contacts with "Contact 1" in FN
        filter_obj.add_property_filter("FN").add_text_match(
            "Contact 1", match_type="contains"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, vcard in self.large_addressbook_contacts
            if filter_obj.check(name, vcard)
        ]
        elapsed = time.perf_counter() - start_time

        # Should find "Contact 1", "Contact 10"-"Contact 19", "Contact 100"-"Contact 199"
        self.assertGreater(len(matches), 100)
        self.assertLess(elapsed, 2.0, f"Filter took {elapsed:.2f}s, expected < 2s")

    def test_carddav_email_filter_performance(self):
        """Test CardDAV email filter performance.

        This tests performance of email-based queries, which are common
        in addressbook applications.
        """
        filter_obj = CardDAVFilter()
        # Match contacts with specific email domain
        filter_obj.add_property_filter("EMAIL").add_text_match(
            "example.com", match_type="contains"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, vcard in self.large_addressbook_contacts
            if filter_obj.check(name, vcard)
        ]
        elapsed = time.perf_counter() - start_time

        # All contacts should match (all have @example.com)
        self.assertEqual(500, len(matches))
        self.assertLess(elapsed, 2.0, f"Filter took {elapsed:.2f}s, expected < 2s")

    def test_caldav_complex_filter_performance(self):
        """Test CalDAV complex filter with multiple conditions.

        This tests performance of complex queries with multiple filter
        conditions combined.
        """
        filter_obj = CalendarFilter(timezone.utc)
        filter_obj.test = all  # AND logic
        comp_filter = filter_obj.filter_subcomponent("VCALENDAR").filter_subcomponent(
            "VEVENT"
        )

        # Multiple conditions: text match + time range
        comp_filter.filter_property("SUMMARY").filter_text_match(
            "Event", collation="i;unicode-casemap"
        )
        start = datetime(2025, 1, 1, tzinfo=timezone.utc)
        end = datetime(2025, 2, 1, tzinfo=timezone.utc)
        comp_filter.filter_time_range(start, end)

        start_time = time.perf_counter()
        matches = [
            name
            for name, event in self.large_calendar_events
            if filter_obj.check(name, event)
        ]
        elapsed = time.perf_counter() - start_time

        # Should find 31 events in January
        self.assertEqual(31, len(matches))
        self.assertLess(elapsed, 5.0, f"Filter took {elapsed:.2f}s, expected < 5s")

    def test_carddav_complex_filter_performance(self):
        """Test CardDAV complex filter with multiple conditions.

        This tests performance of complex queries with multiple filter
        conditions combined.
        """
        filter_obj = CardDAVFilter()
        filter_obj.test = all  # AND logic
        filter_obj.add_property_filter("EMAIL")
        filter_obj.add_property_filter("TEL")
        filter_obj.add_property_filter("FN").add_text_match(
            "Contact 1", match_type="contains"
        )

        start_time = time.perf_counter()
        matches = [
            name
            for name, vcard in self.large_addressbook_contacts
            if filter_obj.check(name, vcard)
        ]
        elapsed = time.perf_counter() - start_time

        # Should find contacts matching all conditions
        self.assertGreater(len(matches), 100)
        self.assertLess(elapsed, 2.0, f"Filter took {elapsed:.2f}s, expected < 2s")


if __name__ == "__main__":
    unittest.main()