]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tools/growtdb-bench.c
tdb2: Add tools/tdb2dump, tools/tdb2restore, use "tdb2.h" includes.
[ccan] / ccan / tdb2 / tools / growtdb-bench.c
1 #include "tdb2.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <err.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 static void logfn(struct tdb_context *tdb,
12                   enum tdb_log_level level,
13                   const char *message,
14                   void *data)
15 {
16         fprintf(stderr, "tdb:%s:%s\n", tdb_name(tdb), message);
17 }
18
19 int main(int argc, char *argv[])
20 {
21         unsigned int i, j, users, groups;
22         TDB_DATA idxkey, idxdata;
23         TDB_DATA k, d, gk;
24         char cmd[100];
25         struct tdb_context *tdb;
26         enum TDB_ERROR ecode;
27         union tdb_attribute log;
28
29         if (argc != 3) {
30                 printf("Usage: growtdb-bench <users> <groups>\n");
31                 exit(1);
32         }
33         users = atoi(argv[1]);
34         groups = atoi(argv[2]);
35
36         sprintf(cmd, "cat /proc/%i/statm", getpid());
37
38         log.base.attr = TDB_ATTRIBUTE_LOG;
39         log.base.next = NULL;
40         log.log.fn = logfn;
41         
42         tdb = tdb_open("/tmp/growtdb.tdb", TDB_DEFAULT,
43                        O_RDWR|O_CREAT|O_TRUNC, 0600, &log);
44
45         idxkey.dptr = (unsigned char *)"User index";
46         idxkey.dsize = strlen("User index");
47         idxdata.dsize = 51;
48         idxdata.dptr = calloc(idxdata.dsize, 1);
49
50         /* Create users. */
51         k.dsize = 48;
52         k.dptr = calloc(k.dsize, 1);
53         d.dsize = 64;
54         d.dptr = calloc(d.dsize, 1);
55
56         tdb_transaction_start(tdb);
57         for (i = 0; i < users; i++) {
58                 memcpy(k.dptr, &i, sizeof(i));
59                 ecode = tdb_store(tdb, k, d, TDB_INSERT);
60                 if (ecode != TDB_SUCCESS)
61                         errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
62
63                 /* This simulates a growing index record. */
64                 ecode = tdb_append(tdb, idxkey, idxdata);
65                 if (ecode != TDB_SUCCESS)
66                         errx(1, "tdb append failed: %s", tdb_errorstr(ecode));
67         }
68         if ((ecode = tdb_transaction_commit(tdb)) != 0)
69                 errx(1, "tdb commit1 failed: %s", tdb_errorstr(ecode));
70
71         if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
72                 errx(1, "tdb_check failed after initial insert!");
73
74         system(cmd);
75
76         /* Now put them all in groups: add 32 bytes to each record for
77          * a group. */
78         gk.dsize = 48;
79         gk.dptr = calloc(k.dsize, 1);
80         gk.dptr[gk.dsize-1] = 1;
81
82         d.dsize = 32;
83         for (i = 0; i < groups; i++) {
84                 tdb_transaction_start(tdb);
85                 /* Create the "group". */
86                 memcpy(gk.dptr, &i, sizeof(i));
87                 ecode = tdb_store(tdb, gk, d, TDB_INSERT);
88                 if (ecode != TDB_SUCCESS)
89                         errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
90
91                 /* Now populate it. */
92                 for (j = 0; j < users; j++) {
93                         /* Append to the user. */
94                         memcpy(k.dptr, &j, sizeof(j));
95                         if ((ecode = tdb_append(tdb, k, d)) != 0)
96                                 errx(1, "tdb append failed: %s",
97                                      tdb_errorstr(ecode));
98                         
99                         /* Append to the group. */
100                         if ((ecode = tdb_append(tdb, gk, d)) != 0)
101                                 errx(1, "tdb append failed: %s",
102                                      tdb_errorstr(ecode));
103                 }
104                 if ((ecode = tdb_transaction_commit(tdb)) != 0)
105                         errx(1, "tdb commit2 failed: %s", tdb_errorstr(ecode));
106                 if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
107                         errx(1, "tdb_check failed after iteration %i!", i);
108                 system(cmd);
109         }
110
111         return 0;
112 }