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",
120 "Print SQL-scheme for statistics:\n"
128 getRDBMS(char *name) {
131 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
132 if ( name == NULL ) {
133 if ( DBDesc[i].init )
134 return DBDesc[i].rdbms;
135 } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
136 if ( DBDesc[i].init == NULL ) {
137 fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
140 return DBDesc[i].rdbms;
144 fprintf(stderr,"Can't find a RDBMS\n");
151 getFLAGS(char *flg) {
154 if ( strcasestr(flg,"gist") )
156 if ( strcasestr(flg,"gin") )
158 if ( strcasestr(flg,"func") )
160 if ( strcasestr(flg,"and") )
162 if ( strcasestr(flg,"or") )
165 if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
166 fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
169 if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
170 fprintf(stderr,"AND and OR flags are mutually exclusive\n");
178 initConnections(RDBMS rdbms, int n, char *connstr) {
179 ftsDB **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
183 fprintf(stderr,"Not enough mwmory\n");
188 dbs[i] = DBDesc[rdbms].init(connstr);
189 pthread_mutex_init(&dbs[i]->nqueryMutex, NULL);
196 timediff(struct timeval *begin, struct timeval *end) {
197 return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6;
201 elapsedtime(struct timeval *begin) {
203 gettimeofday(&end,NULL);
204 return timediff(begin,&end);
207 static int benchFlags = 0;
208 static int benchCount = 0;
209 static pthread_cond_t condFinish = PTHREAD_COND_INITIALIZER;
210 static pthread_mutex_t mutexFinish = PTHREAD_MUTEX_INITIALIZER;
211 static pthread_mutex_t mutexWordGen = PTHREAD_MUTEX_INITIALIZER;
214 execBench(void *in) {
215 ftsDB *db = (ftsDB*)in;
219 for(i=0;i<benchCount;i++) {
221 * generate_querywords() isn't a thread safe
223 pthread_mutex_lock( &mutexWordGen );
224 words = generate_querywords();
225 pthread_mutex_unlock( &mutexWordGen );
227 db->execQuery(db, words, benchFlags);
232 * send message about exitting
234 pthread_mutex_lock( &mutexFinish );
235 pthread_cond_broadcast( &condFinish );
236 pthread_mutex_unlock( &mutexFinish );
244 main(int argn, char *argv[]) {
246 int n = 0, nclients = 1;
250 RDBMS rdbms = NULLSQL;
253 int quiet = 0, scheme=0;
254 StringBuf b = {NULL,0,0};
256 while((i=getopt(argn,argv,"ib:n:l:g:d:c:hf:qS")) != EOF) {
258 case 'i': initMode = 1; break;
259 case 'b': rdbms = getRDBMS(optarg); break;
260 case 'n': n=atoi(optarg); break;
261 case 'c': nclients=atoi(optarg); break;
262 case 'l': lex = strdup(optarg); break;
263 case 'g': doc = strdup(optarg); break;
264 case 'd': dbname = strdup(optarg); break;
265 case 'f': flags = getFLAGS(optarg); break;
266 case 'q': quiet = 1; break;
267 case 'S': scheme = 1; break;
279 if (rdbms == NULLSQL)
280 rdbms = getRDBMS(NULL);
282 if ( dbname == NULL || n<0 || nclients<1 )
285 printf("Running with '%s' RDBMS\n", DBDesc[ rdbms ].longname);
288 ftsDB *db = *initConnections(rdbms, 1, dbname);
291 if (!lex) lex = "gendata/lex";
292 if (!doc) doc = "gendata/gamma-lens";
293 finnegan_init(lex, doc);
295 db->startCreateScheme(db, flags);
299 db->InsertRow(db, i+1, b.str);
300 if ( !quiet && prev!=time(NULL) ) {
301 printf("\r%d(%.02f%%) rows inserted", i, (100.0*i)/n);
306 printf("%s%d(100.00%%) rows inserted. Finalyze insertion... ",
307 (quiet) ? "" : "\r", i);
309 db->finishCreateScheme(db);
313 ftsDB **dbs = initConnections(rdbms, nclients, dbname);
314 pthread_t *tid = (pthread_t*)malloc( sizeof(pthread_t) * nclients);
315 struct timeval begin;
318 struct timespec sleepTo = { 0, 0 };
323 if (!lex) lex = "gendata/query-lex";
324 if (!doc) doc = "gendata/query-lens";
325 finnegan_init(lex, doc);
331 printf("\r0(0.00%%) queries proceed");
337 gettimeofday(&begin,NULL);
339 pthread_mutex_lock( &mutexFinish );
340 for(i=0;i<nclients;i++) {
341 if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
342 fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
351 for(i=0;i<nclients;i++) {
352 pthread_mutex_lock(&dbs[i]->nqueryMutex);
353 total +=dbs[i]->nquery;
354 if ( dbs[i]->nquery < n )
356 pthread_mutex_unlock(&dbs[i]->nqueryMutex);
363 printf("\r%d(%.02f%%) queries proceed", total, (100.0*(float)total)/(nclients * n));
367 sleepTo.tv_sec = time(NULL) + 1;
368 res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
370 if ( !(res == ETIMEDOUT || res == 0) ) {
371 fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
375 elapsed = elapsedtime(&begin);
376 pthread_mutex_unlock( &mutexFinish );
378 for(i=0;i<nclients;i++) {
379 pthread_join(tid[i], NULL);
380 nres += dbs[i]->nres;
381 dbs[i]->Close(dbs[i]);
384 printf("%s%d(%.02f%%) queries proceed\n",
385 (quiet) ? "" : "\r", total, (100.0*(float)total)/(nclients * n));
386 printf("Total number of result: %d\n", nres);
387 printf("Total time: %.02f sec, Queries per second: %.02f\n", elapsed, total/elapsed);