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 from __future__ import print_function
30 from decimal import Decimal
32 sys.path.insert(0, "bin/python")
34 from samba.tests.subunitrun import TestProgram, SubunitOptions
36 import samba.getopt as options
38 from ldb import SCOPE_BASE, SCOPE_SUBTREE
39 from samba.ndr import ndr_unpack
40 from samba.dcerpc import security
42 from samba.auth import system_session
43 from samba import gensec, sd_utils
44 from samba.samdb import SamDB
45 from samba.credentials import Credentials
47 from samba.tests import delete_force
49 parser = optparse.OptionParser("speedtest.py [options] <host>")
50 sambaopts = options.SambaOptions(parser)
51 parser.add_option_group(sambaopts)
52 parser.add_option_group(options.VersionOptions(parser))
54 # use command line creds if available
55 credopts = options.CredentialsOptions(parser)
56 parser.add_option_group(credopts)
57 subunitopts = SubunitOptions(parser)
58 parser.add_option_group(subunitopts)
59 opts, args = parser.parse_args()
67 lp = sambaopts.get_loadparm()
68 creds = credopts.get_credentials(lp)
69 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
75 class SpeedTest(samba.tests.TestCase):
77 def find_domain_sid(self, ldb):
78 res = ldb.search(base=self.base_dn, expression="(objectClass=*)", scope=SCOPE_BASE)
79 return ndr_unpack(security.dom_sid, res[0]["objectSid"][0])
82 super(SpeedTest, self).setUp()
84 self.base_dn = ldb.domain_dn()
85 self.domain_sid = security.dom_sid(ldb.get_domain_sid())
86 self.user_pass = "samba123@"
87 print("baseDN: %s" % self.base_dn)
89 def create_user(self, user_dn):
91 dn: """ + user_dn + """
92 sAMAccountName: """ + user_dn.split(",")[0][3:] + """
94 unicodePwd:: """ + base64.b64encode(("\"%s\"" % self.user_pass).encode('utf-16-le')).decode('utf8') + """
97 self.ldb_admin.add_ldif(ldif)
99 def create_group(self, group_dn, desc=None):
101 dn: """ + group_dn + """
103 sAMAccountName: """ + group_dn.split(",")[0][3:] + """
107 self.ldb_admin.add_ldif(ldif)
109 def create_bundle(self, count):
110 for i in range(count):
111 self.create_user("cn=speedtestuser%d,cn=Users,%s" % (i + 1, self.base_dn))
113 def remove_bundle(self, count):
114 for i in range(count):
115 delete_force(self.ldb_admin, "cn=speedtestuser%d,cn=Users,%s" % (i + 1, self.base_dn))
117 def remove_test_users(self):
118 res = ldb.search(base="cn=Users,%s" % self.base_dn, expression="(objectClass=user)", scope=SCOPE_SUBTREE)
119 dn_list = [item.dn for item in res if "speedtestuser" in str(item.dn)]
121 delete_force(self.ldb_admin, dn)
123 class SpeedTestAddDel(SpeedTest):
126 super(SpeedTestAddDel, self).setUp()
128 def run_bundle(self, num):
129 print("\n=== Test ADD/DEL %s user objects ===\n" % num)
130 avg_add = Decimal("0.0")
131 avg_del = Decimal("0.0")
134 self.create_bundle(num)
135 res_add = Decimal(str(time.time() - start))
137 print(" Attempt %s ADD: %.3fs" % (x, float(res_add)))
140 self.remove_bundle(num)
141 res_del = Decimal(str(time.time() - start))
143 print(" Attempt %s DEL: %.3fs" % (x, float(res_del)))
144 print("Average ADD: %.3fs" % float(Decimal(avg_add) / Decimal("3.0")))
145 print("Average DEL: %.3fs" % float(Decimal(avg_del) / Decimal("3.0")))
148 def test_00000(self):
149 """ Remove possibly undeleted test users from previous test
151 self.remove_test_users()
153 def test_00010(self):
156 def test_00100(self):
159 def test_01000(self):
160 self.run_bundle(1000)
162 def _test_10000(self):
163 """ This test should be enabled preferably against MS Active Directory.
164 It takes quite the time against Samba4 (1-2 days).
166 self.run_bundle(10000)
168 class AclSearchSpeedTest(SpeedTest):
171 super(AclSearchSpeedTest, self).setUp()
172 self.ldb_admin.newuser("acltestuser", "samba123@")
173 self.sd_utils = sd_utils.SDUtils(self.ldb_admin)
174 self.ldb_user = self.get_ldb_connection("acltestuser", "samba123@")
175 self.user_sid = self.sd_utils.get_object_sid(self.get_user_dn("acltestuser"))
178 super(AclSearchSpeedTest, self).tearDown()
179 delete_force(self.ldb_admin, self.get_user_dn("acltestuser"))
181 def run_search_bundle(self, num, _ldb):
182 print("\n=== Creating %s user objects ===\n" % num)
183 self.create_bundle(num)
184 mod = "(A;;LC;;;%s)(D;;RP;;;%s)" % (str(self.user_sid), str(self.user_sid))
186 self.sd_utils.dacl_add_ace("cn=speedtestuser%d,cn=Users,%s" %
187 (i + 1, self.base_dn), mod)
188 print("\n=== %s user objects created ===\n" % num)
189 print("\n=== Test search on %s user objects ===\n" % num)
190 avg_search = Decimal("0.0")
193 res = _ldb.search(base=self.base_dn, expression="(objectClass=*)", scope=SCOPE_SUBTREE)
194 res_search = Decimal(str(time.time() - start))
195 avg_search += res_search
196 print(" Attempt %s SEARCH: %.3fs" % (x, float(res_search)))
197 print("Average Search: %.3fs" % float(Decimal(avg_search) / Decimal("3.0")))
198 self.remove_bundle(num)
200 def get_user_dn(self, name):
201 return "CN=%s,CN=Users,%s" % (name, self.base_dn)
203 def get_ldb_connection(self, target_username, target_password):
204 creds_tmp = Credentials()
205 creds_tmp.set_username(target_username)
206 creds_tmp.set_password(target_password)
207 creds_tmp.set_domain(creds.get_domain())
208 creds_tmp.set_realm(creds.get_realm())
209 creds_tmp.set_workstation(creds.get_workstation())
210 creds_tmp.set_gensec_features(creds_tmp.get_gensec_features()
211 | gensec.FEATURE_SEAL)
212 ldb_target = SamDB(url=host, credentials=creds_tmp, lp=lp)
215 def test_search_01000(self):
216 self.run_search_bundle(1000, self.ldb_admin)
218 def test_search2_01000(self):
219 # allow the user to see objects but not attributes, all attributes will be filtered out
220 mod = "(A;;LC;;;%s)(D;;RP;;;%s)" % (str(self.user_sid), str(self.user_sid))
221 self.sd_utils.dacl_add_ace("CN=Users,%s" % self.base_dn, mod)
222 self.run_search_bundle(1000, self.ldb_user)
224 # Important unit running information
226 if not "://" in host:
227 host = "ldap://%s" % host
229 ldb_options = ["modules:paged_searches"]
230 ldb = SamDB(host, credentials=creds, session_info=system_session(), lp=lp, options=ldb_options)
232 TestProgram(module=__name__, opts=subunitopts)