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
tdb_add_record(struct tdb_wrap * tdbw,const char * fmt1,const char * fmt2,int i)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 */
test_tdb_speed(struct torture_context * torture,const void * _data)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 torture_fail(torture, "Failed to open test.tdb\n");
67 goto failed;
68 }
69
70 torture_comment(torture, "Adding %d SID records\n", 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_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", 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_result(torture, TORTURE_FAIL, "Failed to add UID %d\n", i);
83 goto failed;
84 }
85 }
86
87 torture_comment(torture, "Testing for %d seconds\n", 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_result(torture, TORTURE_FAIL, "Failed to fetch SID %d\n", 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_result(torture, TORTURE_FAIL, "Failed to fetch UID %d\n", i);
107 goto failed;
108 }
109 free(data.dptr);
110 }
111
112 torture_comment(torture, "tdb speed %.2f ops/sec\n", 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
ldb_add_record(struct ldb_context * ldb,unsigned rid)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_new_fmt(msg, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid);
137 if (msg->dn == NULL) {
138 return False;
139 }
140
141 if (ldb_msg_add_fmt(msg, "UID", "%u", rid) != 0) {
142 return False;
143 }
144
145 ret = ldb_add(ldb, msg);
146
147 talloc_free(msg);
148
149 return ret == LDB_SUCCESS;
150 }
151
152
153 /*
154 test ldb speed
155 */
test_ldb_speed(struct torture_context * torture,const void * _data)156 static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
157 {
158 struct timeval tv;
159 struct ldb_context *ldb;
160 int timelimit = torture_setting_int(torture, "timelimit", 10);
161 int i, count;
162 TALLOC_CTX *tmp_ctx = talloc_new(torture);
163 struct ldb_ldif *ldif;
164 const char *init_ldif = "dn: @INDEXLIST\n" \
165 "@IDXATTR: UID\n";
166
167 unlink("./test.ldb");
168
169 torture_comment(torture, "Testing ldb speed for sidmap\n");
170
171 ldb = ldb_wrap_connect(tmp_ctx, "tdb://test.ldb",
172 NULL, NULL, LDB_FLG_NOSYNC, NULL);
173 if (!ldb) {
174 torture_fail(torture, "Failed to open test.ldb\n");
175 goto failed;
176 }
177
178 /* add an index */
179 ldif = ldb_ldif_read_string(ldb, &init_ldif);
180 if (ldif == NULL) goto failed;
181 if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) goto failed;
182 talloc_free(ldif);
183
184 torture_comment(torture, "Adding %d SID records\n", torture_entries);
185
186 for (i=0;i<torture_entries;i++) {
187 if (!ldb_add_record(ldb, i)) {
188 torture_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", i);
189 goto failed;
190 }
191 }
192
193 if (talloc_total_blocks(torture) > 100) {
194 torture_result(torture, TORTURE_FAIL, "memory leak in ldb add\n");
195 goto failed;
196 }
197
198 torture_comment(torture, "Testing for %d seconds\n", timelimit);
199
200 tv = timeval_current();
201
202 for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
203 struct ldb_dn *dn;
204 struct ldb_result *res;
205 char *expr;
206
207 i = random() % torture_entries;
208 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
209 if (ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res) != LDB_SUCCESS || res->count != 1) {
210 torture_fail(torture, talloc_asprintf(torture, "Failed to find SID %d\n", i));
211 }
212 talloc_free(res);
213 talloc_free(dn);
214 expr = talloc_asprintf(tmp_ctx, "(UID=%u)", i);
215 if (ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res) != LDB_SUCCESS || res->count != 1) {
216 torture_fail(torture, talloc_asprintf(torture, "Failed to find UID %d\n", i));
217 }
218 talloc_free(res);
219 talloc_free(expr);
220 }
221
222 if (talloc_total_blocks(torture) > 100) {
223 torture_fail(torture, "memory leak in ldb search\n");
224 goto failed;
225 }
226
227 torture_comment(torture, "ldb speed %.2f ops/sec\n", count/timeval_elapsed(&tv));
228
229
230 unlink("./test.ldb");
231 talloc_free(tmp_ctx);
232 return True;
233
234 failed:
235 unlink("./test.ldb");
236 talloc_free(tmp_ctx);
237 return False;
238 }
239
torture_local_dbspeed(TALLOC_CTX * mem_ctx)240 struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
241 {
242 struct torture_suite *s = torture_suite_create(mem_ctx, "DBSPEED");
243 torture_suite_add_simple_tcase(s, "tdb_speed", test_tdb_speed, NULL);
244 torture_suite_add_simple_tcase(s, "ldb_speed", test_ldb_speed, NULL);
245 return s;
246 }
247