smbd: Remove get_Protocol()
[Samba.git] / lib / tdb / tools / tdbtest.c
blob0be35dc30383b8c70da46f8af08eeeb5c05ee34a
1 /* a test program for tdb - the trivial database */
3 #include "replace.h"
4 #include "tdb.h"
5 #include "system/filesys.h"
6 #include "system/time.h"
8 #include <gdbm.h>
11 #define DELETE_PROB 7
12 #define STORE_PROB 5
14 static struct tdb_context *db;
15 static GDBM_FILE gdbm;
17 struct timeval tp1,tp2;
19 static void _start_timer(void)
21 gettimeofday(&tp1,NULL);
24 static double _end_timer(void)
26 gettimeofday(&tp2,NULL);
27 return((tp2.tv_sec - tp1.tv_sec) +
28 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
31 static void fatal(const char *why)
33 perror(why);
34 exit(1);
37 #ifdef PRINTF_ATTRIBUTE
38 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
39 #endif
40 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...)
42 va_list ap;
44 va_start(ap, format);
45 vfprintf(stdout, format, ap);
46 va_end(ap);
47 fflush(stdout);
50 static void compare_db(void)
52 TDB_DATA d, key, nextkey;
53 datum gd, gkey, gnextkey;
55 key = tdb_firstkey(db);
56 while (key.dptr) {
57 d = tdb_fetch(db, key);
58 gkey.dptr = key.dptr;
59 gkey.dsize = key.dsize;
61 gd = gdbm_fetch(gdbm, gkey);
63 if (!gd.dptr) fatal("key not in gdbm");
64 if (gd.dsize != d.dsize) fatal("data sizes differ");
65 if (memcmp(gd.dptr, d.dptr, d.dsize)) {
66 fatal("data differs");
69 nextkey = tdb_nextkey(db, key);
70 free(key.dptr);
71 free(d.dptr);
72 free(gd.dptr);
73 key = nextkey;
76 gkey = gdbm_firstkey(gdbm);
77 while (gkey.dptr) {
78 gd = gdbm_fetch(gdbm, gkey);
79 key.dptr = gkey.dptr;
80 key.dsize = gkey.dsize;
82 d = tdb_fetch(db, key);
84 if (!d.dptr) fatal("key not in db");
85 if (d.dsize != gd.dsize) fatal("data sizes differ");
86 if (memcmp(d.dptr, gd.dptr, gd.dsize)) {
87 fatal("data differs");
90 gnextkey = gdbm_nextkey(gdbm, gkey);
91 free(gkey.dptr);
92 free(gd.dptr);
93 free(d.dptr);
94 gkey = gnextkey;
98 static char *randbuf(int len)
100 char *buf;
101 int i;
102 buf = (char *)malloc(len+1);
104 for (i=0;i<len;i++) {
105 buf[i] = 'a' + (rand() % 26);
107 buf[i] = 0;
108 return buf;
111 static void addrec_db(void)
113 int klen, dlen;
114 char *k, *d;
115 TDB_DATA key, data;
117 klen = 1 + (rand() % 4);
118 dlen = 1 + (rand() % 100);
120 k = randbuf(klen);
121 d = randbuf(dlen);
123 key.dptr = k;
124 key.dsize = klen+1;
126 data.dptr = d;
127 data.dsize = dlen+1;
129 if (rand() % DELETE_PROB == 0) {
130 tdb_delete(db, key);
131 } else if (rand() % STORE_PROB == 0) {
132 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
133 fatal("tdb_store failed");
135 } else {
136 data = tdb_fetch(db, key);
137 if (data.dptr) free(data.dptr);
140 free(k);
141 free(d);
144 static void addrec_gdbm(void)
146 int klen, dlen;
147 char *k, *d;
148 datum key, data;
150 klen = 1 + (rand() % 4);
151 dlen = 1 + (rand() % 100);
153 k = randbuf(klen);
154 d = randbuf(dlen);
156 key.dptr = k;
157 key.dsize = klen+1;
159 data.dptr = d;
160 data.dsize = dlen+1;
162 if (rand() % DELETE_PROB == 0) {
163 gdbm_delete(gdbm, key);
164 } else if (rand() % STORE_PROB == 0) {
165 if (gdbm_store(gdbm, key, data, GDBM_REPLACE) != 0) {
166 fatal("gdbm_store failed");
168 } else {
169 data = gdbm_fetch(gdbm, key);
170 if (data.dptr) free(data.dptr);
173 free(k);
174 free(d);
177 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
179 #if 0
180 printf("[%s] [%s]\n", key.dptr, dbuf.dptr);
181 #endif
182 tdb_delete(tdb, key);
183 return 0;
186 static void merge_test(void)
188 int i;
189 char keys[5][2];
190 char tdata[] = "test";
191 TDB_DATA key, data;
193 for (i = 0; i < 5; i++) {
194 snprintf(keys[i],2, "%d", i);
195 key.dptr = keys[i];
196 key.dsize = 2;
198 data.dptr = tdata;
199 data.dsize = 4;
201 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
202 fatal("tdb_store failed");
206 key.dptr = keys[0];
207 tdb_delete(db, key);
208 key.dptr = keys[4];
209 tdb_delete(db, key);
210 key.dptr = keys[2];
211 tdb_delete(db, key);
212 key.dptr = keys[1];
213 tdb_delete(db, key);
214 key.dptr = keys[3];
215 tdb_delete(db, key);
218 static char *test_path(const char *filename)
220 const char *prefix = getenv("TEST_DATA_PREFIX");
222 if (prefix) {
223 char *path = NULL;
224 int ret;
226 ret = asprintf(&path, "%s/%s", prefix, filename);
227 if (ret == -1) {
228 return NULL;
230 return path;
233 return strdup(filename);
236 int main(int argc, const char *argv[])
238 int i, seed=0;
239 int loops = 10000;
240 int num_entries;
241 char test_gdbm[1] = "test.gdbm";
242 char *test_tdb;
244 test_gdbm[0] = test_path("test.gdbm");
245 test_tdb = test_path("test.tdb");
247 unlink(test_gdbm[0]);
249 db = tdb_open(test_tdb, 0, TDB_CLEAR_IF_FIRST,
250 O_RDWR | O_CREAT | O_TRUNC, 0600);
251 gdbm = gdbm_open(test_gdbm, 512, GDBM_WRITER|GDBM_NEWDB|GDBM_FAST,
252 0600, NULL);
254 if (!db || !gdbm) {
255 fatal("db open failed");
258 #if 1
259 srand(seed);
260 _start_timer();
261 for (i=0;i<loops;i++) addrec_gdbm();
262 printf("gdbm got %.2f ops/sec\n", i/_end_timer());
263 #endif
265 merge_test();
267 srand(seed);
268 _start_timer();
269 for (i=0;i<loops;i++) addrec_db();
270 printf("tdb got %.2f ops/sec\n", i/_end_timer());
272 if (tdb_validate_freelist(db, &num_entries) == -1) {
273 printf("tdb freelist is corrupt\n");
274 } else {
275 printf("tdb freelist is good (%d entries)\n", num_entries);
278 compare_db();
280 printf("traversed %d records\n", tdb_traverse(db, traverse_fn, NULL));
281 printf("traversed %d records\n", tdb_traverse(db, traverse_fn, NULL));
283 tdb_close(db);
284 gdbm_close(gdbm);
286 free(test_gdbm[0]);
287 free(test_tdb);
289 return 0;