2 Unix SMB/CIFS implementation.
4 local test for tdb/ldb speed
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "tdb_compat.h"
26 #include <ldb_errors.h>
28 #include "lib/tdb_wrap/tdb_wrap.h"
29 #include "torture/smbtorture.h"
30 #include "param/param.h"
34 static bool tdb_add_record(struct tdb_wrap
*tdbw
, const char *fmt1
,
35 const char *fmt2
, int i
)
40 key
.dptr
= (uint8_t *)talloc_asprintf(tdbw
, fmt1
, i
);
41 key
.dsize
= strlen((char *)key
.dptr
)+1;
42 data
.dptr
= (uint8_t *)talloc_asprintf(tdbw
, fmt2
, i
+10000);
43 data
.dsize
= strlen((char *)data
.dptr
)+1;
45 ret
= tdb_store(tdbw
->tdb
, key
, data
, TDB_INSERT
);
47 talloc_free(key
.dptr
);
48 talloc_free(data
.dptr
);
55 static bool test_tdb_speed(struct torture_context
*torture
, const void *_data
)
58 struct tdb_wrap
*tdbw
;
59 int timelimit
= torture_setting_int(torture
, "timelimit", 10);
61 TALLOC_CTX
*tmp_ctx
= talloc_new(torture
);
65 torture_comment(torture
, "Testing tdb speed for sidmap\n");
67 tdbw
= tdb_wrap_open(tmp_ctx
, "test.tdb",
68 10000, 0, O_RDWR
|O_CREAT
|O_TRUNC
, 0600, torture
->lp_ctx
);
70 torture_result(torture
, TORTURE_FAIL
, "Failed to open test.tdb");
74 torture_comment(torture
, "Adding %d SID records\n", torture_entries
);
76 for (i
=0;i
<torture_entries
;i
++) {
77 if (!tdb_add_record(tdbw
,
78 "S-1-5-21-53173311-3623041448-2049097239-%u",
80 torture_result(torture
, TORTURE_FAIL
, "Failed to add SID %d!", i
);
83 if (!tdb_add_record(tdbw
,
85 "S-1-5-21-53173311-3623041448-2049097239-%u", i
)) {
86 torture_result(torture
, TORTURE_FAIL
, "Failed to add UID %d!", i
);
91 torture_comment(torture
, "Testing for %d seconds\n", timelimit
);
93 tv
= timeval_current();
95 for (count
=0;timeval_elapsed(&tv
) < timelimit
;count
++) {
97 i
= random() % torture_entries
;
98 key
.dptr
= (uint8_t *)talloc_asprintf(tmp_ctx
, "S-1-5-21-53173311-3623041448-2049097239-%u", i
);
99 key
.dsize
= strlen((char *)key
.dptr
)+1;
100 data
= tdb_fetch_compat(tdbw
->tdb
, key
);
101 talloc_free(key
.dptr
);
102 if (data
.dptr
== NULL
) {
103 torture_result(torture
, TORTURE_FAIL
, "Failed to find SID %d!", i
);
107 key
.dptr
= (uint8_t *)talloc_asprintf(tmp_ctx
, "UID %u", i
);
108 key
.dsize
= strlen((char *)key
.dptr
)+1;
109 data
= tdb_fetch_compat(tdbw
->tdb
, key
);
110 talloc_free(key
.dptr
);
111 if (data
.dptr
== NULL
) {
112 torture_result(torture
, TORTURE_FAIL
, "Failed to find UID %d!", i
);
118 tdb_speed
= count
/timeval_elapsed(&tv
);
119 torture_comment(torture
, "tdb speed %.2f ops/sec\n", tdb_speed
);
121 talloc_free(tmp_ctx
);
126 talloc_free(tmp_ctx
);
132 static bool ldb_add_record(struct ldb_context
*ldb
, unsigned rid
)
134 struct ldb_message
*msg
;
137 msg
= ldb_msg_new(ldb
);
142 msg
->dn
= ldb_dn_new_fmt(msg
, ldb
, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid
);
143 if (msg
->dn
== NULL
) {
148 ret
= ldb_msg_add_fmt(msg
, "UID", "%u", rid
);
149 if (ret
!= LDB_SUCCESS
) {
154 ret
= ldb_add(ldb
, msg
);
158 return ret
== LDB_SUCCESS
;
165 static bool test_ldb_speed(struct torture_context
*torture
, const void *_data
)
168 struct ldb_context
*ldb
;
169 int timelimit
= torture_setting_int(torture
, "timelimit", 10);
171 TALLOC_CTX
*tmp_ctx
= talloc_new(torture
);
172 struct ldb_ldif
*ldif
;
173 const char *init_ldif
= "dn: @INDEXLIST\n" \
177 unlink("./test.ldb");
179 torture_comment(torture
, "Testing ldb speed for sidmap\n");
181 ldb
= ldb_wrap_connect(tmp_ctx
, torture
->ev
, torture
->lp_ctx
, "tdb://test.ldb",
182 NULL
, NULL
, LDB_FLG_NOSYNC
);
184 torture_result(torture
, TORTURE_FAIL
, "Failed to open test.ldb");
189 ldif
= ldb_ldif_read_string(ldb
, &init_ldif
);
191 torture_result(torture
, TORTURE_FAIL
, "Didn't get LDIF data!");
194 if (ldb_add(ldb
, ldif
->msg
) != LDB_SUCCESS
) {
195 torture_result(torture
, TORTURE_FAIL
, "Couldn't apply LDIF data!");
201 torture_comment(torture
, "Adding %d SID records\n", torture_entries
);
203 for (i
=0;i
<torture_entries
;i
++) {
204 if (!ldb_add_record(ldb
, i
)) {
205 torture_result(torture
, TORTURE_FAIL
, "Failed to add SID %d", i
);
210 if (talloc_total_blocks(tmp_ctx
) > 100) {
211 torture_result(torture
, TORTURE_FAIL
, "memory leak in ldb add");
215 torture_comment(torture
, "Testing for %d seconds\n", timelimit
);
217 tv
= timeval_current();
219 for (count
=0;timeval_elapsed(&tv
) < timelimit
;count
++) {
221 struct ldb_result
*res
;
223 i
= random() % torture_entries
;
224 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i
);
225 if (ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
) != LDB_SUCCESS
|| res
->count
!= 1) {
226 torture_result(torture
, TORTURE_FAIL
, "Failed to find SID %d!", i
);
231 if (ldb_search(ldb
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, NULL
, "(UID=%u)", i
) != LDB_SUCCESS
|| res
->count
!= 1) {
232 torture_result(torture
, TORTURE_FAIL
, "Failed to find UID %d!", i
);
238 if (talloc_total_blocks(tmp_ctx
) > 100) {
239 torture_result(torture
, TORTURE_FAIL
, "memory leak in ldb search");
243 ldb_speed
= count
/timeval_elapsed(&tv
);
244 torture_comment(torture
, "ldb speed %.2f ops/sec\n", ldb_speed
);
246 torture_comment(torture
, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed
/tdb_speed
));
248 talloc_free(tmp_ctx
);
249 unlink("./test.ldb");
253 talloc_free(tmp_ctx
);
254 unlink("./test.ldb");
258 struct torture_suite
*torture_local_dbspeed(TALLOC_CTX
*mem_ctx
)
260 struct torture_suite
*s
= torture_suite_create(mem_ctx
, "dbspeed");
261 torture_suite_add_simple_tcase_const(s
, "tdb_speed", test_tdb_speed
,
263 torture_suite_add_simple_tcase_const(s
, "ldb_speed", test_ldb_speed
,