//gcc testcase.c -g -o testcase -L/home/sbester/server/5.0/mysql-5.0.36-linux-i686/lib -I/home/sbester/server/5.0/mysql-5.0.36-linux-i686/include -lmysqlclient_r -lz -lpthread /*This is generated code!*/ /*section: includes*/ #include #include #include #include #include #define TESTTIME (36000) #define NUMTHREADS (50) #define DATASIZE (16*1024*1024) /*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; char *databucket=NULL; /*section: function declarations*/ void *worker_thread(void *arg); MYSQL *db_connect(MYSQL *dbc); int write_string(char *buf,const int minlen,const int maxlen,const int flag); /*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 && showresults > 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)); } 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); } char* alocmem(const int num) { char *r=calloc(num,1); if(NULL == r) { printf("cannot calloc %d bytes of memory\n",num); exit(1); } return r; } int write_string(char *buf,const int minlen,const int maxlen,const int flag) { if(!buf || maxlen==0) return 0; int lengt=minlen + lrand48() % (maxlen-minlen) - 1; int start_lengt = lrand48()%(DATASIZE - lengt - 1); if(lengt<0) lengt=0; if(start_lengt<0) start_lengt=0; memcpy(buf,databucket+start_lengt,lengt); buf[lengt]=0; return lengt; } 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; } } unsigned int counter=0; char shortquery[1024]={0}; char *longquery=NULL; char *c=NULL; while(!threaddone && dbc!=NULL) { /* printing 9 SQL statements */ c=shortquery; c+=sprintf(c,"%s","set session autocommit=1"); db_query(dbc,shortquery,1); for(counter=0;counter<100;counter++) { c=shortquery; c+=sprintf(c,"%s","set global thread_cache_size=abs("); c+=sprintf(c,"%d",-128 + lrand48()%255); c+=sprintf(c,"%s",")"); db_query(dbc,shortquery,1); } longquery = alocmem(2809); for(counter=0;counter<100;counter++) { c=longquery; c+=sprintf(c,"%s","INSERT IGNORE INTO `table0` SET `col0` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,15), `col1` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,105), `col2` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,68), `col3` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col4` = "); c+=sprintf(c,"%d",-2147483647 + lrand48()%4294967294); c+=sprintf(c,"%s",", `col5` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col6` = "); c+=sprintf(c,"%d",-128 + lrand48()%255); c+=sprintf(c,"%s",", `col7` = '"); c+=sprintf(c,"%04d-%02d-%02d",1000 + (lrand48()%(9999-1000)),01 + (lrand48()%(12-1)),01 + (lrand48()%(31-1))); c+=sprintf(c,"%s","', `col8` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,44), `col9` = "); c+=sprintf(c,"%d",-2147483647 + lrand48()%4294967294); c+=sprintf(c,"%s",", `col10` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col11` = "); c+=sprintf(c,"%10.10f",-8388608 + lrand48()%16777215+drand48()); c+=sprintf(c,"%s",", `col12` = "); c+=sprintf(c,"%d",-128 + lrand48()%255); c+=sprintf(c,"%s",", `col13` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col14` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255)"); db_query(dbc,longquery,1); } free(longquery); longquery = alocmem(2803); for(counter=0;counter<100;counter++) { c=longquery; c+=sprintf(c,"%s","REPLACE INTO `table0` SET `col0` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,15), `col1` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,105), `col2` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,68), `col3` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col4` = "); c+=sprintf(c,"%d",-2147483647 + lrand48()%4294967294); c+=sprintf(c,"%s",", `col5` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col6` = "); c+=sprintf(c,"%d",-128 + lrand48()%255); c+=sprintf(c,"%s",", `col7` = '"); c+=sprintf(c,"%04d-%02d-%02d",1000 + (lrand48()%(9999-1000)),01 + (lrand48()%(12-1)),01 + (lrand48()%(31-1))); c+=sprintf(c,"%s","', `col8` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,44), `col9` = "); c+=sprintf(c,"%d",-2147483647 + lrand48()%4294967294); c+=sprintf(c,"%s",", `col10` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col11` = "); c+=sprintf(c,"%10.10f",-8388608 + lrand48()%16777215+drand48()); c+=sprintf(c,"%s",", `col12` = "); c+=sprintf(c,"%d",-128 + lrand48()%255); c+=sprintf(c,"%s",", `col13` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255), `col14` = SUBSTR('"); c+=write_string(c,0,255,0); c+=sprintf(c,"%s","',1,255)"); db_query(dbc,longquery,1); } free(longquery); for(counter=0;counter<100;counter++) { c=shortquery; c+=sprintf(c,"%s","SHOW TABLE STATUS"); db_query(dbc,shortquery,1); } for(counter=0;counter<100;counter++) { c=shortquery; c+=sprintf(c,"%s","SELECT * FROM `table0` ORDER BY `col6` LIMIT 10"); db_query(dbc,shortquery,1); } c=shortquery; c+=sprintf(c,"%s","commit"); db_query(dbc,shortquery,1); } 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; unsigned int counter=0; char shortquery[1024]={0}; char *longquery=NULL; 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"); printf("pre-generating %d bytes of random data\n",DATASIZE); databucket=calloc(DATASIZE,sizeof(char*)); if(NULL == databucket) { printf("error: cannot calloc data buffer.\n"); exit(1); } for(i=0;i