ldb: Release ldb 2.2.1
[Samba.git] / source4 / torture / local / dbspeed.c
blob9452d6a7e7c1471fe0fc2361fe906b0561c988a7
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.h>
25 #include <ldb.h>
26 #include <ldb_errors.h>
27 #include "ldb_wrap.h"
28 #include "lib/tdb_wrap/tdb_wrap.h"
29 #include "torture/smbtorture.h"
30 #include "torture/local/proto.h"
31 #include "param/param.h"
33 float tdb_speed;
35 static bool tdb_add_record(struct tdb_wrap *tdbw, const char *p1,
36 const char *p2, int i)
38 TDB_DATA key, data;
39 int ret;
41 key.dptr = (uint8_t *)talloc_asprintf(tdbw, "%s%u", p1, i);
42 key.dsize = strlen((char *)key.dptr)+1;
43 data.dptr = (uint8_t *)talloc_asprintf(tdbw, "%s%u", p2, i+10000);
44 data.dsize = strlen((char *)data.dptr)+1;
46 ret = tdb_store(tdbw->tdb, key, data, TDB_INSERT);
48 talloc_free(key.dptr);
49 talloc_free(data.dptr);
50 return ret == 0;
54 test tdb speed
56 static bool test_tdb_speed(struct torture_context *torture, const void *_data)
58 struct timeval tv;
59 struct tdb_wrap *tdbw;
60 int timelimit = torture_setting_int(torture, "timelimit", 10);
61 int i, count;
62 TALLOC_CTX *tmp_ctx = talloc_new(torture);
64 unlink("test.tdb");
66 torture_comment(torture, "Testing tdb speed for sidmap\n");
68 tdbw = tdb_wrap_open(tmp_ctx, "test.tdb", 10000,
69 lpcfg_tdb_flags(torture->lp_ctx, 0),
70 O_RDWR|O_CREAT|O_TRUNC, 0600);
71 if (!tdbw) {
72 torture_result(torture, TORTURE_FAIL, "Failed to open test.tdb");
73 goto failed;
76 torture_comment(torture, "Adding %d SID records\n", torture_entries);
78 for (i=0;i<torture_entries;i++) {
79 if (!tdb_add_record(tdbw,
80 "S-1-5-21-53173311-3623041448-2049097239-",
81 "UID ", i)) {
82 torture_result(torture, TORTURE_FAIL, "Failed to add SID %d!", i);
83 goto failed;
85 if (!tdb_add_record(tdbw,
86 "UID ",
87 "S-1-5-21-53173311-3623041448-2049097239-", i)) {
88 torture_result(torture, TORTURE_FAIL, "Failed to add UID %d!", i);
89 goto failed;
93 torture_comment(torture, "Testing for %d seconds\n", timelimit);
95 tv = timeval_current();
97 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
98 TDB_DATA key, data;
99 i = random() % torture_entries;
100 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "S-1-5-21-53173311-3623041448-2049097239-%u", i);
101 key.dsize = strlen((char *)key.dptr)+1;
102 data = tdb_fetch(tdbw->tdb, key);
103 talloc_free(key.dptr);
104 if (data.dptr == NULL) {
105 torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
106 goto failed;
108 free(data.dptr);
109 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "UID %u", i);
110 key.dsize = strlen((char *)key.dptr)+1;
111 data = tdb_fetch(tdbw->tdb, key);
112 talloc_free(key.dptr);
113 if (data.dptr == NULL) {
114 torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
115 goto failed;
117 free(data.dptr);
120 tdb_speed = count/timeval_elapsed(&tv);
121 torture_comment(torture, "tdb speed %.2f ops/sec\n", tdb_speed);
123 talloc_free(tmp_ctx);
124 unlink("test.tdb");
125 return true;
127 failed:
128 talloc_free(tmp_ctx);
129 unlink("test.tdb");
130 return false;
134 static bool ldb_add_record(struct ldb_context *ldb, unsigned rid)
136 struct ldb_message *msg;
137 int ret;
139 msg = ldb_msg_new(ldb);
140 if (msg == NULL) {
141 return false;
144 msg->dn = ldb_dn_new_fmt(msg, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid);
145 if (msg->dn == NULL) {
146 talloc_free(msg);
147 return false;
150 ret = ldb_msg_add_fmt(msg, "UID", "%u", rid);
151 if (ret != LDB_SUCCESS) {
152 talloc_free(msg);
153 return false;
156 ret = ldb_add(ldb, msg);
158 talloc_free(msg);
160 return ret == LDB_SUCCESS;
165 test ldb speed
167 static bool test_ldb_speed(struct torture_context *torture, const void *_data)
169 struct timeval tv;
170 struct ldb_context *ldb;
171 int timelimit = torture_setting_int(torture, "timelimit", 10);
172 int i, count;
173 TALLOC_CTX *tmp_ctx = talloc_new(torture);
174 struct ldb_ldif *ldif;
175 const char *init_ldif = "dn: @INDEXLIST\n" \
176 "@IDXATTR: UID\n";
177 float ldb_speed;
179 unlink("./test.ldb");
181 torture_comment(torture, "Testing ldb speed for sidmap\n");
183 ldb = ldb_wrap_connect(tmp_ctx, torture->ev, torture->lp_ctx, "tdb://test.ldb",
184 NULL, NULL, LDB_FLG_NOSYNC);
185 if (!ldb) {
186 torture_result(torture, TORTURE_FAIL, "Failed to open test.ldb");
187 goto failed;
190 /* add an index */
191 ldif = ldb_ldif_read_string(ldb, &init_ldif);
192 if (ldif == NULL) {
193 torture_result(torture, TORTURE_FAIL, "Didn't get LDIF data!");
194 goto failed;
196 if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) {
197 torture_result(torture, TORTURE_FAIL, "Couldn't apply LDIF data!");
198 talloc_free(ldif);
199 goto failed;
201 talloc_free(ldif);
203 torture_comment(torture, "Adding %d SID records\n", torture_entries);
205 for (i=0;i<torture_entries;i++) {
206 if (!ldb_add_record(ldb, i)) {
207 torture_result(torture, TORTURE_FAIL, "Failed to add SID %d", i);
208 goto failed;
212 if (talloc_total_blocks(tmp_ctx) > 100) {
213 torture_result(torture, TORTURE_FAIL, "memory leak in ldb add");
214 goto failed;
217 torture_comment(torture, "Testing for %d seconds\n", timelimit);
219 tv = timeval_current();
221 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
222 struct ldb_dn *dn;
223 struct ldb_result *res;
225 i = random() % torture_entries;
226 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
227 if (ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL) != LDB_SUCCESS || res->count != 1) {
228 torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
229 goto failed;
231 talloc_free(res);
232 talloc_free(dn);
233 if (ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(UID=%u)", i) != LDB_SUCCESS || res->count != 1) {
234 torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
235 goto failed;
237 talloc_free(res);
240 if (talloc_total_blocks(tmp_ctx) > 100) {
241 torture_result(torture, TORTURE_FAIL, "memory leak in ldb search");
242 goto failed;
245 ldb_speed = count/timeval_elapsed(&tv);
246 torture_comment(torture, "ldb speed %.2f ops/sec\n", ldb_speed);
248 torture_comment(torture, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed/tdb_speed));
250 talloc_free(tmp_ctx);
251 unlink("./test.ldb");
252 return true;
254 failed:
255 talloc_free(tmp_ctx);
256 unlink("./test.ldb");
257 return false;
260 struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
262 struct torture_suite *s = torture_suite_create(mem_ctx, "dbspeed");
263 torture_suite_add_simple_tcase_const(s, "tdb_speed", test_tdb_speed,
264 NULL);
265 torture_suite_add_simple_tcase_const(s, "ldb_speed", test_ldb_speed,
266 NULL);
267 return s;