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 "Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
77 "ftsbench - full text search benchmark for RDBMS\n"
78 "Initialization of DB:\n"
79 "ftsbench -i [-b RDBMS] [-n NUMROW] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q] -d DBNAME\n"
80 " -b RDBMS\t- type of DB: ",
86 " -n NUMROW - number of row in table\n"
87 " -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
88 " -g GAMMAFILE - file with doc's length distribution (default gendata/gamma-lens)\n"
89 " -l FLGAS - options for db's schema (see below)\n"
90 " -q - do not print progress message\n",
95 "ftsbench [-b RDBMS] [-c NCLIENTS] [-n NUMQUERY] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q] -d DBNAME\n"
96 " -b RDBMS\t- type of DB: ",
102 " -c NCLIENTS - number of clients in parallel\n"
103 " -n NUMQUERY - number of queries per client\n"
104 " -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
105 " -g GAMMAFILE - file with doc's length distribution (default gendata/query-lens)\n"
106 " -l FLGAS - options for db's schema (see below)\n"
107 " -q - do not print progress message\n",
111 "FLAGS are comma-separate list of:\n"
112 " gin - use GIN index\n"
113 " gist - use GiST index\n"
114 " func - use functional index\n"
115 " and - AND'ing lexemes in query (default)\n"
116 " or - OR'ing lexemes in query\n",
123 getRDBMS(char *name) {
126 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
127 if ( name == NULL ) {
128 if ( DBDesc[i].init )
129 return DBDesc[i].rdbms;
130 } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
131 if ( DBDesc[i].init == NULL ) {
132 fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
135 return DBDesc[i].rdbms;
139 fprintf(stderr,"Can't find a RDBMS\n");
146 getFLAGS(char *flg) {
149 if ( strcasestr(flg,"gist") )
151 if ( strcasestr(flg,"gin") )
153 if ( strcasestr(flg,"func") )
155 if ( strcasestr(flg,"and") )
157 if ( strcasestr(flg,"or") )
160 if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
161 fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
164 if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
165 fprintf(stderr,"AND and OR flags are mutually exclusive\n");
173 initConnections(RDBMS rdbms, int n, char *connstr) {
174 ftsDB **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
178 fprintf(stderr,"Not enough mwmory\n");
183 dbs[i] = DBDesc[rdbms].init(connstr);
184 pthread_mutex_init(&dbs[i]->nqueryMutex, NULL);
191 timediff(struct timeval *begin, struct timeval *end) {
192 return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6;
196 elapsedtime(struct timeval *begin) {
198 gettimeofday(&end,NULL);
199 return timediff(begin,&end);
202 static int benchFlags = 0;
203 static int benchCount = 0;
204 static pthread_cond_t condFinish = PTHREAD_COND_INITIALIZER;
205 static pthread_mutex_t mutexFinish = PTHREAD_MUTEX_INITIALIZER;
206 static pthread_mutex_t mutexWordGen = PTHREAD_MUTEX_INITIALIZER;
209 execBench(void *in) {
210 ftsDB *db = (ftsDB*)in;
214 for(i=0;i<benchCount;i++) {
216 * generate_querywords() isn't a thread safe
218 pthread_mutex_lock( &mutexWordGen );
219 words = generate_querywords();
220 pthread_mutex_unlock( &mutexWordGen );
222 db->execQuery(db, words, benchFlags);
227 * send message about exitting
229 pthread_mutex_lock( &mutexFinish );
230 pthread_cond_broadcast( &condFinish );
231 pthread_mutex_unlock( &mutexFinish );
239 main(int argn, char *argv[]) {
241 int n = 0, nclients = 1;
245 RDBMS rdbms = NULLSQL;
249 StringBuf b = {NULL,0,0};
251 while((i=getopt(argn,argv,"ib:n:l:g:d:c:hf:q")) != EOF) {
253 case 'i': initMode = 1; break;
254 case 'b': rdbms = getRDBMS(optarg); break;
255 case 'n': n=atoi(optarg); break;
256 case 'c': nclients=atoi(optarg); break;
257 case 'l': lex = strdup(optarg); break;
258 case 'g': doc = strdup(optarg); break;
259 case 'd': dbname = strdup(optarg); break;
260 case 'f': flags = getFLAGS(optarg); break;
261 case 'q': quiet = 1; break;
268 if (rdbms == NULLSQL)
269 rdbms = getRDBMS(NULL);
271 if ( dbname == NULL || n<0 || nclients<1 )
274 printf("Running with '%s' RDBMS\n", DBDesc[ rdbms ].longname);
277 ftsDB *db = *initConnections(rdbms, 1, dbname);
280 if (!lex) lex = "gendata/lex";
281 if (!doc) doc = "gendata/gamma-lens";
282 finnegan_init(lex, doc);
284 db->startCreateScheme(db, flags);
288 db->InsertRow(db, i+1, b.str);
289 if ( !quiet && prev!=time(NULL) ) {
290 printf("\r%d(%.02f%%) rows inserted", i, (100.0*i)/n);
295 printf("%s%d(100.00%%) rows inserted. Finalyze insertion... ",
296 (quiet) ? "" : "\r", i);
298 db->finishCreateScheme(db);
302 ftsDB **dbs = initConnections(rdbms, nclients, dbname);
303 pthread_t *tid = (pthread_t*)malloc( sizeof(pthread_t) * nclients);
304 struct timeval begin;
307 struct timespec sleepTo = { 0, 0 };
312 if (!lex) lex = "gendata/query-lex";
313 if (!doc) doc = "gendata/query-lens";
314 finnegan_init(lex, doc);
320 printf("\r0(0.00%%) queries proceed");
326 gettimeofday(&begin,NULL);
328 pthread_mutex_lock( &mutexFinish );
329 for(i=0;i<nclients;i++) {
330 if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
331 fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
340 for(i=0;i<nclients;i++) {
341 pthread_mutex_lock(&dbs[i]->nqueryMutex);
342 total +=dbs[i]->nquery;
343 if ( dbs[i]->nquery < n )
345 pthread_mutex_unlock(&dbs[i]->nqueryMutex);
352 printf("\r%d(%.02f%%) queries proceed", total, (100.0*(float)total)/(nclients * n));
356 sleepTo.tv_sec = time(NULL) + 1;
357 res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
359 if ( !(res == ETIMEDOUT || res == 0) ) {
360 fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
364 elapsed = elapsedtime(&begin);
365 pthread_mutex_unlock( &mutexFinish );
367 for(i=0;i<nclients;i++) {
368 pthread_join(tid[i], NULL);
369 nres += dbs[i]->nres;
370 dbs[i]->Close(dbs[i]);
373 printf("%s%d(%.02f%%) queries proceed\n",
374 (quiet) ? "" : "\r", total, (100.0*(float)total)/(nclients * n));
375 printf("Total number of result: %d\n", nres);
376 printf("Total time: %.02f sec, Queries per second: %.02f\n", elapsed, total/elapsed);