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