16 #include <my_global.h>
19 #include <my_pthread.h>
21 #include <my_getopt.h>
23 static my_bool version, verbose, tty_password= 0;
24 static uint thread_count,number_of_tests=1000,number_of_threads=2;
25 static pthread_cond_t COND_thread_count;
26 static pthread_mutex_t LOCK_thread_count;
28 static char *database,*host,*user,*password,*unix_socket,*
query;
32 void *test_thread(
void *arg __attribute__((unused)))
34 unsigned __stdcall test_thread(
void *arg __attribute__((unused)))
40 mysql=mysql_init(NULL);
41 if (!mysql_real_connect(mysql,host,user,password,database,tcp_port,
44 fprintf(stderr,
"Couldn't connect to engine!\n%s\n\n",mysql_error(mysql));
49 if (verbose) { putchar(
'*'); fflush(stdout); }
50 for (count=0 ; count < number_of_tests ; count++)
53 if (mysql_query(mysql,
query))
55 fprintf(stderr,
"Query failed (%s)\n",mysql_error(mysql));
58 if (!(res=mysql_store_result(mysql)))
60 fprintf(stderr,
"Couldn't get result from %s\n", mysql_error(mysql));
63 mysql_free_result(res);
64 if (verbose) { putchar(
'.'); fflush(stdout); }
67 if (verbose) { putchar(
'#'); fflush(stdout); }
69 pthread_mutex_lock(&LOCK_thread_count);
71 pthread_cond_signal(&COND_thread_count);
72 pthread_mutex_unlock(&LOCK_thread_count);
78 static struct my_option my_long_options[] =
80 {
"help",
'?',
"Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
82 {
"database",
'D',
"Database to use", &database, &database,
83 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
84 {
"host",
'h',
"Connect to host", &host, &host, 0, GET_STR,
85 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
87 "Password to use when connecting to server. If password is not given it's asked from the tty.",
88 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
89 {
"user",
'u',
"User for login if not current user", &user,
90 &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
91 {
"version",
'V',
"Output version information and exit",
92 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
93 {
"verbose",
'v',
"Write some progress indicators", &verbose,
94 &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
95 {
"query",
'Q',
"Query to execute in each threads", &
query,
96 &
query, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
97 {
"port",
'P',
"Port number to use for connection or 0 for default to, in "
98 "order of preference, my.cnf, $MYSQL_TCP_PORT, "
99 #if MYSQL_PORT_DEFAULT == 0
102 "built-in default (" STRINGIFY_ARG(MYSQL_PORT)
").",
104 &tcp_port, 0, GET_UINT, REQUIRED_ARG, MYSQL_PORT, 0, 0, 0, 0, 0},
105 {
"socket",
'S',
"Socket file to use for connection", &unix_socket,
106 &unix_socket, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
107 {
"test-count",
'c',
"Run test count times (default %d)",
108 &number_of_tests, &number_of_tests, 0, GET_UINT,
109 REQUIRED_ARG, 1000, 0, 0, 0, 0, 0},
110 {
"thread-count",
't',
"Number of threads to start",
111 &number_of_threads, &number_of_threads, 0, GET_UINT,
112 REQUIRED_ARG, 2, 0, 0, 0, 0, 0},
113 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
117 static const char *load_default_groups[]= {
"client",0 };
121 printf(
"Connection to a mysql server with multiple threads\n");
124 puts(
"This software comes with ABSOLUTELY NO WARRANTY.\n");
125 printf(
"Usage: %s [OPTIONS] [database]\n", my_progname);
127 my_print_help(my_long_options);
128 print_defaults(
"my",load_default_groups);
129 my_print_variables(my_long_options);
130 printf(
"\nExample usage:\n\n\
131 %s -Q 'select * from mysql.user' -c %d -t %d\n",
132 my_progname, number_of_tests, number_of_threads);
137 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
145 password= my_strdup(argument, MYF(MY_FAE));
146 while (*argument) *argument++=
'x';
166 static void get_options(
int argc,
char **argv)
170 if ((ho_error= load_defaults(
"my",load_default_groups,&argc,&argv)) ||
171 (ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
176 password=get_tty_password(NullS);
181 int main(
int argc,
char **argv)
184 pthread_attr_t thr_attr;
188 get_options(argc,argv);
190 if ((error=pthread_cond_init(&COND_thread_count,NULL)))
192 fprintf(stderr,
"Got error: %d from pthread_cond_init (errno: %d)",
196 pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
198 if ((error=pthread_attr_init(&thr_attr)))
200 fprintf(stderr,
"Got error: %d from pthread_attr_init (errno: %d)",
204 if ((error=pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED)))
207 "Got error: %d from pthread_attr_setdetachstate (errno: %d)",
212 printf(
"Init ok. Creating %d threads\n",number_of_threads);
213 for (i=1 ; i <= number_of_threads ; i++)
217 if (verbose) { putchar(
'+'); fflush(stdout); }
218 pthread_mutex_lock(&LOCK_thread_count);
219 if ((error=pthread_create(&tid,&thr_attr,test_thread,(
void*) param)))
221 fprintf(stderr,
"\nGot error: %d from pthread_create (errno: %d) when creating thread: %i\n",
223 pthread_mutex_unlock(&LOCK_thread_count);
227 pthread_mutex_unlock(&LOCK_thread_count);
230 printf(
"Waiting for threads to finnish\n");
231 error=pthread_mutex_lock(&LOCK_thread_count);
234 if ((error=pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
235 fprintf(stderr,
"\nGot error: %d from pthread_cond_wait\n",error);
237 pthread_mutex_unlock(&LOCK_thread_count);
238 pthread_attr_destroy(&thr_attr);