s4:librpc/rpc: don't do async requests if gensec doesn't support async replies (bug...
[Samba/gebeck_regimport.git] / source4 / scripting / devel / speedtest.py
blobfed270527a07b317f285383337bed792bffc88d5
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Unix SMB/CIFS implementation.
5 # This speed test aims to show difference in execution time for bulk
6 # creation of user objects. This will help us compare
7 # Samba4 vs MS Active Directory performance.
9 # Copyright (C) Zahari Zahariev <zahari.zahariev@postpath.com> 2010
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import optparse
26 import sys
27 import time
28 import base64
29 from decimal import Decimal
31 sys.path.insert(0, "bin/python")
32 import samba
33 samba.ensure_external_module("testtools", "testtools")
34 samba.ensure_external_module("subunit", "subunit/python")
36 import samba.getopt as options
38 from ldb import (
39 SCOPE_BASE, SCOPE_SUBTREE, LdbError, ERR_NO_SUCH_OBJECT,
40 ERR_UNWILLING_TO_PERFORM, ERR_INSUFFICIENT_ACCESS_RIGHTS)
41 from samba.ndr import ndr_pack, ndr_unpack
42 from samba.dcerpc import security
44 from samba.auth import system_session
45 from samba import gensec, sd_utils
46 from samba.samdb import SamDB
47 from samba.credentials import Credentials
48 import samba.tests
49 from samba.tests import delete_force
50 from subunit.run import SubunitTestRunner
51 import unittest
53 parser = optparse.OptionParser("speedtest.py [options] <host>")
54 sambaopts = options.SambaOptions(parser)
55 parser.add_option_group(sambaopts)
56 parser.add_option_group(options.VersionOptions(parser))
59 # use command line creds if available
60 credopts = options.CredentialsOptions(parser)
61 parser.add_option_group(credopts)
62 opts, args = parser.parse_args()
64 if len(args) < 1:
65 parser.print_usage()
66 sys.exit(1)
68 host = args[0]
70 lp = sambaopts.get_loadparm()
71 creds = credopts.get_credentials(lp)
72 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
75 # Tests start here
78 class SpeedTest(samba.tests.TestCase):
80 def find_domain_sid(self, ldb):
81 res = ldb.search(base=self.base_dn, expression="(objectClass=*)", scope=SCOPE_BASE)
82 return ndr_unpack(security.dom_sid,res[0]["objectSid"][0])
84 def setUp(self):
85 super(SpeedTest, self).setUp()
86 self.ldb_admin = ldb
87 self.base_dn = ldb.domain_dn()
88 self.domain_sid = security.dom_sid(ldb.get_domain_sid())
89 self.user_pass = "samba123@"
90 print "baseDN: %s" % self.base_dn
92 def create_user(self, user_dn):
93 ldif = """
94 dn: """ + user_dn + """
95 sAMAccountName: """ + user_dn.split(",")[0][3:] + """
96 objectClass: user
97 unicodePwd:: """ + base64.b64encode(("\"%s\"" % self.user_pass).encode('utf-16-le')) + """
98 url: www.example.com
99 """
100 self.ldb_admin.add_ldif(ldif)
102 def create_group(self, group_dn, desc=None):
103 ldif = """
104 dn: """ + group_dn + """
105 objectClass: group
106 sAMAccountName: """ + group_dn.split(",")[0][3:] + """
107 groupType: 4
108 url: www.example.com
110 self.ldb_admin.add_ldif(ldif)
112 def create_bundle(self, count):
113 for i in range(count):
114 self.create_user("cn=speedtestuser%d,cn=Users,%s" % (i+1, self.base_dn))
116 def remove_bundle(self, count):
117 for i in range(count):
118 delete_force(self.ldb_admin, "cn=speedtestuser%d,cn=Users,%s" % (i+1, self.base_dn))
120 def remove_test_users(self):
121 res = ldb.search(base="cn=Users,%s" % self.base_dn, expression="(objectClass=user)", scope=SCOPE_SUBTREE)
122 dn_list = [item.dn for item in res if "speedtestuser" in str(item.dn)]
123 for dn in dn_list:
124 delete_force(self.ldb_admin, dn)
126 class SpeedTestAddDel(SpeedTest):
128 def setUp(self):
129 super(SpeedTestAddDel, self).setUp()
131 def run_bundle(self, num):
132 print "\n=== Test ADD/DEL %s user objects ===\n" % num
133 avg_add = Decimal("0.0")
134 avg_del = Decimal("0.0")
135 for x in [1, 2, 3]:
136 start = time.time()
137 self.create_bundle(num)
138 res_add = Decimal( str(time.time() - start) )
139 avg_add += res_add
140 print " Attempt %s ADD: %.3fs" % ( x, float(res_add) )
142 start = time.time()
143 self.remove_bundle(num)
144 res_del = Decimal( str(time.time() - start) )
145 avg_del += res_del
146 print " Attempt %s DEL: %.3fs" % ( x, float(res_del) )
147 print "Average ADD: %.3fs" % float( Decimal(avg_add) / Decimal("3.0") )
148 print "Average DEL: %.3fs" % float( Decimal(avg_del) / Decimal("3.0") )
149 print ""
151 def test_00000(self):
152 """ Remove possibly undeleted test users from previous test
154 self.remove_test_users()
156 def test_00010(self):
157 self.run_bundle(10)
159 def test_00100(self):
160 self.run_bundle(100)
162 def test_01000(self):
163 self.run_bundle(1000)
165 def _test_10000(self):
166 """ This test should be enabled preferably against MS Active Directory.
167 It takes quite the time against Samba4 (1-2 days).
169 self.run_bundle(10000)
171 class AclSearchSpeedTest(SpeedTest):
173 def setUp(self):
174 super(AclSearchSpeedTest, self).setUp()
175 self.ldb_admin.newuser("acltestuser", "samba123@")
176 self.sd_utils = sd_utils.SDUtils(self.ldb_admin)
177 self.ldb_user = self.get_ldb_connection("acltestuser", "samba123@")
178 self.user_sid = self.sd_utils.get_object_sid(self.get_user_dn("acltestuser"))
180 def tearDown(self):
181 super(AclSearchSpeedTest, self).tearDown()
182 delete_force(self.ldb_admin, self.get_user_dn("acltestuser"))
184 def run_search_bundle(self, num, _ldb):
185 print "\n=== Creating %s user objects ===\n" % num
186 self.create_bundle(num)
187 mod = "(A;;LC;;;%s)(D;;RP;;;%s)" % (str(self.user_sid), str(self.user_sid))
188 for i in range(num):
189 self.sd_utils.dacl_add_ace("cn=speedtestuser%d,cn=Users,%s" %
190 (i+1, self.base_dn), mod)
191 print "\n=== %s user objects created ===\n" % num
192 print "\n=== Test search on %s user objects ===\n" % num
193 avg_search = Decimal("0.0")
194 for x in [1, 2, 3]:
195 start = time.time()
196 res = _ldb.search(base=self.base_dn, expression="(objectClass=*)", scope=SCOPE_SUBTREE)
197 res_search = Decimal( str(time.time() - start) )
198 avg_search += res_search
199 print " Attempt %s SEARCH: %.3fs" % ( x, float(res_search) )
200 print "Average Search: %.3fs" % float( Decimal(avg_search) / Decimal("3.0") )
201 self.remove_bundle(num)
203 def get_user_dn(self, name):
204 return "CN=%s,CN=Users,%s" % (name, self.base_dn)
206 def get_ldb_connection(self, target_username, target_password):
207 creds_tmp = Credentials()
208 creds_tmp.set_username(target_username)
209 creds_tmp.set_password(target_password)
210 creds_tmp.set_domain(creds.get_domain())
211 creds_tmp.set_realm(creds.get_realm())
212 creds_tmp.set_workstation(creds.get_workstation())
213 creds_tmp.set_gensec_features(creds_tmp.get_gensec_features()
214 | gensec.FEATURE_SEAL)
215 ldb_target = SamDB(url=host, credentials=creds_tmp, lp=lp)
216 return ldb_target
218 def test_search_01000(self):
219 self.run_search_bundle(1000, self.ldb_admin)
221 def test_search2_01000(self):
222 # allow the user to see objects but not attributes, all attributes will be filtered out
223 mod = "(A;;LC;;;%s)(D;;RP;;;%s)" % (str(self.user_sid), str(self.user_sid))
224 self.sd_utils.dacl_add_ace("CN=Users,%s" % self.base_dn, mod)
225 self.run_search_bundle(1000, self.ldb_user)
227 # Important unit running information
229 if not "://" in host:
230 host = "ldap://%s" % host
232 ldb_options = ["modules:paged_searches"]
233 ldb = SamDB(host, credentials=creds, session_info=system_session(), lp=lp, options=ldb_options)
235 runner = SubunitTestRunner()
236 rc = 0
237 if not runner.run(unittest.makeSuite(SpeedTestAddDel)).wasSuccessful():
238 rc = 1
239 if not runner.run(unittest.makeSuite(AclSearchSpeedTest)).wasSuccessful():
240 rc = 1
241 sys.exit(rc)