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