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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/filesys.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "lib/db_wrap.h"
29 #include "torture/torture.h"
32 static BOOL
tdb_add_record(struct tdb_wrap
*tdbw
, const char *fmt1
, const char *fmt2
, int i
)
36 key
.dptr
= (uint8_t *)talloc_asprintf(tdbw
, fmt1
, i
);
37 key
.dsize
= strlen((char *)key
.dptr
)+1;
38 data
.dptr
= (uint8_t *)talloc_asprintf(tdbw
, fmt2
, i
+10000);
39 data
.dsize
= strlen((char *)data
.dptr
)+1;
41 ret
= tdb_store(tdbw
->tdb
, key
, data
, TDB_INSERT
);
43 talloc_free(key
.dptr
);
44 talloc_free(data
.dptr
);
51 static BOOL
test_tdb_speed(struct torture_context
*torture
, const void *_data
)
54 struct tdb_wrap
*tdbw
;
55 int timelimit
= lp_parm_int(-1, "torture", "timelimit", 10);
57 TALLOC_CTX
*tmp_ctx
= talloc_new(torture
);
61 torture_comment(torture
, "Testing tdb speed for sidmap");
63 tdbw
= tdb_wrap_open(tmp_ctx
, "test.tdb",
64 10000, 0, O_RDWR
|O_CREAT
|O_TRUNC
, 0600);
66 torture_fail(torture
, "Failed to open test.tdb");
70 torture_comment(torture
, "Adding %d SID records", torture_entries
);
72 for (i
=0;i
<torture_entries
;i
++) {
73 if (!tdb_add_record(tdbw
,
74 "S-1-5-21-53173311-3623041448-2049097239-%u",
76 _torture_fail_ext(torture
, "Failed to add SID %d", i
);
79 if (!tdb_add_record(tdbw
,
81 "S-1-5-21-53173311-3623041448-2049097239-%u", i
)) {
82 _torture_fail_ext(torture
, "Failed to add UID %d", i
);
87 torture_comment(torture
, "Testing for %d seconds", timelimit
);
89 tv
= timeval_current();
91 for (count
=0;timeval_elapsed(&tv
) < timelimit
;count
++) {
93 i
= random() % torture_entries
;
94 key
.dptr
= (uint8_t *)talloc_asprintf(tmp_ctx
, "S-1-5-21-53173311-3623041448-2049097239-%u", i
);
95 key
.dsize
= strlen((char *)key
.dptr
)+1;
96 data
= tdb_fetch(tdbw
->tdb
, key
);
97 if (data
.dptr
== NULL
) {
98 _torture_fail_ext(torture
, "Failed to fetch SID %d", i
);
102 key
.dptr
= (uint8_t *)talloc_asprintf(tmp_ctx
, "UID %u", i
);
103 key
.dsize
= strlen((char *)key
.dptr
)+1;
104 data
= tdb_fetch(tdbw
->tdb
, key
);
105 if (data
.dptr
== NULL
) {
106 _torture_fail_ext(torture
, "Failed to fetch UID %d", i
);
112 torture_comment(torture
, "tdb speed %.2f ops/sec", count
/timeval_elapsed(&tv
));
116 talloc_free(tmp_ctx
);
121 talloc_free(tmp_ctx
);
126 static BOOL
ldb_add_record(struct ldb_context
*ldb
, unsigned rid
)
128 struct ldb_message
*msg
;
131 msg
= ldb_msg_new(ldb
);
136 msg
->dn
= ldb_dn_string_compose(msg
, NULL
, "SID=S-1-5-21-53173311-3623041448-2049097239-%u",
138 if (msg
->dn
== NULL
) {
142 if (ldb_msg_add_fmt(msg
, "UID", "%u", rid
) != 0) {
146 ret
= ldb_add(ldb
, msg
);
150 return ret
== LDB_SUCCESS
;
157 static BOOL
test_ldb_speed(struct torture_context
*torture
, const void *_data
)
160 struct ldb_context
*ldb
;
161 int timelimit
= lp_parm_int(-1, "torture", "timelimit", 10);
163 TALLOC_CTX
*tmp_ctx
= talloc_new(torture
);
164 struct ldb_ldif
*ldif
;
165 const char *init_ldif
= "dn: @INDEXLIST\n" \
168 unlink("./test.ldb");
170 torture_comment(torture
, "Testing ldb speed for sidmap");
172 ldb
= ldb_wrap_connect(tmp_ctx
, "tdb://test.ldb",
173 NULL
, NULL
, LDB_FLG_NOSYNC
, NULL
);
175 torture_fail(torture
, "Failed to open test.ldb");
180 ldif
= ldb_ldif_read_string(ldb
, &init_ldif
);
181 if (ldif
== NULL
) goto failed
;
182 if (ldb_add(ldb
, ldif
->msg
) != LDB_SUCCESS
) goto failed
;
185 torture_comment(torture
, "Adding %d SID records", torture_entries
);
187 for (i
=0;i
<torture_entries
;i
++) {
188 if (!ldb_add_record(ldb
, i
)) {
189 _torture_fail_ext(torture
, "Failed to add SID %d", i
);
194 if (talloc_total_blocks(torture
) > 100) {
195 _torture_fail_ext(torture
, "memory leak in ldb add");
199 torture_comment(torture
, "Testing for %d seconds", timelimit
);
201 tv
= timeval_current();
203 for (count
=0;timeval_elapsed(&tv
) < timelimit
;count
++) {
205 struct ldb_result
*res
;
208 i
= random() % torture_entries
;
209 dn
= ldb_dn_string_compose(tmp_ctx
, NULL
, "SID=S-1-5-21-53173311-3623041448-2049097239-%u",
211 if (ldb_search(ldb
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
, &res
) != LDB_SUCCESS
||
213 torture_fail(torture
, talloc_asprintf(torture
,
214 "Failed to find SID %d", i
));
218 expr
= talloc_asprintf(tmp_ctx
, "(UID=%u)", i
);
219 if (ldb_search(ldb
, NULL
, LDB_SCOPE_SUBTREE
, expr
, NULL
, &res
) != LDB_SUCCESS
||
221 torture_fail(torture
, talloc_asprintf(torture
,
222 "Failed to find UID %d", i
));
228 if (talloc_total_blocks(torture
) > 100) {
229 torture_fail(torture
, "memory leak in ldb search");
233 torture_comment(torture
, "ldb speed %.2f ops/sec", count
/timeval_elapsed(&tv
));
236 unlink("./test.ldb");
237 talloc_free(tmp_ctx
);
241 unlink("./test.ldb");
242 talloc_free(tmp_ctx
);
246 struct torture_suite
*torture_local_dbspeed(TALLOC_CTX
*mem_ctx
)
248 struct torture_suite
*s
= torture_suite_create(mem_ctx
, "LOCAL-DBSPEED");
249 torture_suite_add_simple_tcase(s
, "tdb_speed", test_tdb_speed
, NULL
);
250 torture_suite_add_simple_tcase(s
, "ldb_speed", test_ldb_speed
, NULL
);