]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/tcdbtest.c
ebl Update about STAP
[bacula/bacula] / bacula / patches / testing / tcdbtest.c
1 /* 
2  export LD_LIBRARY_PATH=/home/eric/dev/bacula/tcdb/lib/
3  g++ -I/home/eric/dev/bacula/tcdb/include -o tt -L /home/eric/dev/bacula/tcdb/lib/ -ltokyocabinet -lz -lpthread -lm tcdbtest.c 
4
5 ./tt $(wc -l src.txt2) 0
6
7  */
8
9 #include <tcutil.h>
10 #include <tchdb.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <sys/time.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <signal.h>
20
21 #define NITEMS 5000000
22
23
24
25 void atend()
26 {
27    unlink("casket.hdb");
28 }
29
30 int64_t get_current_time()
31 {
32    struct timeval tv;
33    if (gettimeofday(&tv, NULL) != 0) {
34       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
35       tv.tv_usec = 0;
36    }
37    return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
38 }
39
40 typedef struct PrivateCurFile {
41    char *fname;                 /* not stored with tchdb mode */
42    time_t ctime;
43    time_t mtime;
44    bool seen;
45 } CurFile;
46
47 int main(int argc, char **argv)
48 {
49    CurFile elt;
50    FILE *fp, *res;
51    TCHDB *hdb;
52    int ecode;
53    char *key;
54    int value;
55    int i=0;
56    char save_key[512];
57    char line[512];
58    int64_t ctime, ttime;;
59    char result[200];
60
61    if (argc != 4) {
62       fprintf(stderr, "Usage: tt count file cache %i\n");
63       exit(1);
64    }
65
66    atexit(atend);
67    signal(15, exit);
68    signal(2, exit);
69
70    snprintf(result, sizeof(result), "result.%i.csv", getpid());
71    res = fopen(result, "w");
72    
73    /* create the object */
74    hdb = tchdbnew();
75
76    if (atoi(argv[3]) > 0) {
77       tchdbsetcache(hdb, atoi(argv[3]));
78    }
79    fprintf(res, "cache;%i\n", atoi(argv[3]));
80
81    /*
82     * apow : 128 (size of stat hash field)
83     */
84    int opt=HDBTLARGE | HDBTTCBS;
85    tchdbtune(hdb, atoll(argv[1]), 7, 16, opt);
86    fprintf(res, "bucket;%lli\n", atoll(argv[1]));
87    fprintf(res, "compress;%i\n", opt);
88
89    /* open the database */
90    if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
91       ecode = tchdbecode(hdb);
92       fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
93    }
94
95    ctime = get_current_time();
96
97    /* fill hash with real data (find / > src.txt) */
98    fp = fopen(argv[2], "r");
99    if (!fp) {
100       fprintf(stderr, "open %s file error\n", argv[2]);
101       exit (1);
102    }
103    while (fgets(line, sizeof(line), fp)) {
104       if (!tchdbputasync(hdb, line, strlen(line)+1, &elt, sizeof(elt))) {
105          ecode = tchdbecode(hdb);
106          fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
107       }
108       if (i++ == 99) {
109          strcpy(save_key, line);
110       }
111    }
112    fclose(fp);
113
114    ttime= get_current_time();
115    fprintf(res, "nbelt;%u\n", i);
116
117    fprintf(stderr, "loading %i file into hash database in %ims\n", 
118            i, (ttime - ctime)/1000);
119    fprintf(res, "load;%i\n", (ttime - ctime)/1000);
120
121
122    /* retrieve records */
123    value = tchdbget3(hdb, save_key, strlen(save_key)+1, &elt, sizeof(elt));
124    if(value == -1){
125       ecode = tchdbecode(hdb);
126       fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
127    }
128
129    /* retrieve all records and mark them as seen */
130    i=0;
131    fp = fopen(argv[2], "r");
132    if (!fp) {
133       fprintf(stderr, "open %s file error\n", argv[2]);
134       exit (1);
135    }
136    while (fgets(line, sizeof(line), fp)) {
137       if (i++ != 200) {
138          value = tchdbget3(hdb, line, strlen(line)+1, &elt, sizeof(elt));
139          if (value > 0) {
140             elt.seen=1;
141             if (!tchdbputasync(hdb, line, strlen(line)+1, &elt, sizeof(elt))) {
142                ecode = tchdbecode(hdb);
143                fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
144             }
145          } else {
146             fprintf(stderr, "can't find %s in hash\n", line);
147          }
148       }
149    }
150    fclose(fp);
151
152    ctime = get_current_time();
153    fprintf(stderr, "marking as seen in %ims\n", (ctime - ttime)/1000);
154    fprintf(res, "seen;%i\n", (ctime - ttime)/1000);
155
156   /* traverse records */
157   tchdbiterinit(hdb);
158   while((key = tchdbiternext2(hdb)) != NULL){
159          value = tchdbget3(hdb, key, strlen(key)+1, &elt, sizeof(elt));
160          if (value > 0) {
161             elt.seen=1; // check seen element
162          } else {
163             fprintf(stderr, "can't find %s in hash\n", line);
164          }
165   }
166
167    ttime = get_current_time();
168    fprintf(stderr, "checking not seen in %ims\n", (ttime - ctime)/1000);
169    fprintf(res, "walk;%i\n", (ttime - ctime)/1000);
170
171   /* close the database */
172   if(!tchdbclose(hdb)){
173     ecode = tchdbecode(hdb);
174     fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
175   }
176
177   /* delete the object */
178   tchdbdel(hdb);
179   struct stat statp;
180   stat("casket.hdb", &statp);
181   fprintf(res, "size;%lli\n", statp.st_size);
182   unlink("casket.hdb");
183   fclose(res);
184   return 0;
185 }