최대 동시접속자수 테스트 프로그램

리눅스/MySQL|2015. 1. 16. 11:09
반응형

최대 동시접속자 수 테스트용 프로그램입니다.


컴파일 방법:
g++ -o mysql-con-stress -I/usr/local/mysql/include/mysql/ -L/usr/local/mysql/lib/mysql/ -lmysqlclient mysql-con-stress.cc


사용방법:
./mysql-con-stress host user pass max_tries

host : 예를 들면 localhost
user : root
pass : 암호
max_tries : 최대동시접속자 수


성공시:
Successefully established 1500 connections
Never failed 
Cleaning up 1500 connections ...
Clean up complete


실패시:
Successefully established 1501 connections
Failed because Too many connections
Cleaning up 1501 connections ...
Clean up complete


MySQL Client 상에서 최대 동시접속 확인법: 
show status like max_used_connections;

이 결과값이 max_tries와 일치하여야 함.

 

주의사항: mysql_connect 함수가 deprecate 되었으므로 mysql_real_connect 함수로 수정하였습니다. 자신의 MySQL버젼에서 오류가 발상할 경우 mysql_init() 부분을 삭제하고 mysql_real_connect 함수를 mysql_connect()함수로 수정바랍니다.

 

/*

* mysql-con-stress.cc by Sasha Pachev

* Stresses a running mysqld to see the maximum number of additional concurrent

* connections it can handle

* to compile: g++ -o mysql-con-stress -I<where_mysql.h_is_found> r

-L<where-libmysqlclient-is-found> mysql-con-stress.cc -lmysqlclient

* to use: mysql-con-stress host user pass max_tries

* If the first three parameters are not obvious, you should not be using this program

* max_tries is the maximum number of concurrent connection mysql-con-stress will try to

* estabilsh

*/

#include <mysql.h>

#include <iostream.h>

#include <stdlib.h>

#include <sys/types.h>

#include <signal.h>

 

int num_cons = 0;

MYSQL* glob_cons = NULL;

 

void clean_up(int )

{

int i;

cerr << Cleaning up << num_cons << connections ... << endl;

 

 

for(i = 0; i < num_cons; i++)

{

mysql_close(glob_cons + i);

}

 

cerr << Clean up complete << endl;

exit(0);

}

 

void usage()

{

cerr << Usage: mysql-con-stress host user pass max_tries << endl;

exit(1);

}

 

int main(int argc, char** argv)

{

if(argc != 5) usage();

char* user, *host, *pass;

int max_tries;

 

user = argv[2];

host = argv[1];

pass = argv[3];

max_tries = atoi(argv[4]);

 

MYSQL cons[max_tries];

glob_cons = cons;

 

int i;

int never_failed = 1;

int port = 0;

 

// if we get a signal, clean up first

 

signal(SIGTERM, clean_up);

signal(SIGINT, clean_up);

 

// establish as many connections as we can

 

for(i = 0; i < max_tries; i++)

{

mysql_init(&cons);

mysql_real_connect(cons + i, host, user, pass, test, 0, NULL, 0);

num_cons = i ;

 

if((mysql_error(cons + i))[0])

{

never_failed = 0;

cout << Successefully established << num_cons << connections << r

endl; cout << Failed because << mysql_error(cons + i) << endl;

break;

}

}

 

 

if(never_failed)

{

num_cons = i ;

cout << Successefully established << num_cons << connections << endl;

cout << Never failed << endl;

}

 

// this is really not necessary, because exiting will close all the connection,

// but it is good practice to clean up

 

clean_up(0);

 

return 0;

}

 

 

[출처] rootman | rootman (http://www.rootman.co.kr/bbs/zboard.php?id=linux&no=1900)

반응형

댓글()