add .gitignore
[ftsbench.git] / ftsbench.c
index 0232353..3c8b7f6 100644 (file)
@@ -88,7 +88,7 @@ usage() {
                "  -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
                "  -g GAMMAFILE - file with doc's length distribution (default \n"
                "                 gendata/gamma-lens)\n"
-               "  -l FLGAS - options for db's schema (see below)\n"
+               "  -l FLAGS - options for db's schema (see below)\n"
                "  -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
                "          statement\n"
                "  -q - do not print progress message\n",
@@ -108,7 +108,7 @@ usage() {
                "  -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
                "  -g GAMMAFILE - file with doc's length distribution (default \n"
                "                 gendata/query-lens)\n"
-               "  -l FLGAS - options for db's schema (see below)\n"
+               "  -l FLAGS - options for db's schema (see below)\n"
                "  -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
                "          statement\n"
                "  -r - row mode: timing every query\n"
@@ -141,16 +141,14 @@ getRDBMS(char *name) {
                        if ( DBDesc[i].init )
                                return DBDesc[i].rdbms; 
                } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
-                       if ( DBDesc[i].init == NULL ) {
-                               fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
-                               exit(1);
-                       }
+                       if ( DBDesc[i].init == NULL ) 
+                               fatal("Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
+
                        return DBDesc[i].rdbms;
                }
        }
 
-       fprintf(stderr,"Can't find a RDBMS\n");
-       exit(1);
+       fatal("Can't find a RDBMS\n");
        
        return NULLSQL;
 }
@@ -170,14 +168,11 @@ getFLAGS(char *flg) {
        if ( strcasestr(flg,"or") )
                flags |= FLG_OR;
 
-       if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
-               fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
-               exit(1);
-       }
-       if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
-               fprintf(stderr,"AND and OR flags are mutually exclusive\n");
-               exit(1);
-       } else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
+       if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) 
+               fatal("GIN and GiST flags are mutually exclusive\n");
+       if ( (flags & FLG_AND) && (flags & FLG_OR) ) 
+               fatal("AND and OR flags are mutually exclusive\n");
+       else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
                flags |= FLG_AND;
 
        return flags;
@@ -188,10 +183,8 @@ initConnections(RDBMS rdbms, int n, char *connstr) {
        ftsDB   **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
        int i;
 
-       if (!dbs) {
-               fprintf(stderr,"Not enough mwmory\n");
-               exit(1);
-       }
+       if (!dbs) 
+               fatal("Not enough mwmory\n");
 
        for(i=0;i<n;i++) { 
                dbs[i] = DBDesc[rdbms].init(connstr);
@@ -223,6 +216,27 @@ static pthread_cond_t condFinish = PTHREAD_COND_INITIALIZER;
 static pthread_mutex_t mutexFinish = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t mutexWordGen = PTHREAD_MUTEX_INITIALIZER;
 
+static void
+printQueryWords(StringBuf *b, char **words) {
+       char **wptr = words, *ptr;
+
+       b->strlen = 0;
+       while(*wptr) {
+               if ( wptr != words ) 
+                       sb_add(b, " ", 1);
+
+               ptr = *wptr;
+               while( *ptr ) {
+                       if ( *ptr == '\'' )
+                               sb_add( b, "'", 1 );
+                       sb_add( b, ptr, 1 );
+                       ptr++;
+               }
+
+               wptr++;
+       }
+}
+
 /*
  * main test function, executed in thread
  */
@@ -233,6 +247,7 @@ execBench(void *in) {
        char **words;
        struct  timeval begin;
        double  elapsed;
+       StringBuf       b = {NULL,0,0};
 
        for(i=0;i<benchCount;i++) {
                /*
@@ -249,11 +264,14 @@ execBench(void *in) {
 
                if ( rowMode ) {
                        elapsed = elapsedtime(&begin);
-                       printf("INSERT INTO fb_row (id, f_and, f_or, nclients, nres, elapsed) VALUES (%d, '%c', '%c', %d, %d, %g);\n",
+                       printQueryWords(&b, words);
+
+                       printf("INSERT INTO fb_row (id, f_and, f_or, nclients, query, nres, elapsed) VALUES (%d, '%c', '%c', %d, '%s', %d, %g);\n",
                                        Id,
                                        ( benchFlags & FLG_AND ) ? 't' : 'f',
                                        ( benchFlags & FLG_OR ) ? 't' : 'f',
                                        nClients,
+                                       b.str,
                                        db->nres - nres,
                                        elapsed
                        );
@@ -276,7 +294,7 @@ void
 report(const char *format, ...) {
        va_list args;
 
-       if (benchFlags & FLG_SQL)
+       if (sqlMode)
                return;
 
        va_start(args, format);
@@ -286,6 +304,20 @@ report(const char *format, ...) {
        fflush(stdout);
 }
 
+void
+fatal(const char *format, ...) {
+       va_list args;
+
+       va_start(args, format);
+       vfprintf(stderr, format, args);
+       va_end(args);
+
+       fflush(stderr);
+
+       exit(1);
+}
+
+
 extern char *optarg;
 
 int
@@ -334,10 +366,9 @@ main(int argn, char *argv[]) {
        if ( dbname == NULL || n<0 || (initMode == 0 && nClients<1) ) 
                usage();
 
-       if ( sqlMode ) {
+       if ( sqlMode ) 
                quiet = 1;
-               flags |= FLG_SQL;
-       } else
+       else
                rowMode = 0;
 
        benchFlags = flags;
@@ -408,10 +439,8 @@ main(int argn, char *argv[]) {
 
        pthread_mutex_lock( &mutexFinish );
                for(i=0;i<nClients;i++) {
-                       if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
-                               fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                       if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) 
+                               fatal("pthread_create failed: %s\n", strerror(errno));
                }
 
                for(;;) {
@@ -435,10 +464,8 @@ main(int argn, char *argv[]) {
                        sleepTo.tv_sec = time(NULL) + 1;
                        res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
 
-                       if ( !(res == ETIMEDOUT || res == 0) ) {
-                               fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                       if ( !(res == ETIMEDOUT || res == 0) ) 
+                               fatal("pthread_cond_timedwait failed: %s\n", strerror(errno));
                }
                elapsed = elapsedtime(&begin);
                pthread_mutex_unlock( &mutexFinish );