2 * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 typedef struct RDBMSDesc {
49 ftsDB* (*init)(char *);
52 static RDBMSDesc DBDesc[] = {
53 { PostgreSQL, "pgsql", "PostgreSQL", PGInit },
54 { MySQL, "mysql", "MySQL", MYInit },
55 { NULLSQL, NULL, NULL, NULL }
64 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
65 if ( DBDesc[i].init == NULL )
69 strcat(buf, DBDesc[i].shortname);
71 strcat(buf, "(default)");
76 "ftsbench - full text search benchmark for RDBMS\n"
77 "Initialization of DB:\n"
78 "ftsbench -i [-b RDBMS] [-n NUMROW] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q] -d DBNAME\n"
79 " -b RDBMS\t- type of DB: ",
85 " -n NUMROW - number of row in table\n"
86 " -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
87 " -g GAMMAFILE - file with doc's length distribution (default gendata/gamma-lens)\n"
88 " -l FLGAS - options for db's schema (see below)\n"
89 " -q - do not print progress message\n",
94 "ftsbench [-b RDBMS] [-c NCLIENTS] [-n NUMQUERY] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q] -d DBNAME\n"
95 " -b RDBMS\t- type of DB: ",
101 " -c NCLIENTS - number of clients in parallel\n"
102 " -n NUMQUERY - number of queries per client\n"
103 " -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
104 " -g GAMMAFILE - file with doc's length distribution (default gendata/query-lens)\n"
105 " -l FLGAS - options for db's schema (see below)\n"
106 " -q - do not print progress message\n",
110 "FLAGS are comma-separate list of:\n"
111 " gin - use GIN index\n"
112 " gist - use GiST index\n"
113 " func - use functional index\n"
114 " and - AND'ing lexemes in query (default)\n"
115 " or - OR'ing lexemes in query\n",
122 getRDBMS(char *name) {
125 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
126 if ( name == NULL ) {
127 if ( DBDesc[i].init )
128 return DBDesc[i].rdbms;
129 } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
130 if ( DBDesc[i].init == NULL ) {
131 fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
134 return DBDesc[i].rdbms;
138 fprintf(stderr,"Can't find a RDBMS\n");
145 getFLAGS(char *flg) {
148 if ( strcasestr(flg,"gist") )
150 if ( strcasestr(flg,"gin") )
152 if ( strcasestr(flg,"func") )
154 if ( strcasestr(flg,"and") )
156 if ( strcasestr(flg,"or") )
159 if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
160 fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
163 if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
164 fprintf(stderr,"AND and OR flags are mutually exclusive\n");
172 initConnections(RDBMS rdbms, int n, char *connstr) {
173 ftsDB **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
177 fprintf(stderr,"Not enough mwmory\n");
182 dbs[i] = DBDesc[rdbms].init(connstr);
183 pthread_mutex_init(&dbs[i]->nqueryMutex, NULL);
190 timediff(struct timeval *begin, struct timeval *end) {
191 return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6;
195 elapsedtime(struct timeval *begin) {
197 gettimeofday(&end,NULL);
198 return timediff(begin,&end);
201 static int benchFlags = 0;
202 static int benchCount = 0;
203 static pthread_cond_t condFinish = PTHREAD_COND_INITIALIZER;
204 static pthread_mutex_t mutexFinish = PTHREAD_MUTEX_INITIALIZER;
205 static pthread_mutex_t mutexWordGen = PTHREAD_MUTEX_INITIALIZER;
208 execBench(void *in) {
209 ftsDB *db = (ftsDB*)in;
213 for(i=0;i<benchCount;i++) {
215 * generate_querywords() isn't a thread safe
217 pthread_mutex_lock( &mutexWordGen );
218 words = generate_querywords();
219 pthread_mutex_unlock( &mutexWordGen );
221 db->execQuery(db, words, benchFlags);
226 * send message about exitting
228 pthread_mutex_lock( &mutexFinish );
229 pthread_cond_broadcast( &condFinish );
230 pthread_mutex_unlock( &mutexFinish );
238 main(int argn, char *argv[]) {
240 int n = 0, nclients = 1;
244 RDBMS rdbms = NULLSQL;
248 StringBuf b = {NULL,0,0};
250 while((i=getopt(argn,argv,"ib:n:l:g:d:c:hf:q")) != EOF) {
252 case 'i': initMode = 1; break;
253 case 'b': rdbms = getRDBMS(optarg); break;
254 case 'n': n=atoi(optarg); break;
255 case 'c': nclients=atoi(optarg); break;
256 case 'l': lex = strdup(optarg); break;
257 case 'g': doc = strdup(optarg); break;
258 case 'd': dbname = strdup(optarg); break;
259 case 'f': flags = getFLAGS(optarg); break;
260 case 'q': quiet = 1; break;
267 if (rdbms == NULLSQL)
268 rdbms = getRDBMS(NULL);
270 if ( dbname == NULL || n<0 || nclients<1 )
273 printf("Running with '%s' RDBMS\n", DBDesc[ rdbms ].longname);
276 ftsDB *db = *initConnections(rdbms, 1, dbname);
279 if (!lex) lex = "gendata/lex";
280 if (!doc) doc = "gendata/gamma-lens";
281 finnegan_init(lex, doc);
283 db->startCreateScheme(db, flags);
287 db->InsertRow(db, i+1, b.str);
288 if ( !quiet && prev!=time(NULL) ) {
289 printf("\r%d(%.02f%%) rows inserted", i, (100.0*i)/n);
294 printf("%s%d(100.00%%) rows inserted. Finalyze insertion... ",
295 (quiet) ? "" : "\r", i);
297 db->finishCreateScheme(db);
301 ftsDB **dbs = initConnections(rdbms, nclients, dbname);
302 pthread_t *tid = (pthread_t*)malloc( sizeof(pthread_t) * nclients);
303 struct timeval begin;
306 struct timespec sleepTo = { 0, 0 };
311 if (!lex) lex = "gendata/query-lex";
312 if (!doc) doc = "gendata/query-lens";
313 finnegan_init(lex, doc);
319 printf("\r0(0.00%%) queries proceed");
325 gettimeofday(&begin,NULL);
327 pthread_mutex_lock( &mutexFinish );
328 for(i=0;i<nclients;i++) {
329 if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
330 fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
339 for(i=0;i<nclients;i++) {
340 pthread_mutex_lock(&dbs[i]->nqueryMutex);
341 total +=dbs[i]->nquery;
342 if ( dbs[i]->nquery < n )
344 pthread_mutex_unlock(&dbs[i]->nqueryMutex);
351 printf("\r%d(%.02f%%) queries proceed", total, (100.0*(float)total)/(nclients * n));
355 sleepTo.tv_sec = time(NULL) + 1;
356 res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
358 if ( !(res == ETIMEDOUT || res == 0) ) {
359 fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
363 elapsed = elapsedtime(&begin);
364 pthread_mutex_unlock( &mutexFinish );
366 for(i=0;i<nclients;i++) {
367 pthread_join(tid[i], NULL);
368 nres += dbs[i]->nres;
369 dbs[i]->Close(dbs[i]);
372 printf("%s%d(%.02f%%) queries proceed\n",
373 (quiet) ? "" : "\r", total, (100.0*(float)total)/(nclients * n));
374 printf("Total number of result: %d\n", nres);
375 printf("Total time: %.02f sec, Queries per second: %.02f\n", elapsed, total/elapsed);