tdb_wrap: Remove tdb_wrap_open_ again
[samba.git] / source4 / torture / local / dbspeed.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local test for tdb/ldb speed
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "tdb_compat.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 "param/param.h"
31
32 float tdb_speed;
33
34 static bool tdb_add_record(struct tdb_wrap *tdbw, const char *fmt1, 
35                            const char *fmt2, int i)
36 {
37         TDB_DATA key, data;
38         int ret;
39
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;
44
45         ret = tdb_store(tdbw->tdb, key, data, TDB_INSERT);
46
47         talloc_free(key.dptr);
48         talloc_free(data.dptr);
49         return ret == 0;
50 }
51
52 /*
53   test tdb speed
54 */
55 static bool test_tdb_speed(struct torture_context *torture, const void *_data)
56 {
57         struct timeval tv;
58         struct tdb_wrap *tdbw;
59         int timelimit = torture_setting_int(torture, "timelimit", 10);
60         int i, count;
61         TALLOC_CTX *tmp_ctx = talloc_new(torture);
62
63         unlink("test.tdb");
64
65         torture_comment(torture, "Testing tdb speed for sidmap\n");
66
67         tdbw = tdb_wrap_open(tmp_ctx, "test.tdb", 10000,
68                              lpcfg_tdb_flags(torture->lp_ctx, 0),
69                              O_RDWR|O_CREAT|O_TRUNC, 0600);
70         if (!tdbw) {
71                 torture_result(torture, TORTURE_FAIL, "Failed to open test.tdb");
72                 goto failed;
73         }
74
75         torture_comment(torture, "Adding %d SID records\n", torture_entries);
76
77         for (i=0;i<torture_entries;i++) {
78                 if (!tdb_add_record(tdbw, 
79                                     "S-1-5-21-53173311-3623041448-2049097239-%u",
80                                     "UID %u", i)) {
81                         torture_result(torture, TORTURE_FAIL, "Failed to add SID %d!", i);
82                         goto failed;
83                 }
84                 if (!tdb_add_record(tdbw, 
85                                     "UID %u",
86                                     "S-1-5-21-53173311-3623041448-2049097239-%u", i)) {
87                         torture_result(torture, TORTURE_FAIL, "Failed to add UID %d!", i);
88                         goto failed;
89                 }
90         }
91
92         torture_comment(torture, "Testing for %d seconds\n", timelimit);
93
94         tv = timeval_current();
95
96         for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
97                 TDB_DATA key, data;
98                 i = random() % torture_entries;
99                 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "S-1-5-21-53173311-3623041448-2049097239-%u", i);
100                 key.dsize = strlen((char *)key.dptr)+1;
101                 data = tdb_fetch_compat(tdbw->tdb, key);
102                 talloc_free(key.dptr);
103                 if (data.dptr == NULL) {
104                         torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
105                         goto failed;
106                 }
107                 free(data.dptr);
108                 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "UID %u", i);
109                 key.dsize = strlen((char *)key.dptr)+1;
110                 data = tdb_fetch_compat(tdbw->tdb, key);
111                 talloc_free(key.dptr);
112                 if (data.dptr == NULL) {
113                         torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
114                         goto failed;
115                 }
116                 free(data.dptr);
117         }
118
119         tdb_speed = count/timeval_elapsed(&tv);
120         torture_comment(torture, "tdb speed %.2f ops/sec\n", tdb_speed);
121         
122         talloc_free(tmp_ctx);
123         unlink("test.tdb");
124         return true;
125
126 failed:
127         talloc_free(tmp_ctx);
128         unlink("test.tdb");
129         return false;
130 }
131
132
133 static bool ldb_add_record(struct ldb_context *ldb, unsigned rid)
134 {
135         struct ldb_message *msg;        
136         int ret;
137
138         msg = ldb_msg_new(ldb);
139         if (msg == NULL) {
140                 return false;
141         }
142
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                 talloc_free(msg);
146                 return false;
147         }
148
149         ret = ldb_msg_add_fmt(msg, "UID", "%u", rid);
150         if (ret != LDB_SUCCESS) {
151                 talloc_free(msg);
152                 return false;
153         }
154
155         ret = ldb_add(ldb, msg);
156
157         talloc_free(msg);
158
159         return ret == LDB_SUCCESS;
160 }
161
162
163 /*
164   test ldb speed
165 */
166 static bool test_ldb_speed(struct torture_context *torture, const void *_data)
167 {
168         struct timeval tv;
169         struct ldb_context *ldb;
170         int timelimit = torture_setting_int(torture, "timelimit", 10);
171         int i, count;
172         TALLOC_CTX *tmp_ctx = talloc_new(torture);
173         struct ldb_ldif *ldif;
174         const char *init_ldif = "dn: @INDEXLIST\n" \
175                 "@IDXATTR: UID\n";
176         float ldb_speed;
177
178         unlink("./test.ldb");
179
180         torture_comment(torture, "Testing ldb speed for sidmap\n");
181
182         ldb = ldb_wrap_connect(tmp_ctx, torture->ev, torture->lp_ctx, "tdb://test.ldb",
183                                 NULL, NULL, LDB_FLG_NOSYNC);
184         if (!ldb) {
185                 torture_result(torture, TORTURE_FAIL, "Failed to open test.ldb");
186                 goto failed;
187         }
188
189         /* add an index */
190         ldif = ldb_ldif_read_string(ldb, &init_ldif);
191         if (ldif == NULL) {
192                 torture_result(torture, TORTURE_FAIL, "Didn't get LDIF data!");
193                 goto failed;
194         }
195         if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) {
196                 torture_result(torture, TORTURE_FAIL, "Couldn't apply LDIF data!");
197                 talloc_free(ldif);
198                 goto failed;
199         }
200         talloc_free(ldif);
201
202         torture_comment(torture, "Adding %d SID records\n", torture_entries);
203
204         for (i=0;i<torture_entries;i++) {
205                 if (!ldb_add_record(ldb, i)) {
206                         torture_result(torture, TORTURE_FAIL, "Failed to add SID %d", i);
207                         goto failed;
208                 }
209         }
210
211         if (talloc_total_blocks(tmp_ctx) > 100) {
212                 torture_result(torture, TORTURE_FAIL, "memory leak in ldb add");
213                 goto failed;
214         }
215
216         torture_comment(torture, "Testing for %d seconds\n", timelimit);
217
218         tv = timeval_current();
219
220         for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
221                 struct ldb_dn *dn;
222                 struct ldb_result *res;
223
224                 i = random() % torture_entries;
225                 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
226                 if (ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL) != LDB_SUCCESS || res->count != 1) {
227                         torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
228                         goto failed;
229                 }
230                 talloc_free(res);
231                 talloc_free(dn);
232                 if (ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(UID=%u)", i) != LDB_SUCCESS || res->count != 1) {
233                         torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
234                         goto failed;
235                 }
236                 talloc_free(res);
237         }
238
239         if (talloc_total_blocks(tmp_ctx) > 100) {
240                 torture_result(torture, TORTURE_FAIL, "memory leak in ldb search");
241                 goto failed;
242         }
243
244         ldb_speed = count/timeval_elapsed(&tv);
245         torture_comment(torture, "ldb speed %.2f ops/sec\n", ldb_speed);
246
247         torture_comment(torture, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed/tdb_speed));
248         
249         talloc_free(tmp_ctx);
250         unlink("./test.ldb");
251         return true;
252
253 failed:
254         talloc_free(tmp_ctx);
255         unlink("./test.ldb");
256         return false;
257 }
258
259 struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
260 {
261         struct torture_suite *s = torture_suite_create(mem_ctx, "dbspeed");
262         torture_suite_add_simple_tcase_const(s, "tdb_speed", test_tdb_speed,
263                         NULL);
264         torture_suite_add_simple_tcase_const(s, "ldb_speed", test_ldb_speed,
265                         NULL);
266         return s;
267 }