//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 (3600) #define NUMTHREADS (450) /*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; if(NULL == dbc) return 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; } 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) { c=shortquery; c+=sprintf(c,"%s","set foreign_key_checks=0"); db_query(dbc,shortquery,1); for(counter=0;counter<10000;counter++) { c=shortquery; c+=sprintf(c,"%s","call proc1(@a)"); 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"); c=shortquery; c+=sprintf(c,"%s","set global innodb_flush_log_at_trx_commit=0"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","set global max_connections=1000"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","set foreign_key_checks=0"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","flush tables"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","drop table if exists users"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","drop table if exists transactions"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","create table users ( id int unsigned not null auto_increment, unique(id), username char(32) not null, primary key(username) ) engine=InnoDB"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","create table transactions ( id bigint unsigned not null auto_increment, unique(id), type tinyint unsigned not null, status tinyint unsigned not null, updated timestamp not null, entered timestamp not null, user int unsigned not null, amount decimal not null, key(entered desc), key(user), foreign key(user) references users (id) on delete cascade on update cascade ) engine=InnoDB"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","drop trigger trx_bins"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","create trigger trx_bins before insert on transactions for each row begin set new.status=2; if new.type=2 then set new.amount=(select amount from transactions where type=1 and user=new.user order by entered desc limit 1); end if; end;"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","drop procedure if exists proc1"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","create procedure proc1(out tid bigint unsigned) language sql not deterministic modifies sql data sql security definer begin set tid=null; retry: repeat begin declare continue handler for 1205 iterate retry; declare continue handler for sqlstate '40001' iterate retry; start transaction; insert into transactions (type,entered,user,amount) values (20,null,1,0); set tid=last_insert_id(); commit; end; until true end repeat retry; end;"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","insert into users set id=1,username='test'"); db_query(dbc,shortquery,1); c=shortquery; c+=sprintf(c,"%s","insert into transactions set type=1,entered=null,user=1,amount=0"); db_query(dbc,shortquery,1); mysql_close(dbc); printf("about to spawn %d threads\n",NUMTHREADS); for (i=0;i