thread_test.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:8k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #include <global.h>
  18. #ifndef THREAD
  19. int main(int argc, char **argv)
  20. {
  21.   printf("This test must be compiled with multithread support to workn");
  22.   exit(1);
  23. }
  24. #else
  25. #include <my_sys.h>
  26. #include <my_pthread.h>
  27. #include "mysql.h"
  28. #include <getopt.h>
  29. static my_bool version,verbose;
  30. static uint thread_count,number_of_tests=1000,number_of_threads=2;
  31. static pthread_cond_t COND_thread_count;
  32. static pthread_mutex_t LOCK_thread_count;
  33. static char *database,*host,*user,*password,*unix_socket,*query;
  34. uint tcp_port;
  35. #ifndef __WIN__
  36. void *test_thread(void *arg)
  37. #else
  38. unsigned __stdcall test_thread(void *arg)
  39. #endif
  40. {
  41.   MYSQL *mysql;
  42.   uint count;
  43.   mysql=mysql_init(NULL);
  44.   if (!mysql_real_connect(mysql,host,user,password,database,tcp_port,
  45.   unix_socket,0))
  46.   {
  47.     fprintf(stderr,"Couldn't connect to engine!n%snn",mysql_error(mysql));
  48.     perror("");
  49.     goto end;
  50.   }
  51.   if (verbose) { putchar('*'); fflush(stdout); }
  52.   for (count=0 ; count < number_of_tests ; count++)
  53.   {
  54.     MYSQL_RES *res;
  55.     if (mysql_query(mysql,query))
  56.     {
  57.       fprintf(stderr,"Query failed (%s)n",mysql_error(mysql));
  58.       goto end;
  59.     }
  60.     if (!(res=mysql_store_result(mysql)))
  61.     {
  62.       fprintf(stderr,"Couldn't get result from %sn", mysql_error(mysql));
  63.       goto end;
  64.     }
  65.     mysql_free_result(res);
  66.     if (verbose) { putchar('.'); fflush(stdout); }
  67.   }
  68. end:
  69.   if (verbose) { putchar('#'); fflush(stdout); }
  70.   mysql_close(mysql);
  71.   pthread_mutex_lock(&LOCK_thread_count);
  72.   thread_count--;
  73.   VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
  74.   pthread_mutex_unlock(&LOCK_thread_count);
  75.   pthread_exit(0);
  76.   return 0;
  77. }
  78. static struct option long_options[] =
  79. {
  80.   {"help", no_argument,    0, '?'},
  81.   {"database",  required_argument, 0, 'D'},
  82.   {"host", required_argument, 0, 'h'},
  83.   {"password", optional_argument, 0, 'p'},
  84.   {"user", required_argument, 0, 'u'},
  85.   {"version", no_argument,    0, 'V'},
  86.   {"verbose", no_argument,    0, 'v'},
  87.   {"query", required_argument, 0, 'Q'},
  88.   {"port", required_argument, 0, 'P'},
  89.   {"socket", required_argument, 0, 'S'},
  90.   {"test-count",required_argument, 0, 'c'},
  91.   {"thread-count",required_argument, 0, 't'},
  92.   {0, 0, 0, 0}
  93. };
  94. static const char *load_default_groups[]= { "client",0 };
  95. static void usage()
  96. {
  97.   printf("Connection to a mysql server with multiple threadsn");
  98.   if (version)
  99.     return;
  100.   puts("This software comes with ABSOLUTELY NO WARRANTY.n");
  101.   printf("Usage: %s [OPTIONS] [database]n", my_progname);
  102.   printf("n
  103.   -?, --help Display this help and exitn
  104.   -c #, --test-count=# Run test count times (default %d)n",number_of_tests);
  105.   printf("
  106.   -D, --database=.. Database to usen
  107.   -h, --host=... Connect to hostn
  108.   -p[password], --password[=...]n
  109. Password to use when connecting to servern
  110. If password is not given it's asked from the tty.n");
  111.   printf("n
  112.   -P  --port=... Port number to use for connectionn
  113.   -Q, --query=... Query to execute in each threadsn
  114.   -S  --socket=... Socket file to use for connectionn");
  115.   printf("
  116.   -t  --thread-count=# Number of threads to start (default: %d) n
  117.   -u, --user=# User for login if not current usern
  118.   -v, --verbose Write some progress indicatorsn
  119.   -V, --version Output version information and exitn",
  120.  number_of_threads);
  121.   print_defaults("my",load_default_groups);
  122.   printf("nExample usage:nn
  123. %s -Q 'select * from mysql.user' -c %d -t %dn",
  124.  my_progname, number_of_tests, number_of_threads);
  125. }
  126. static void get_options(int argc, char **argv)
  127. {
  128.   int c,option_index=0,error=0;
  129.   bool tty_password=0;
  130.   load_defaults("my",load_default_groups,&argc,&argv);
  131.   while ((c=getopt_long(argc,argv, "c:D:h:p::VQ:P:S:t:?I",
  132. long_options, &option_index)) != EOF)
  133.   {
  134.     switch (c) {
  135.     case 'c':
  136.       number_of_tests=atoi(optarg);
  137.       break;
  138.     case 'D':
  139.       my_free(database,MYF(MY_ALLOW_ZERO_PTR));      
  140.       database=my_strdup(optarg,MYF(MY_WME));
  141.       break;
  142.     case 'h':
  143.       host = optarg;
  144.       break;
  145.     case 'Q': /* Allow old 'q' option */
  146.       query= optarg;
  147.       break;
  148.     case 'p':
  149.       if (optarg)
  150.       {
  151. my_free(password,MYF(MY_ALLOW_ZERO_PTR));
  152. password=my_strdup(optarg,MYF(MY_FAE));
  153. while (*optarg) *optarg++= 'x'; /* Destroy argument */
  154.       }
  155.       else
  156. tty_password=1;
  157.       break;
  158.     case 'u':
  159.       my_free(user,MYF(MY_ALLOW_ZERO_PTR));
  160.       user= my_strdup(optarg,MYF(0));
  161.       break;
  162.     case 'P':
  163.       tcp_port= (unsigned int) atoi(optarg);
  164.       break;
  165.     case 'S':
  166.       my_free(unix_socket,MYF(MY_ALLOW_ZERO_PTR));
  167.       unix_socket= my_strdup(optarg,MYF(0));
  168.       break;
  169.     case 't':
  170.       number_of_threads=atoi(optarg);
  171.       break;
  172.     case 'v':
  173.       verbose=1;
  174.       break;
  175.     case 'V':
  176.       version=1;
  177.       usage();
  178.       exit(0);
  179.       break;
  180.     default:
  181.       fprintf(stderr,"Illegal option character '%c'n",opterr);
  182.       /* Fall through */
  183.     case '?':
  184.     case 'I': /* Info */
  185.       error++;
  186.       break;
  187.     }
  188.   }
  189.   if (error || argc != optind)
  190.   {
  191.     usage();
  192.     exit(1);
  193.   }
  194.   free_defaults(argv);
  195.   if (tty_password)
  196.     password=get_tty_password(NullS);
  197.   return;
  198. }
  199. int main(int argc, char **argv)
  200. {
  201.   pthread_t tid;
  202.   pthread_attr_t thr_attr;
  203.   int i,error;
  204.   MY_INIT(argv[0]);
  205.   get_options(argc,argv);
  206.   if ((error=pthread_cond_init(&COND_thread_count,NULL)))
  207.   {
  208.     fprintf(stderr,"Got error: %d from pthread_cond_init (errno: %d)",
  209.     error,errno);
  210.     exit(1);
  211.   }
  212.   pthread_mutex_init(&LOCK_thread_count,NULL);
  213.   if ((error=pthread_attr_init(&thr_attr)))
  214.   {
  215.     fprintf(stderr,"Got error: %d from pthread_attr_init (errno: %d)",
  216.     error,errno);
  217.     exit(1);
  218.   }
  219.   if ((error=pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED)))
  220.   {
  221.     fprintf(stderr,
  222.     "Got error: %d from pthread_attr_setdetachstate (errno: %d)",
  223.     error,errno);
  224.     exit(1);
  225.   }
  226.   printf("Init ok. Creating %d threadsn",number_of_threads);
  227.   for (i=1 ; i <= number_of_threads ; i++)
  228.   {
  229.     int *param= &i;
  230.     if (verbose) { putchar('+'); fflush(stdout); }
  231.     pthread_mutex_lock(&LOCK_thread_count);
  232.     if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param)))
  233.     {
  234.       fprintf(stderr,"nGot error: %d from pthread_create (errno: %d) when creating thread: %in",
  235.       error,errno,i);
  236.       pthread_mutex_unlock(&LOCK_thread_count);
  237.       exit(1);
  238.     }
  239.     thread_count++;
  240.     pthread_mutex_unlock(&LOCK_thread_count);
  241.   }
  242.   printf("Waiting for threads to finnishn");
  243.   error=pthread_mutex_lock(&LOCK_thread_count);
  244.   while (thread_count)
  245.   {
  246.     if ((error=pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
  247.       fprintf(stderr,"nGot error: %d from pthread_cond_waitn",error);
  248.   }
  249.   pthread_mutex_unlock(&LOCK_thread_count);
  250.   pthread_attr_destroy(&thr_attr);
  251.   printf("nendn");
  252.   my_end(0);
  253.   return 0;
  254.   exit(0);
  255.   return 0; /* Keep some compilers happy */
  256. }
  257. #endif /* THREAD */