s4:ldb Change ldb_request_add_control to the normal 'for loop' pattern
[Samba/cd1.git] / source4 / torture / local / dbspeed.c
blob236a52b5172c342fef63759539b026ee0c8140ab
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 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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "../tdb/include/tdb.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb_wrap.h"
28 #include "lib/tdb_wrap.h"
29 #include "torture/smbtorture.h"
30 #include "param/param.h"
32 float tdb_speed;
34 static bool tdb_add_record(struct tdb_wrap *tdbw, const char *fmt1,
35 const char *fmt2, int i)
37 TDB_DATA key, data;
38 int ret;
39 key.dptr = (uint8_t *)talloc_asprintf(tdbw, fmt1, i);
40 key.dsize = strlen((char *)key.dptr)+1;
41 data.dptr = (uint8_t *)talloc_asprintf(tdbw, fmt2, i+10000);
42 data.dsize = strlen((char *)data.dptr)+1;
44 ret = tdb_store(tdbw->tdb, key, data, TDB_INSERT);
46 talloc_free(key.dptr);
47 talloc_free(data.dptr);
48 return ret == 0;
52 test tdb speed
54 static bool test_tdb_speed(struct torture_context *torture, const void *_data)
56 struct timeval tv;
57 struct tdb_wrap *tdbw;
58 int timelimit = torture_setting_int(torture, "timelimit", 10);
59 int i, count;
60 TALLOC_CTX *tmp_ctx = talloc_new(torture);
62 unlink("test.tdb");
64 torture_comment(torture, "Testing tdb speed for sidmap\n");
66 tdbw = tdb_wrap_open(tmp_ctx, "test.tdb",
67 10000, 0, O_RDWR|O_CREAT|O_TRUNC, 0600);
68 if (!tdbw) {
69 unlink("test.tdb");
70 talloc_free(tmp_ctx);
71 torture_fail(torture, "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",
79 "UID %u", i)) {
80 torture_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", i);
81 goto failed;
83 if (!tdb_add_record(tdbw,
84 "UID %u",
85 "S-1-5-21-53173311-3623041448-2049097239-%u", i)) {
86 torture_result(torture, TORTURE_FAIL, "Failed to add UID %d\n", i);
87 goto failed;
91 torture_comment(torture, "Testing for %d seconds\n", timelimit);
93 tv = timeval_current();
95 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
96 TDB_DATA key, data;
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(tdbw->tdb, key);
101 talloc_free(key.dptr);
102 if (data.dptr == NULL) {
103 torture_result(torture, TORTURE_FAIL, "Failed to fetch SID %d\n", i);
104 goto failed;
106 free(data.dptr);
107 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "UID %u", i);
108 key.dsize = strlen((char *)key.dptr)+1;
109 data = tdb_fetch(tdbw->tdb, key);
110 talloc_free(key.dptr);
111 if (data.dptr == NULL) {
112 torture_result(torture, TORTURE_FAIL, "Failed to fetch UID %d\n", i);
113 goto failed;
115 free(data.dptr);
118 tdb_speed = count/timeval_elapsed(&tv);
119 torture_comment(torture, "tdb speed %.2f ops/sec\n", tdb_speed);
122 unlink("test.tdb");
123 talloc_free(tmp_ctx);
124 return true;
126 failed:
127 unlink("test.tdb");
128 talloc_free(tmp_ctx);
129 return false;
133 static bool ldb_add_record(struct ldb_context *ldb, unsigned rid)
135 struct ldb_message *msg;
136 int ret;
138 msg = ldb_msg_new(ldb);
139 if (msg == NULL) {
140 return false;
143 msg->dn = ldb_dn_new_fmt(msg, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid);
144 if (msg->dn == NULL) {
145 return false;
148 if (ldb_msg_add_fmt(msg, "UID", "%u", rid) != 0) {
149 return false;
152 ret = ldb_add(ldb, msg);
154 talloc_free(msg);
156 return ret == LDB_SUCCESS;
161 test ldb speed
163 static bool test_ldb_speed(struct torture_context *torture, const void *_data)
165 struct timeval tv;
166 struct ldb_context *ldb;
167 int timelimit = torture_setting_int(torture, "timelimit", 10);
168 int i, count;
169 TALLOC_CTX *tmp_ctx = talloc_new(torture);
170 struct ldb_ldif *ldif;
171 const char *init_ldif = "dn: @INDEXLIST\n" \
172 "@IDXATTR: UID\n";
173 float ldb_speed;
175 unlink("./test.ldb");
177 torture_comment(torture, "Testing ldb speed for sidmap\n");
179 ldb = ldb_wrap_connect(tmp_ctx, torture->ev, torture->lp_ctx, "tdb://test.ldb",
180 NULL, NULL, LDB_FLG_NOSYNC);
181 if (!ldb) {
182 unlink("./test.ldb");
183 talloc_free(tmp_ctx);
184 torture_fail(torture, "Failed to open test.ldb");
187 /* add an index */
188 ldif = ldb_ldif_read_string(ldb, &init_ldif);
189 if (ldif == NULL) goto failed;
190 if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) goto failed;
191 talloc_free(ldif);
193 torture_comment(torture, "Adding %d SID records\n", torture_entries);
195 for (i=0;i<torture_entries;i++) {
196 if (!ldb_add_record(ldb, i)) {
197 torture_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", i);
198 goto failed;
202 if (talloc_total_blocks(torture) > 100) {
203 torture_result(torture, TORTURE_FAIL, "memory leak in ldb add\n");
204 goto failed;
207 torture_comment(torture, "Testing for %d seconds\n", timelimit);
209 tv = timeval_current();
211 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
212 struct ldb_dn *dn;
213 struct ldb_result *res;
215 i = random() % torture_entries;
216 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
217 if (ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL) != LDB_SUCCESS || res->count != 1) {
218 torture_fail(torture, talloc_asprintf(torture, "Failed to find SID %d", i));
220 talloc_free(res);
221 talloc_free(dn);
222 if (ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(UID=%u)", i) != LDB_SUCCESS || res->count != 1) {
223 torture_fail(torture, talloc_asprintf(torture, "Failed to find UID %d", i));
225 talloc_free(res);
228 if (talloc_total_blocks(torture) > 100) {
229 unlink("./test.ldb");
230 talloc_free(tmp_ctx);
231 torture_fail(torture, "memory leak in ldb search");
234 ldb_speed = count/timeval_elapsed(&tv);
235 torture_comment(torture, "ldb speed %.2f ops/sec\n", ldb_speed);
237 torture_comment(torture, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed/tdb_speed));
240 unlink("./test.ldb");
241 talloc_free(tmp_ctx);
242 return true;
244 failed:
245 unlink("./test.ldb");
246 talloc_free(tmp_ctx);
247 return false;
250 struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
252 struct torture_suite *s = torture_suite_create(mem_ctx, "DBSPEED");
253 torture_suite_add_simple_tcase_const(s, "tdb_speed", test_tdb_speed,
254 NULL);
255 torture_suite_add_simple_tcase_const(s, "ldb_speed", test_ldb_speed,
256 NULL);
257 return s;