r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[Samba.git] / source / torture / local / dbspeed.c
blob757c13cf6e9046cb30082a8c406cb434586a8a31
1 /*
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.
23 #include "includes.h"
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)
34 TDB_DATA key, data;
35 int ret;
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);
45 return ret == 0;
49 test tdb speed
51 static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
53 struct timeval tv;
54 struct tdb_wrap *tdbw;
55 int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
56 int i, count;
57 TALLOC_CTX *tmp_ctx = talloc_new(torture);
59 unlink("test.tdb");
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);
65 if (!tdbw) {
66 torture_fail(torture, "Failed to open test.tdb");
67 goto failed;
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",
75 "UID %u", i)) {
76 _torture_fail_ext(torture, "Failed to add SID %d", i);
77 goto failed;
79 if (!tdb_add_record(tdbw,
80 "UID %u",
81 "S-1-5-21-53173311-3623041448-2049097239-%u", i)) {
82 _torture_fail_ext(torture, "Failed to add UID %d", i);
83 goto failed;
87 torture_comment(torture, "Testing for %d seconds", timelimit);
89 tv = timeval_current();
91 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
92 TDB_DATA key, data;
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);
99 goto failed;
101 free(data.dptr);
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);
107 goto failed;
109 free(data.dptr);
112 torture_comment(torture, "tdb speed %.2f ops/sec", count/timeval_elapsed(&tv));
115 unlink("test.tdb");
116 talloc_free(tmp_ctx);
117 return True;
119 failed:
120 unlink("test.tdb");
121 talloc_free(tmp_ctx);
122 return False;
126 static BOOL ldb_add_record(struct ldb_context *ldb, unsigned rid)
128 struct ldb_message *msg;
129 int ret;
131 msg = ldb_msg_new(ldb);
132 if (msg == NULL) {
133 return False;
136 msg->dn = ldb_dn_string_compose(msg, NULL, "SID=S-1-5-21-53173311-3623041448-2049097239-%u",
137 rid);
138 if (msg->dn == NULL) {
139 return False;
142 if (ldb_msg_add_fmt(msg, "UID", "%u", rid) != 0) {
143 return False;
146 ret = ldb_add(ldb, msg);
148 talloc_free(msg);
150 return ret == LDB_SUCCESS;
155 test ldb speed
157 static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
159 struct timeval tv;
160 struct ldb_context *ldb;
161 int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
162 int i, count;
163 TALLOC_CTX *tmp_ctx = talloc_new(torture);
164 struct ldb_ldif *ldif;
165 const char *init_ldif = "dn: @INDEXLIST\n" \
166 "@IDXATTR: UID\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);
174 if (!ldb) {
175 torture_fail(torture, "Failed to open test.ldb");
176 goto failed;
179 /* add an index */
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;
183 talloc_free(ldif);
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);
190 goto failed;
194 if (talloc_total_blocks(torture) > 100) {
195 _torture_fail_ext(torture, "memory leak in ldb add");
196 goto failed;
199 torture_comment(torture, "Testing for %d seconds", timelimit);
201 tv = timeval_current();
203 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
204 struct ldb_dn *dn;
205 struct ldb_result *res;
206 char *expr;
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 ||
212 res->count != 1) {
213 torture_fail(torture, talloc_asprintf(torture,
214 "Failed to find SID %d", i));
216 talloc_free(res);
217 talloc_free(dn);
218 expr = talloc_asprintf(tmp_ctx, "(UID=%u)", i);
219 if (ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res) != LDB_SUCCESS ||
220 res->count != 1) {
221 torture_fail(torture, talloc_asprintf(torture,
222 "Failed to find UID %d", i));
224 talloc_free(res);
225 talloc_free(expr);
228 if (talloc_total_blocks(torture) > 100) {
229 torture_fail(torture, "memory leak in ldb search");
230 goto failed;
233 torture_comment(torture, "ldb speed %.2f ops/sec", count/timeval_elapsed(&tv));
236 unlink("./test.ldb");
237 talloc_free(tmp_ctx);
238 return True;
240 failed:
241 unlink("./test.ldb");
242 talloc_free(tmp_ctx);
243 return False;
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);
251 return s;