//gcc qc01.c -Wall -g -o qc01 -L/home/sbester/server/5.0/mysql-5.0.46-linux-i686/lib -I/home/sbester/server/5.0/mysql-5.0.46-linux-i686/include -lmysqlclient_r -lz -lpthread #include #include #include #include #include #include "mysql.h" #define TESTTIME (3600) #define NUMTHREADS (5) #define NUMTABLES 2 #define ROWSPERTABLE 5000000 #define QUERYCACHESIZE 1024*1024*1024 #define INSERTTHREADSLEEP 300 #define PERCENTTABLESMODIFY 50 char host[]="127.0.0.1"; char username[]="root"; char password[]=""; char database[]="test"; int port=3306; pthread_t pthreads[NUMTHREADS]; pthread_t inserter; int threaddone=0; unsigned long long total_selects=0; void *selecting_thread(void *arg); void *inserting_thread(void *arg); MYSQL *db_connect(MYSQL *dbc); int db_query(MYSQL *dbc,char *sql,int showresults); int main(int argc, const char *argv[]) { MYSQL *dbc=NULL; MYSQL_RES *r=NULL; MYSQL_ROW w; int res=0; unsigned int i=0,err=0; time_t timestart=0,timenow=0; srand48(time(NULL)); char shortquery[1024]={0}; char *c=NULL; my_init(); if (!(dbc = mysql_init(NULL))) { printf("mysql_init\n"); dbc=NULL; goto threadexit; } else { if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS)) { printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc)); dbc=NULL; goto threadexit; } } printf("running initializations..\n"); c=shortquery; c+=sprintf(c,"%s","flush status"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","flush tables"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","reset query cache"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"set global query_cache_size=%d",QUERYCACHESIZE); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","set global query_cache_type=1"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","set global query_cache_min_res_unit=4096"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","set global max_allowed_packet=1024*1024*1024"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","drop database if exists `qc01`"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","create database `qc01`"); db_query(dbc,shortquery,1); printf("creating %d tables\n",NUMTABLES); for(i=0;i 0) { printf("query failed '%s' : %d (%s)\n",sql,mysql_errno(dbc),mysql_error(dbc)); return 0; } do { r = mysql_store_result(dbc); if(r) { unsigned int numfields = mysql_num_fields(r); //unsigned int numrows=mysql_num_rows(r); while((field = mysql_fetch_field(r))) { //print metadata information about each field if(showresults > 1) { printf("%s ",field->name); } } if(showresults > 1) { printf("\n------------------------------------\n"); } while ((w = mysql_fetch_row(r))) { for(i = 0; i < numfields; i++) { //print each field here if(showresults > 1) { printf("%s\t",w[i]); } } if(showresults > 1) { printf("\n"); } } if(showresults > 1) { printf("\n"); } mysql_free_result(r); } else //no rows returned. was it a select? { if(mysql_field_count(dbc) > 0 && showresults > 0) { printf("No results for '%s'. (%d) - %s\n",sql,mysql_errno(dbc),mysql_error(dbc)); return 0; } else //it could have been some insert/update/delete { //this is successful query } } moreresult=mysql_next_result(dbc); if(moreresult > 0 && showresults > 0) { printf("mysql_next_result returned %d, mysql error %s, (%d)\n",moreresult,mysql_error(dbc),mysql_errno(dbc)); break; } } while (0==moreresult); return 1; }