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