1 | /* Test code for libibex */ |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <errno.h> |
---|
5 | #include <string.h> |
---|
6 | #include <glib.h> |
---|
7 | #include "ibex_internal.h" |
---|
8 | |
---|
9 | #ifdef ENABLE_THREADS |
---|
10 | #include <pthread.h> |
---|
11 | #endif |
---|
12 | |
---|
13 | #define TIMEIT |
---|
14 | /*#define DO_MCHECK*/ |
---|
15 | |
---|
16 | #ifdef TIMEIT |
---|
17 | #include <sys/time.h> |
---|
18 | #include <unistd.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #ifdef DO_MCHECK |
---|
22 | #include <mcheck.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | void word_index_mem_dump_info(struct _IBEXWord *idx); |
---|
26 | |
---|
27 | /* |
---|
28 | The following is a routine to generate a Gaussian distribution |
---|
29 | of pseudo random numbers, to make the results a little more |
---|
30 | meaningful |
---|
31 | */ |
---|
32 | |
---|
33 | /* boxmuller.c Implements the Polar form of the Box-Muller |
---|
34 | Transformation |
---|
35 | |
---|
36 | (c) Copyright 1994, Everett F. Carter Jr. |
---|
37 | Permission is granted by the author to use |
---|
38 | this software for any application provided this |
---|
39 | copyright notice is preserved. |
---|
40 | |
---|
41 | */ |
---|
42 | |
---|
43 | #include <stdlib.h> |
---|
44 | #include <math.h> |
---|
45 | |
---|
46 | #define ranf() ((float)rand()/(float)RAND_MAX) |
---|
47 | |
---|
48 | static float box_muller(float m, float s) /* normal random variate generator */ |
---|
49 | { /* mean m, standard deviation s */ |
---|
50 | float x1, x2, w, y1; |
---|
51 | static float y2; |
---|
52 | static int use_last = 0; |
---|
53 | |
---|
54 | if (use_last) /* use value from previous call */ |
---|
55 | { |
---|
56 | y1 = y2; |
---|
57 | use_last = 0; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | do { |
---|
62 | x1 = 2.0 * ranf() - 1.0; |
---|
63 | x2 = 2.0 * ranf() - 1.0; |
---|
64 | w = x1 * x1 + x2 * x2; |
---|
65 | } while ( w >= 1.0 ); |
---|
66 | |
---|
67 | w = sqrt( (-2.0 * log( w ) ) / w ); |
---|
68 | y1 = x1 * w; |
---|
69 | y2 = x2 * w; |
---|
70 | use_last = 1; |
---|
71 | } |
---|
72 | |
---|
73 | return( m + y1 * s ); |
---|
74 | } |
---|
75 | |
---|
76 | /* gets a word from words, using m and s as distribution values */ |
---|
77 | static char *getword(GPtrArray *words, float m, float s) |
---|
78 | { |
---|
79 | int index; |
---|
80 | |
---|
81 | do { |
---|
82 | index = (int)box_muller(m, s); |
---|
83 | } while (index<0 || index>=words->len); |
---|
84 | |
---|
85 | return words->pdata[index]; |
---|
86 | } |
---|
87 | |
---|
88 | #ifdef ENABLE_THREADS |
---|
89 | int do_read_words; |
---|
90 | |
---|
91 | static void * |
---|
92 | read_words(void *in) |
---|
93 | { |
---|
94 | ibex *ib = in; |
---|
95 | GPtrArray *a; |
---|
96 | int lastlen = 0; |
---|
97 | int i; |
---|
98 | |
---|
99 | while (do_read_words) { |
---|
100 | a = ibex_find(ib, "joneses"); |
---|
101 | if (a->len != lastlen) { |
---|
102 | printf("Found %d joneses!\n", a->len); |
---|
103 | lastlen = a->len; |
---|
104 | } |
---|
105 | for (i=0;i<a->len;i++) |
---|
106 | g_free(a->pdata[i]); |
---|
107 | g_ptr_array_free(a, TRUE); |
---|
108 | } |
---|
109 | } |
---|
110 | #endif |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | #ifdef DO_MCHECK |
---|
115 | static int blowup(int status) |
---|
116 | { |
---|
117 | switch(status) { |
---|
118 | case 1: |
---|
119 | printf("Double free failure\n"); |
---|
120 | break; |
---|
121 | case 2: |
---|
122 | printf("Memory clobbered before block\n"); |
---|
123 | break; |
---|
124 | case 3: |
---|
125 | printf("Memory clobbered after block\n"); |
---|
126 | break; |
---|
127 | } |
---|
128 | abort(); |
---|
129 | return status; |
---|
130 | } |
---|
131 | #endif |
---|
132 | |
---|
133 | int main(int argc, char **argv) |
---|
134 | { |
---|
135 | int i, j; |
---|
136 | GPtrArray *words; |
---|
137 | char line[256]; |
---|
138 | int len; |
---|
139 | FILE *file; |
---|
140 | float m, s; |
---|
141 | ibex *ib; |
---|
142 | GString *buffer; |
---|
143 | int files; |
---|
144 | char *dict; |
---|
145 | int synccount; |
---|
146 | #ifdef TIMEIT |
---|
147 | struct timeval start, end; |
---|
148 | unsigned long diff; |
---|
149 | #endif |
---|
150 | #ifdef ENABLE_THREADS |
---|
151 | pthread_t id; |
---|
152 | #endif |
---|
153 | |
---|
154 | #ifdef DO_MCHECK |
---|
155 | mcheck(blowup); |
---|
156 | #endif |
---|
157 | words = g_ptr_array_new(); |
---|
158 | buffer = g_string_new(""); |
---|
159 | |
---|
160 | #ifdef ENABLE_THREADS |
---|
161 | g_thread_init(0); |
---|
162 | #undef ENABLE_THREADS |
---|
163 | #endif |
---|
164 | |
---|
165 | #ifdef TIMEIT |
---|
166 | gettimeofday(&start, NULL); |
---|
167 | #endif |
---|
168 | |
---|
169 | srand(0xABADF00D); |
---|
170 | |
---|
171 | synccount = 1000; |
---|
172 | files = 8000; |
---|
173 | dict = "/usr/dict/words"; |
---|
174 | |
---|
175 | /* read words into an array */ |
---|
176 | file = fopen(dict, "r"); |
---|
177 | if (file == NULL) { |
---|
178 | fprintf(stderr, "Cannot open word file: %s: %s\n", dict, strerror(errno)); |
---|
179 | return 1; |
---|
180 | } |
---|
181 | while (fgets(line, sizeof(line), file) != NULL) { |
---|
182 | len = strlen(line); |
---|
183 | if (len>0 && line[len-1]=='\n') { |
---|
184 | line[len-1]=0; |
---|
185 | } |
---|
186 | g_ptr_array_add(words, g_strdup(line)); |
---|
187 | } |
---|
188 | fclose(file); |
---|
189 | |
---|
190 | fprintf(stderr, "Read %d words\n", words->len); |
---|
191 | |
---|
192 | /* *shrug* arbitrary values really */ |
---|
193 | m = words->len/2; |
---|
194 | /* well, the average vocabulary of a mailbox is about 10K words */ |
---|
195 | s = 1000.0; |
---|
196 | |
---|
197 | printf("mean is %f, s is %f\n", m, s); |
---|
198 | |
---|
199 | /* open ibex file */ |
---|
200 | ib = ibex_open("test.ibex", O_RDWR|O_CREAT, 0600); |
---|
201 | if (ib == NULL) { |
---|
202 | perror("Creating ibex file\n"); |
---|
203 | return 1; |
---|
204 | } |
---|
205 | |
---|
206 | #ifdef ENABLE_THREADS |
---|
207 | do_read_words = 1; |
---|
208 | pthread_create(&id, 0, read_words, ib); |
---|
209 | #endif |
---|
210 | printf("Adding %d files\n", files); |
---|
211 | |
---|
212 | |
---|
213 | /* simulate adding new words to a bunch of files */ |
---|
214 | for (j=0;j<200000;j++) { |
---|
215 | /* always new name */ |
---|
216 | char *name; |
---|
217 | /* something like 60 words in a typical message, say */ |
---|
218 | int count = (int)box_muller(60.0, 20.0); |
---|
219 | int word = (int)box_muller(m, 4000); |
---|
220 | GPtrArray *a; |
---|
221 | static int lastlen = 0; |
---|
222 | |
---|
223 | /* random name */ |
---|
224 | name = words->pdata[word % words->len]; |
---|
225 | |
---|
226 | if (j%1000 == 0 && j>0) { |
---|
227 | IBEX_LOCK(ib); |
---|
228 | word_index_mem_dump_info(ib->words); |
---|
229 | IBEX_UNLOCK(ib); |
---|
230 | } |
---|
231 | |
---|
232 | /* lookup word just to test lookup */ |
---|
233 | a = ibex_find(ib, name); |
---|
234 | if (a) { |
---|
235 | for (i=0;i<a->len;i++) |
---|
236 | g_free(a->pdata[i]); |
---|
237 | g_ptr_array_free(a, TRUE); |
---|
238 | } |
---|
239 | |
---|
240 | /* half the time, remove items from the index */ |
---|
241 | if (rand() < RAND_MAX/2) { |
---|
242 | ibex_unindex(ib, name); |
---|
243 | } else { |
---|
244 | /* cache the name info */ |
---|
245 | ibex_contains_name(ib, name); |
---|
246 | |
---|
247 | /*printf("Adding %d words to '%s'\n", count, name);*/ |
---|
248 | |
---|
249 | g_string_truncate(buffer, 0); |
---|
250 | |
---|
251 | /* build up the word buffer */ |
---|
252 | for (i=0;i<count;i++) { |
---|
253 | if (i>0) |
---|
254 | g_string_append_c(buffer, ' '); |
---|
255 | g_string_append(buffer, getword(words, m, 2000)); |
---|
256 | } |
---|
257 | |
---|
258 | /* and index it */ |
---|
259 | ibex_index_buffer(ib, name, buffer->str, buffer->len, NULL); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | a = ibex_find(ib, "joneses"); |
---|
264 | if (a) { |
---|
265 | if (a->len != lastlen) { |
---|
266 | printf("Found %d joneses!\n", a->len); |
---|
267 | lastlen = a->len; |
---|
268 | } |
---|
269 | for (i=0;i<a->len;i++) |
---|
270 | g_free(a->pdata[i]); |
---|
271 | g_ptr_array_free(a, TRUE); |
---|
272 | } |
---|
273 | |
---|
274 | if (j%synccount == 0) { |
---|
275 | printf("Reloading index\n"); |
---|
276 | IBEX_LOCK(ib); |
---|
277 | word_index_mem_dump_info(ib->words); |
---|
278 | IBEX_UNLOCK(ib); |
---|
279 | #ifdef ENABLE_THREADS |
---|
280 | do_read_words = 0; |
---|
281 | pthread_join(id, 0); |
---|
282 | #endif |
---|
283 | ibex_save(ib); |
---|
284 | ibex_close(ib); |
---|
285 | |
---|
286 | ib = ibex_open("test.ibex", O_RDWR|O_CREAT, 0600); |
---|
287 | ibex_contains_name(ib, name); |
---|
288 | IBEX_LOCK(ib); |
---|
289 | word_index_mem_dump_info(ib->words); |
---|
290 | IBEX_UNLOCK(ib); |
---|
291 | #ifdef ENABLE_THREADS |
---|
292 | do_read_words = 1; |
---|
293 | pthread_create(&id, 0, read_words, ib); |
---|
294 | #endif |
---|
295 | } |
---|
296 | |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | IBEX_LOCK(ib); |
---|
301 | word_index_mem_dump_info(ib->words); |
---|
302 | IBEX_UNLOCK(ib); |
---|
303 | |
---|
304 | #ifdef ENABLE_THREADS |
---|
305 | do_read_words = 0; |
---|
306 | pthread_join(id, 0); |
---|
307 | #endif |
---|
308 | |
---|
309 | ibex_close(ib); |
---|
310 | |
---|
311 | #ifdef TIMEIT |
---|
312 | gettimeofday(&end, NULL); |
---|
313 | diff = end.tv_sec * 1000 + end.tv_usec/1000; |
---|
314 | diff -= start.tv_sec * 1000 + start.tv_usec/1000; |
---|
315 | printf("Total time taken %ld.%03ld seconds\n", diff / 1000, diff % 1000); |
---|
316 | #endif |
---|
317 | |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|