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.
46 typedef struct RDBMSDesc {
50 ftsDB* (*init)(char *);
53 static RDBMSDesc DBDesc[] = {
54 { PostgreSQL, "pgsql", "PostgreSQL", PGInit },
55 { MySQL, "mysql", "MySQL", MYInit },
56 { NULLSQL, NULL, NULL, NULL }
65 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
66 if ( DBDesc[i].init == NULL )
70 strcat(buf, DBDesc[i].shortname);
72 strcat(buf, "(default)");
77 "Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
78 "ftsbench - full text search benchmark for RDBMS\n"
79 "Initialization of DB:\n"
80 "ftsbench -i [-b RDBMS] [-n NUMROW] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q | -s ID] -d DBNAME\n"
81 " -b RDBMS\t- type of DB: ",
87 " -n NUMROW - number of row in table\n"
88 " -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
89 " -g GAMMAFILE - file with doc's length distribution (default \n"
90 " gendata/gamma-lens)\n"
91 " -l FLGAS - options for db's schema (see below)\n"
92 " -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
94 " -q - do not print progress message\n",
99 "ftsbench [-b RDBMS] [-c NCLIENTS] [-n NUMQUERY] [-l LEXFILE] [-g GAMMAFILE] [-f FLAGS] [-q | -s ID [-r]] -d DBNAME\n"
100 " -b RDBMS\t- type of DB: ",
103 fputs( buf, stdout );
106 " -c NCLIENTS - number of clients in parallel\n"
107 " -n NUMQUERY - number of queries per client\n"
108 " -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
109 " -g GAMMAFILE - file with doc's length distribution (default \n"
110 " gendata/query-lens)\n"
111 " -l FLGAS - options for db's schema (see below)\n"
112 " -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
114 " -r - row mode: timing every query\n"
115 " -q - do not print progress message\n",
119 "FLAGS are comma-separate list of:\n"
120 " gin - use GIN index\n"
121 " gist - use GiST index\n"
122 " func - use functional index\n"
123 " and - AND'ing lexemes in query (default)\n"
124 " or - OR'ing lexemes in query\n",
128 "Print SQL-scheme for statistics:\n"
136 getRDBMS(char *name) {
139 for(i=0; DBDesc[i].rdbms != NULLSQL; i++) {
140 if ( name == NULL ) {
141 if ( DBDesc[i].init )
142 return DBDesc[i].rdbms;
143 } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
144 if ( DBDesc[i].init == NULL ) {
145 fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
148 return DBDesc[i].rdbms;
152 fprintf(stderr,"Can't find a RDBMS\n");
159 getFLAGS(char *flg) {
162 if ( strcasestr(flg,"gist") )
164 if ( strcasestr(flg,"gin") )
166 if ( strcasestr(flg,"func") )
168 if ( strcasestr(flg,"and") )
170 if ( strcasestr(flg,"or") )
173 if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
174 fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
177 if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
178 fprintf(stderr,"AND and OR flags are mutually exclusive\n");
180 } else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
187 initConnections(RDBMS rdbms, int n, char *connstr) {
188 ftsDB **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
192 fprintf(stderr,"Not enough mwmory\n");
197 dbs[i] = DBDesc[rdbms].init(connstr);
198 pthread_mutex_init(&dbs[i]->nqueryMutex, NULL);
205 timediff(struct timeval *begin, struct timeval *end) {
206 return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6;
210 elapsedtime(struct timeval *begin) {
212 gettimeofday(&end,NULL);
213 return timediff(begin,&end);
217 static int sqlMode = 0;
218 static int rowMode = 0;
219 static int benchFlags = 0;
220 static int benchCount = 0;
221 static int nClients = 0;
222 static pthread_cond_t condFinish = PTHREAD_COND_INITIALIZER;
223 static pthread_mutex_t mutexFinish = PTHREAD_MUTEX_INITIALIZER;
224 static pthread_mutex_t mutexWordGen = PTHREAD_MUTEX_INITIALIZER;
227 * main test function, executed in thread
230 execBench(void *in) {
231 ftsDB *db = (ftsDB*)in;
234 struct timeval begin;
237 for(i=0;i<benchCount;i++) {
239 * generate_querywords() isn't a thread safe
241 pthread_mutex_lock( &mutexWordGen );
242 words = generate_querywords();
243 pthread_mutex_unlock( &mutexWordGen );
246 gettimeofday(&begin,NULL);
248 db->execQuery(db, words, benchFlags);
251 elapsed = elapsedtime(&begin);
252 printf("INSERT INTO fb_row (id, f_and, f_or, nclients, nres, elapsed) VALUES (%d, '%c', '%c', %d, %d, %g);\n",
254 ( benchFlags & FLG_AND ) ? 't' : 'f',
255 ( benchFlags & FLG_OR ) ? 't' : 'f',
266 * send message about exitting
268 pthread_mutex_lock( &mutexFinish );
269 pthread_cond_broadcast( &condFinish );
270 pthread_mutex_unlock( &mutexFinish );
276 report(const char *format, ...) {
282 va_start(args, format);
283 vfprintf(stdout, format, args);
292 main(int argn, char *argv[]) {
298 RDBMS rdbms = NULLSQL;
301 int quiet = 0, scheme=0;
302 StringBuf b = {NULL,0,0};
303 struct timeval begin;
306 while((i=getopt(argn,argv,"ib:n:l:g:d:c:hf:qSs:r")) != EOF) {
308 case 'i': initMode = 1; break;
309 case 'b': rdbms = getRDBMS(optarg); break;
310 case 'n': n=atoi(optarg); break;
311 case 'c': nClients=atoi(optarg); break;
312 case 'l': lex = strdup(optarg); break;
313 case 'g': doc = strdup(optarg); break;
314 case 'd': dbname = strdup(optarg); break;
315 case 'f': flags = getFLAGS(optarg); break;
316 case 'q': quiet = 1; break;
317 case 'S': scheme = 1; break;
318 case 's': sqlMode = 1; Id = atoi(optarg); break;
319 case 'r': rowMode = 1; break;
331 if (rdbms == NULLSQL)
332 rdbms = getRDBMS(NULL);
334 if ( dbname == NULL || n<0 || (initMode == 0 && nClients<1) )
345 report("Running with '%s' RDBMS\n", DBDesc[ rdbms ].longname);
348 ftsDB *db = *initConnections(rdbms, 1, dbname);
351 if (!lex) lex = "gendata/lex";
352 if (!doc) doc = "gendata/gamma-lens";
353 finnegan_init(lex, doc, sqlMode);
355 gettimeofday(&begin,NULL);
357 db->startCreateScheme(db, flags);
361 db->InsertRow(db, i+1, b.str);
362 if ( !quiet && prev!=time(NULL) ) {
363 report("\r%d(%.02f%%) rows inserted", i, (100.0*i)/n);
368 report("%s%d(100.00%%) rows inserted. Finalyze insertion... ",
369 (quiet) ? "" : "\r", i);
370 db->finishCreateScheme(db);
371 elapsed = elapsedtime(&begin);
373 report("done\nTime: %.02f secs\n", elapsed);
375 printf("INSERT INTO fb_create (id, rdbms, f_gin, f_gist, f_func, rows, elapsed) VALUES (%d, '%s', '%c', '%c', '%c', %d, %g);\n",
377 DBDesc[ rdbms ].shortname,
378 ( flags & FLG_GIN ) ? 't' : 'f',
379 ( flags & FLG_GIST ) ? 't' : 'f',
380 ( flags & FLG_FUNC ) ? 't' : 'f',
387 ftsDB **dbs = initConnections(rdbms, nClients, dbname);
388 pthread_t *tid = (pthread_t*)malloc( sizeof(pthread_t) * nClients);
389 struct timeval begin;
391 struct timespec sleepTo = { 0, 0 };
396 if (!lex) lex = "gendata/query-lex";
397 if (!doc) doc = "gendata/query-lens";
398 finnegan_init(lex, doc, sqlMode);
404 report("\r0(0.00%%) queries proceed");
406 gettimeofday(&begin,NULL);
408 pthread_mutex_lock( &mutexFinish );
409 for(i=0;i<nClients;i++) {
410 if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
411 fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
420 for(i=0;i<nClients;i++) {
421 pthread_mutex_lock(&dbs[i]->nqueryMutex);
422 total +=dbs[i]->nquery;
423 if ( dbs[i]->nquery < n )
425 pthread_mutex_unlock(&dbs[i]->nqueryMutex);
432 report("\r%d(%.02f%%) queries proceed", total, (100.0*(float)total)/(nClients * n));
434 sleepTo.tv_sec = time(NULL) + 1;
435 res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
437 if ( !(res == ETIMEDOUT || res == 0) ) {
438 fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
442 elapsed = elapsedtime(&begin);
443 pthread_mutex_unlock( &mutexFinish );
445 for(i=0;i<nClients;i++) {
446 pthread_join(tid[i], NULL);
447 nres += dbs[i]->nres;
448 dbs[i]->Close(dbs[i]);
451 report("%s%d(%.02f%%) queries proceed\n",
452 (quiet) ? "" : "\r", total, (100.0*(float)total)/(nClients * n));
453 report("Total number of result: %d\n", nres);
454 report("Total time: %.02f sec, Queries per second: %.02f\n", elapsed, total/elapsed);
455 if (sqlMode && !rowMode) {
456 printf("INSERT INTO fb_search (id, f_and, f_or, nclients, nqueries, nres, elapsed) VALUES (%d, '%c', '%c', %d, %d, %d, %g);\n",
458 ( flags & FLG_AND ) ? 't' : 'f',
459 ( flags & FLG_OR ) ? 't' : 'f',