//gcc testcase.c -g -o testcase -L/home/sbester/server/5.0/mysql-5.0.34-linux-i686/lib -I/home/sbester/server/5.0/mysql-5.0.34-linux-i686/include -lmysqlclient_r -lz -lpthread //gcc testcase.c -Wall -D_GNU_SOURCE -lm -g -o testcase -L/users/sbester/gypsy_dist/mysql-standard-5.0.26-linux-x86_64-glibc23/lib -I/users/sbester/gypsy_dist/mysql-standard-5.0.26-linux-x86_64-glibc23/include -lmysqlclient_r -lz -lpthread /*This is generated code!*/ /*section: includes*/ #include #include #include #include #include #define NUMTHREADS (30) /*section: globals*/ char host[]="127.0.0.1"; char username[]="root"; char password[]=""; char database[]="test"; int port=3306; pthread_t pthreads[NUMTHREADS]; int threaddone=0; /*section: function declarations*/ void *worker_thread(void *arg); MYSQL *db_connect(MYSQL *dbc); /*section: functions*/ int db_query(MYSQL *dbc,char *sql,int showresults) { int res=0; MYSQL_RES *r=NULL; MYSQL_ROW w; MYSQL_FIELD *field=NULL; int i=0,moreresult=0; res = mysql_query(dbc,sql); if(res != 0) { if(showresults>0 && mysql_errno(dbc)!=2006 && mysql_errno(dbc)!=2013 && mysql_errno(dbc)!=1053) 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) { if(showresults>0) printf("No results for '%s'. (%d) - %s\n",sql,mysql_errno(dbc),mysql_error(dbc)); } else //it could have been some insert/update/delete { //this is successful query } } moreresult=mysql_next_result(dbc); if(moreresult>0) { if(showresults>0) printf("mysql_next_result returned %d, mysql error %s, (%d)\n",moreresult,mysql_error(dbc),mysql_errno(dbc)); break; } } while (0==moreresult); } void *worker_thread(void *arg) { MYSQL *dbc=NULL; int cancelstate=0; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&cancelstate); 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; } } unsigned int counter=0; char query[1024]={0}; int i=0; while(!threaddone) { if(lrand48()%2==0) { for(i=0;i<1000;i++) { sprintf(query,"insert into `t1` set `a`=%lu,`b`=%lu,`c`=%lu,`d`='%lu',`e`=%lu",lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000); db_query(dbc,query,1); } } if(lrand48()%2==0) { for(i=0;i<100;i++) { sprintf(query,"delete from `t1` where `a` in (%lu,%lu,%lu,%lu,%lu)",lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000); db_query(dbc,query,1); } } if(lrand48()%2==0) { for(i=0;i<100;i++) { sprintf(query,"update `t1` set `a`=%lu,`b`=%lu,`c`=%lu,`d`='%lu',`e`=%lu where `b` in (%lu,%lu,%lu,%lu,%lu,%lu)",lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000); db_query(dbc,query,1); } } if(lrand48()%2==0) { for(i=0;i<1000;i++) { sprintf(query,"kill %lu%%10000",lrand48()); db_query(dbc,query,0); } } if(lrand48()%2==0) { for(i=0;i<100;i++) { sprintf(query,"replace into `t1` set `a`=%lu,`b`=%lu,`c`=%lu,`d`='%lu',`e`=%lu",lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000,lrand48()%20000); db_query(dbc,query,1); } } if(lrand48()%2==0) { //db_query(dbc,"check table `t1`",2); db_query(dbc,"repair table `t1`",2); } if(lrand48()%2==0) { mysql_close(dbc); dbc=mysql_init(NULL); 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)\n", mysql_error(dbc),mysql_errno(dbc)); } } } threadexit: mysql_close(dbc); mysql_thread_end(); pthread_exit(0); } int main(int argc, const char *argv[]) { MYSQL *dbc=NULL; int i=0,err=0; time_t timestart=0,timenow=0; 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"); db_query(dbc,"set global table_cache=200",0); db_query(dbc,"set global thread_cache_size=200",0); db_query(dbc,"drop table if exists `t1`",0); db_query(dbc,"create table `t1`(`a` int, `b` int, `c` int, `d` varchar(255), `e` int,key(`a`),key(`b`),key(`c`),key(`d`),key(`e`),key(`a`,`b`),key(`b`,`c`),key(`c`,`d`(200)),key(`c`,`e`))engine=myisam",0); mysql_close(dbc); for (i=0;i