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