Switched from CPU time to real time in connection test.

This commit is contained in:
Markus Makela
2014-11-10 14:30:56 +02:00
parent a3675e9098
commit 7f7cb0a982
2 changed files with 92 additions and 28 deletions

View File

@ -1,6 +1,6 @@
if(MYSQLCLIENT_FOUND) if(MYSQLCLIENT_FOUND)
add_executable(testconnect testconnect.c) add_executable(testconnect testconnect.c)
message(STATUS "Linking against: ${MYSQLCLIENT_LIBRARIES}") message(STATUS "Linking against: ${MYSQLCLIENT_LIBRARIES}")
target_link_libraries(testconnect ${MYSQLCLIENT_LIBRARIES} ssl crypto dl z m rt) target_link_libraries(testconnect ${MYSQLCLIENT_LIBRARIES} ssl crypto dl z m rt pthread)
add_test(NAME ReadConnRouterLoginTest COMMAND $<TARGET_FILE:testconnect> 10000 ${TEST_HOST} ${MASTER_PORT} ${TEST_HOST} ${TEST_PORT} 1.10) add_test(NAME ReadConnRouterLoginTest COMMAND $<TARGET_FILE:testconnect> 10000 ${TEST_HOST} ${MASTER_PORT} ${TEST_HOST} ${TEST_PORT} 1.10)
endif() endif()

View File

@ -4,24 +4,40 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
MYSQL* server; MYSQL* server;
char *host; char *host = NULL, *str_baseline = NULL,*str_test = NULL, *errmsg = NULL;
unsigned int port; unsigned int port;
int rval, iterations,i; int rval, iterations,i;
clock_t begin,end; clock_t begin,end;
double baseline,test, ratio, result, minimum; size_t offset;
struct timeval real_begin,real_end,real_baseline,real_test;
time_t time;
double baseline, test, ratio, result;
if(argc < 7){ if(argc < 7){
fprintf(stderr,"Usage: %s <iterations> <baseline host> <baseline port> <test host> <test port> <max result ratio>\n",argv[0]); fprintf(stderr,"Usage: %s <iterations> <baseline host> <baseline port> <test host> <test port> <max result ratio>\n",argv[0]);
fprintf(stderr,"The ratio is measured as:\ntest CPU time / baseline CPU time\n"); fprintf(stderr,"The ratio is measured as:\ntest time / baseline time\n");
fprintf(stderr,"The test fails if this ratio is exceeded.\n"); fprintf(stderr,"The test fails if this ratio is exceeded.\n");
return 1; return 1;
} }
;
if((str_baseline = calloc(256,sizeof(char))) == NULL){
return 1;
}
if((str_test = calloc(256,sizeof(char))) == NULL){
free(str_baseline);
return 1;
}
iterations = atoi(argv[1]); iterations = atoi(argv[1]);
host = strdup(argv[2]); host = strdup(argv[2]);
port = atoi(argv[3]); port = atoi(argv[3]);
@ -35,24 +51,36 @@ int main(int argc, char** argv)
/**Testing direct connection to master*/ /**Testing direct connection to master*/
printf("Connecting to MySQL server through %s:%d.\n",host,port); printf("Connecting to MySQL server through %s:%d.\n",host,port);
gettimeofday(&real_begin,NULL);
begin = clock(); begin = clock();
for(i = 0;i<iterations;i++)
{
if((server = mysql_init(NULL)) == NULL){ if((server = mysql_init(NULL)) == NULL){
return 1; return 1;
} }
if(mysql_real_connect(server,host,"maxuser","maxpwd",NULL,port,NULL,0) == NULL){ if(mysql_real_connect(server,host,"maxuser","maxpwd",NULL,port,NULL,0) == NULL){
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(server));
rval = 1; rval = 1;
break; printf( "Failed to connect to database: Error: %s\n",
} mysql_error(server));
mysql_close(server); goto report;
} }
for(i = 0;i<iterations;i++)
{
if(mysql_change_user(server,"maxuser","maxpwd",NULL)){
rval = 1;
printf( "Failed to change user: Error: %s\n",
mysql_error(server));
goto report;
}
}
mysql_close(server);
end = clock(); end = clock();
gettimeofday(&real_end,NULL);
baseline = (double)(end - begin)/CLOCKS_PER_SEC; baseline = (double)(end - begin)/CLOCKS_PER_SEC;
timersub(&real_end,&real_begin,&real_baseline);
free(host); free(host);
host = strdup(argv[4]); host = strdup(argv[4]);
@ -61,37 +89,73 @@ int main(int argc, char** argv)
/**Testing connection to master through MaxScale*/ /**Testing connection to master through MaxScale*/
printf("Connecting to MySQL server through %s:%d.\n",host,port); printf("Connecting to MySQL server through %s:%d.\n",host,port);
gettimeofday(&real_begin,NULL);
begin = clock(); begin = clock();
for(i = 0;i<iterations;i++)
{
if((server = mysql_init(NULL)) == NULL){ if((server = mysql_init(NULL)) == NULL){
return 1; return 1;
} }
if(mysql_real_connect(server,host,"maxuser","maxpwd",NULL,port,NULL,0) == NULL){ if(mysql_real_connect(server,host,"maxuser","maxpwd",NULL,port,NULL,0) == NULL){
rval = 1; rval = 1;
fprintf(stderr, "Failed to connect to database: Error: %s\n", printf("Failed to connect to database: Error: %s\n",
mysql_error(server)); mysql_error(server));
break; goto report;
} }
for(i = 0;i<iterations;i++)
{
if(mysql_change_user(server,"maxuser","maxpwd",NULL)){
rval = 1;
printf("Failed to change user: Error: %s\n",
mysql_error(server));
goto report;
}
}
mysql_close(server); mysql_close(server);
}
end = clock(); end = clock();
gettimeofday(&real_end,NULL);
test = (double)(end - begin)/CLOCKS_PER_SEC; test = (double)(end - begin)/CLOCKS_PER_SEC;
timersub(&real_end,&real_begin,&real_test);
printf("CPU time used in seconds:\nDirect connection: %f\nThrough MaxScale: %f\n",baseline,test); report:
result = test / baseline;
if(rval){ if(rval){
printf("Test failed: Errors during test run.");
}else if(result > ratio){ printf("\nTest failed: Errors during test run.\n");
printf("Test failed: CPU time ratio was %f which exceeded the limit of %f.\n", result, ratio);
}else{
struct tm *tm;
time = real_baseline.tv_sec;
tm = localtime(&time);
offset = strftime(str_baseline,256*sizeof(char),"%S",tm);
sprintf(str_baseline + offset,".%06d",(int)real_baseline.tv_usec);
time = real_test.tv_sec;
tm = localtime(&time);
offset = strftime(str_test,256*sizeof(char),"%S",tm);
sprintf(str_test + offset,".%06d",(int)real_test.tv_usec);
printf("\n\tCPU time in seconds\n\nDirect connection: %f\nThrough MaxScale: %f\n",baseline,test);
printf("\n\tReal time in seconds\n\nDirect connection: %s\nThrough MaxScale: %s\n",str_baseline,str_test);
double base_res = real_baseline.tv_sec + (real_baseline.tv_usec / 1000000.0);
double test_res = real_test.tv_sec + (real_test.tv_usec / 1000000.0);
result = test_res/base_res;
if(result > ratio){
printf("\nTest failed: Time ratio was %f which exceeded the limit of %f.\n", result, ratio);
rval = 1; rval = 1;
}else{ }else{
printf("Test passed: CPU time ratio was %f.\n",result); printf("\nTest passed: Time ratio was %f.\n",result);
} }
}
free(str_baseline);
free(str_test);
free(host); free(host);
free(errmsg);
return rval; return rval;
} }