python: Fix usage strings
[samba.git] / source4 / dsdb / tests / python / ad_dc_medley_performance.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 import optparse
5 import sys
6 sys.path.insert(0, 'bin/python')
7
8 import os
9 import samba
10 import samba.getopt as options
11 import random
12 import tempfile
13 import shutil
14 import time
15 import itertools
16
17 from samba.netcmd.main import cmd_sambatool
18
19 # We try to use the test infrastructure of Samba 4.3+, but if it
20 # doesn't work, we are probably in a back-ported patch and trying to
21 # run on 4.1 or something.
22 #
23 # Don't copy this horror into ordinary tests -- it is special for
24 # performance tests that want to apply to old versions.
25 try:
26     from samba.tests.subunitrun import SubunitOptions, TestProgram
27     ANCIENT_SAMBA = False
28 except ImportError:
29     ANCIENT_SAMBA = True
30     samba.ensure_external_module("testtools", "testtools")
31     samba.ensure_external_module("subunit", "subunit/python")
32     from subunit.run import SubunitTestRunner
33     import unittest
34
35 from samba.samdb import SamDB
36 from samba.auth import system_session
37 from ldb import Message, MessageElement, Dn, LdbError
38 from ldb import FLAG_MOD_ADD, FLAG_MOD_REPLACE, FLAG_MOD_DELETE
39 from ldb import SCOPE_BASE, SCOPE_SUBTREE, SCOPE_ONELEVEL
40 from ldb import ERR_NO_SUCH_OBJECT
41
42 parser = optparse.OptionParser("ad_dc_medley_performance.py [options] <host>")
43 sambaopts = options.SambaOptions(parser)
44 sambaopts.add_option("-p", "--use-paged-search", action="store_true",
45                      help="Use paged search module")
46
47 parser.add_option_group(sambaopts)
48 parser.add_option_group(options.VersionOptions(parser))
49
50 if not ANCIENT_SAMBA:
51     subunitopts = SubunitOptions(parser)
52     parser.add_option_group(subunitopts)
53
54 # use command line creds if available
55 credopts = options.CredentialsOptions(parser)
56 parser.add_option_group(credopts)
57 opts, args = parser.parse_args()
58
59 if len(args) < 1:
60     parser.print_usage()
61     sys.exit(1)
62
63 host = args[0]
64
65 lp = sambaopts.get_loadparm()
66 creds = credopts.get_credentials(lp)
67
68 random.seed(1)
69
70
71 class PerfTestException(Exception):
72     pass
73
74
75 BATCH_SIZE = 2000
76 LINK_BATCH_SIZE = 1000
77 DELETE_BATCH_SIZE = 50
78 N_GROUPS = 29
79
80
81 class GlobalState(object):
82     next_user_id = 0
83     n_groups = 0
84     next_linked_user = 0
85     next_relinked_user = 0
86     next_linked_user_3 = 0
87     next_removed_link_0 = 0
88     test_number = 0
89     active_links = set()
90
91
92 class UserTests(samba.tests.TestCase):
93
94     def add_if_possible(self, *args, **kwargs):
95         """In these tests sometimes things are left in the database
96         deliberately, so we don't worry if we fail to add them a second
97         time."""
98         try:
99             self.ldb.add(*args, **kwargs)
100         except LdbError:
101             pass
102
103     def setUp(self):
104         super(UserTests, self).setUp()
105         self.state = GlobalState  # the class itself, not an instance
106         self.lp = lp
107
108         kwargs = {}
109         if opts.use_paged_search:
110             kwargs["options"] = ["modules:paged_searches"]
111
112         self.ldb = SamDB(host, credentials=creds,
113                          session_info=system_session(lp), lp=lp, **kwargs)
114         self.base_dn = self.ldb.domain_dn()
115         self.ou = "OU=pid%s,%s" % (os.getpid(), self.base_dn)
116         self.ou_users = "OU=users,%s" % self.ou
117         self.ou_groups = "OU=groups,%s" % self.ou
118         self.ou_computers = "OU=computers,%s" % self.ou
119
120         self.state.test_number += 1
121         random.seed(self.state.test_number)
122
123     def tearDown(self):
124         super(UserTests, self).tearDown()
125
126     def test_00_00_do_nothing(self):
127         # this gives us an idea of the overhead
128         pass
129
130     def test_00_01_do_nothing_relevant(self):
131         # takes around 1 second on i7-4770
132         j = 0
133         for i in range(30000000):
134             j += i
135
136     def test_00_02_do_nothing_sleepily(self):
137         time.sleep(1)
138
139     def test_00_03_add_ous_and_groups(self):
140         # initialise the database
141         for dn in (self.ou,
142                    self.ou_users,
143                    self.ou_groups,
144                    self.ou_computers):
145             self.ldb.add({
146                 "dn": dn,
147                 "objectclass": "organizationalUnit"
148             })
149
150         for i in range(N_GROUPS):
151             self.ldb.add({
152                 "dn": "cn=g%d,%s" % (i, self.ou_groups),
153                 "objectclass": "group"
154             })
155
156         self.state.n_groups = N_GROUPS
157
158     def _add_users(self, start, end):
159         for i in range(start, end):
160             self.ldb.add({
161                 "dn": "cn=u%d,%s" % (i, self.ou_users),
162                 "objectclass": "user"
163             })
164
165     def _add_users_ldif(self, start, end):
166         lines = []
167         for i in range(start, end):
168             lines.append("dn: cn=u%d,%s" % (i, self.ou_users))
169             lines.append("objectclass: user")
170             lines.append("")
171         self.ldb.add_ldif('\n'.join(lines))
172
173     def _test_join(self):
174         tmpdir = tempfile.mkdtemp()
175         if '://' in host:
176             server = host.split('://', 1)[1]
177         else:
178             server = host
179         cmd = cmd_sambatool.subcommands['domain'].subcommands['join']
180         result = cmd._run("samba-tool domain join",
181                           creds.get_realm(),
182                           "dc", "-U%s%%%s" % (creds.get_username(),
183                                               creds.get_password()),
184                           '--targetdir=%s' % tmpdir,
185                           '--server=%s' % server)
186
187         shutil.rmtree(tmpdir)
188
189     def _test_unindexed_search(self):
190         expressions = [
191             ('(&(objectclass=user)(description='
192              'Built-in account for adminstering the computer/domain))'),
193             '(description=Built-in account for adminstering the computer/domain)',
194             '(objectCategory=*)',
195             '(samaccountname=Administrator*)'
196         ]
197         for expression in expressions:
198             t = time.time()
199             for i in range(25):
200                 self.ldb.search(self.ou,
201                                 expression=expression,
202                                 scope=SCOPE_SUBTREE,
203                                 attrs=['cn'])
204             print('%d %s took %s' % (i, expression,
205                                      time.time() - t),
206                   file=sys.stderr)
207
208     def _test_indexed_search(self):
209         expressions = ['(objectclass=group)',
210                        '(samaccountname=Administrator)'
211                        ]
212         for expression in expressions:
213             t = time.time()
214             for i in range(4000):
215                 self.ldb.search(self.ou,
216                                 expression=expression,
217                                 scope=SCOPE_SUBTREE,
218                                 attrs=['cn'])
219             print('%d runs %s took %s' % (i, expression,
220                                           time.time() - t),
221                   file=sys.stderr)
222
223     def _test_base_search(self):
224         for dn in [self.base_dn, self.ou, self.ou_users,
225                    self.ou_groups, self.ou_computers]:
226             for i in range(4000):
227                 try:
228                     self.ldb.search(dn,
229                                     scope=SCOPE_BASE,
230                                     attrs=['cn'])
231                 except LdbError as e:
232                     (num, msg) = e.args
233                     if num != ERR_NO_SUCH_OBJECT:
234                         raise
235
236     def _test_base_search_failing(self):
237         pattern = 'missing%d' + self.ou
238         for i in range(4000):
239             try:
240                 self.ldb.search(pattern % i,
241                                 scope=SCOPE_BASE,
242                                 attrs=['cn'])
243             except LdbError as e:
244                 (num, msg) = e
245                 if num != ERR_NO_SUCH_OBJECT:
246                     raise
247
248     def search_expression_list(self, expressions, rounds,
249                                attrs=['cn'],
250                                scope=SCOPE_SUBTREE):
251         for expression in expressions:
252             t = time.time()
253             for i in range(rounds):
254                 self.ldb.search(self.ou,
255                                 expression=expression,
256                                 scope=SCOPE_SUBTREE,
257                                 attrs=['cn'])
258             print('%d runs %s took %s' % (i, expression,
259                                           time.time() - t),
260                   file=sys.stderr)
261
262     def _test_complex_search(self, n=100):
263         classes = ['samaccountname', 'objectCategory', 'dn', 'member']
264         values = ['*', '*t*', 'g*', 'user']
265         comparators = ['=', '<=', '>=']  # '~=' causes error
266         maybe_not = ['!(', '']
267         joiners = ['&', '|']
268
269         # The number of permuations is 18432, which is not huge but
270         # would take hours to search. So we take a sample.
271         all_permutations = list(itertools.product(joiners,
272                                                   classes, classes,
273                                                   values, values,
274                                                   comparators, comparators,
275                                                   maybe_not, maybe_not))
276
277         expressions = []
278
279         for (j, c1, c2, v1, v2,
280              o1, o2, n1, n2) in random.sample(all_permutations, n):
281             expression = ''.join(['(', j,
282                                   '(', n1, c1, o1, v1,
283                                   '))' if n1 else ')',
284                                   '(', n2, c2, o2, v2,
285                                   '))' if n2 else ')',
286                                   ')'])
287             expressions.append(expression)
288
289         self.search_expression_list(expressions, 1)
290
291     def _test_member_search(self, rounds=10):
292         expressions = []
293         for d in range(20):
294             expressions.append('(member=cn=u%d,%s)' % (d + 500, self.ou_users))
295             expressions.append('(member=u%d*)' % (d + 700,))
296
297         self.search_expression_list(expressions, rounds)
298
299     def _test_memberof_search(self, rounds=200):
300         expressions = []
301         for i in range(min(self.state.n_groups, rounds)):
302             expressions.append('(memberOf=cn=g%d,%s)' % (i, self.ou_groups))
303             expressions.append('(memberOf=cn=g%d*)' % (i,))
304             expressions.append('(memberOf=cn=*%s*)' % self.ou_groups)
305
306         self.search_expression_list(expressions, 2)
307
308     def _test_add_many_users(self, n=BATCH_SIZE):
309         s = self.state.next_user_id
310         e = s + n
311         self._add_users(s, e)
312         self.state.next_user_id = e
313
314     def _test_add_many_users_ldif(self, n=BATCH_SIZE):
315         s = self.state.next_user_id
316         e = s + n
317         self._add_users_ldif(s, e)
318         self.state.next_user_id = e
319
320     def _link_user_and_group(self, u, g):
321         link = (u, g)
322         if link in self.state.active_links:
323             return False
324
325         m = Message()
326         m.dn = Dn(self.ldb, "CN=g%d,%s" % (g, self.ou_groups))
327         m["member"] = MessageElement("cn=u%d,%s" % (u, self.ou_users),
328                                      FLAG_MOD_ADD, "member")
329         self.ldb.modify(m)
330         self.state.active_links.add(link)
331         return True
332
333     def _unlink_user_and_group(self, u, g):
334         link = (u, g)
335         if link not in self.state.active_links:
336             return False
337
338         user = "cn=u%d,%s" % (u, self.ou_users)
339         group = "CN=g%d,%s" % (g, self.ou_groups)
340         m = Message()
341         m.dn = Dn(self.ldb, group)
342         m["member"] = MessageElement(user, FLAG_MOD_DELETE, "member")
343         self.ldb.modify(m)
344         self.state.active_links.remove(link)
345         return True
346
347     def _test_link_many_users(self, n=LINK_BATCH_SIZE):
348         # this links unevenly, putting more users in the first group
349         # and fewer in the last.
350         ng = self.state.n_groups
351         nu = self.state.next_user_id
352         while n:
353             u = random.randrange(nu)
354             g = random.randrange(random.randrange(ng) + 1)
355             if self._link_user_and_group(u, g):
356                 n -= 1
357
358     def _test_link_many_users_batch(self, n=(LINK_BATCH_SIZE * 10)):
359         # this links unevenly, putting more users in the first group
360         # and fewer in the last.
361         ng = self.state.n_groups
362         nu = self.state.next_user_id
363         messages = []
364         for g in range(ng):
365             m = Message()
366             m.dn = Dn(self.ldb, "CN=g%d,%s" % (g, self.ou_groups))
367             messages.append(m)
368
369         while n:
370             u = random.randrange(nu)
371             g = random.randrange(random.randrange(ng) + 1)
372             link = (u, g)
373             if link in self.state.active_links:
374                 continue
375             m = messages[g]
376             m["member%s" % u] = MessageElement("cn=u%d,%s" %
377                                                (u, self.ou_users),
378                                                FLAG_MOD_ADD, "member")
379             self.state.active_links.add(link)
380             n -= 1
381
382         for m in messages:
383             try:
384                 self.ldb.modify(m)
385             except LdbError as e:
386                 print(e)
387                 print(m)
388
389     def _test_remove_some_links(self, n=(LINK_BATCH_SIZE // 2)):
390         victims = random.sample(list(self.state.active_links), n)
391         for x in victims:
392             self._unlink_user_and_group(*x)
393
394     test_00_11_join_empty_dc = _test_join
395
396     test_00_12_adding_users_2000 = _test_add_many_users
397
398     test_00_20_join_unlinked_2k_users = _test_join
399     test_00_21_unindexed_search_2k_users = _test_unindexed_search
400     test_00_22_indexed_search_2k_users = _test_indexed_search
401
402     test_00_23_complex_search_2k_users = _test_complex_search
403     test_00_24_member_search_2k_users = _test_member_search
404     test_00_25_memberof_search_2k_users = _test_memberof_search
405
406     test_00_27_base_search_2k_users = _test_base_search
407     test_00_28_base_search_failing_2k_users = _test_base_search_failing
408
409     test_01_01_link_2k_users = _test_link_many_users
410     test_01_02_link_2k_users_batch = _test_link_many_users_batch
411
412     test_02_10_join_2k_linked_dc = _test_join
413     test_02_11_unindexed_search_2k_linked_dc = _test_unindexed_search
414     test_02_12_indexed_search_2k_linked_dc = _test_indexed_search
415
416     test_04_01_remove_some_links_2k = _test_remove_some_links
417
418     test_05_01_adding_users_after_links_4k_ldif = _test_add_many_users_ldif
419
420     test_06_04_link_users_4k = _test_link_many_users
421     test_06_05_link_users_4k_batch = _test_link_many_users_batch
422
423     test_07_01_adding_users_after_links_6k = _test_add_many_users
424
425     def _test_ldif_well_linked_group(self, link_chance=1.0):
426         g = self.state.n_groups
427         self.state.n_groups += 1
428         lines = ["dn: CN=g%d,%s" % (g, self.ou_groups),
429                  "objectclass: group"]
430
431         for i in range(self.state.next_user_id):
432             if random.random() <= link_chance:
433                 lines.append("member: cn=u%d,%s" % (i, self.ou_users))
434                 self.state.active_links.add((i, g))
435
436         lines.append("")
437         self.ldb.add_ldif('\n'.join(lines))
438
439     test_09_01_add_fully_linked_group = _test_ldif_well_linked_group
440
441     def test_09_02_add_exponentially_diminishing_linked_groups(self):
442         linkage = 0.8
443         while linkage > 0.01:
444             self._test_ldif_well_linked_group(linkage)
445             linkage *= 0.75
446
447     test_09_04_link_users_6k = _test_link_many_users
448
449     test_10_01_unindexed_search_6k_users = _test_unindexed_search
450     test_10_02_indexed_search_6k_users = _test_indexed_search
451
452     test_10_27_base_search_6k_users = _test_base_search
453     test_10_28_base_search_failing_6k_users = _test_base_search_failing
454
455     def test_10_03_complex_search_6k_users(self):
456         self._test_complex_search(n=50)
457
458     def test_10_04_member_search_6k_users(self):
459         self._test_member_search(rounds=1)
460
461     def test_10_05_memberof_search_6k_users(self):
462         self._test_memberof_search(rounds=5)
463
464     test_11_02_join_full_dc = _test_join
465
466     test_12_01_remove_some_links_6k = _test_remove_some_links
467
468     def _test_delete_many_users(self, n=DELETE_BATCH_SIZE):
469         e = self.state.next_user_id
470         s = max(0, e - n)
471         self.state.next_user_id = s
472         for i in range(s, e):
473             self.ldb.delete("cn=u%d,%s" % (i, self.ou_users))
474
475         for x in tuple(self.state.active_links):
476             if s >= x[0] > e:
477                 self.state.active_links.remove(x)
478
479     test_20_01_delete_users_6k = _test_delete_many_users
480
481     def test_21_01_delete_10_groups(self):
482         for i in range(self.state.n_groups - 10, self.state.n_groups):
483             self.ldb.delete("cn=g%d,%s" % (i, self.ou_groups))
484         self.state.n_groups -= 10
485         for x in tuple(self.state.active_links):
486             if x[1] >= self.state.n_groups:
487                 self.state.active_links.remove(x)
488
489     test_21_02_delete_users_5950 = _test_delete_many_users
490
491     def test_22_01_delete_all_groups(self):
492         for i in range(self.state.n_groups):
493             self.ldb.delete("cn=g%d,%s" % (i, self.ou_groups))
494         self.state.n_groups = 0
495         self.state.active_links = set()
496
497     # XXX assert the state is as we think, using searches
498
499     def test_23_01_delete_users_5900_after_groups(self):
500         # we do not delete everything because it takes too long
501         n = 4 * DELETE_BATCH_SIZE
502         self._test_delete_many_users(n=n)
503
504     test_24_02_join_after_partial_cleanup = _test_join
505
506
507 if "://" not in host:
508     if os.path.isfile(host):
509         host = "tdb://%s" % host
510     else:
511         host = "ldap://%s" % host
512
513
514 if ANCIENT_SAMBA:
515     runner = SubunitTestRunner()
516     if not runner.run(unittest.makeSuite(UserTests)).wasSuccessful():
517         sys.exit(1)
518     sys.exit(0)
519 else:
520     TestProgram(module=__name__, opts=subunitopts)