69 #define SLAP_VERSION "1.0"
71 #define HUGE_STRING_LENGTH 8196
72 #define RAND_STRING_SIZE 126
78 #define UPDATE_TYPE_REQUIRES_PREFIX 3
79 #define CREATE_TABLE_TYPE 4
80 #define SELECT_TYPE_REQUIRES_PREFIX 5
81 #define DELETE_TYPE_REQUIRES_PREFIX 6
83 #include "client_priv.h"
84 #include "my_default.h"
85 #include <mysqld_error.h>
89 #include <sslopt-vars.h>
90 #include <sys/types.h>
95 #include <welcome_copyright_notice.h>
100 #define snprintf _snprintf
104 static char *shared_memory_base_name=0;
109 pthread_mutex_t counter_mutex;
110 pthread_cond_t count_threshhold;
112 pthread_mutex_t sleeper_mutex;
113 pthread_cond_t sleep_threshhold;
115 static char **defaults_argv;
118 unsigned long long primary_keys_number_of;
120 static char *host= NULL, *opt_password= NULL, *user= NULL,
121 *user_supplied_query= NULL,
122 *user_supplied_pre_statements= NULL,
123 *user_supplied_post_statements= NULL,
127 *opt_mysql_unix_port= NULL;
128 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
129 static uint opt_enable_cleartext_plugin= 0;
130 static my_bool using_opt_enable_cleartext_plugin= 0;
132 const char *delimiter=
"\n";
134 const char *create_schema_string=
"mysqlslap";
136 static my_bool opt_preserve= TRUE, opt_no_drop= FALSE;
137 static my_bool debug_info_flag= 0, debug_check_flag= 0;
138 static my_bool opt_only_print= FALSE;
139 static my_bool opt_compress= FALSE, tty_password= FALSE,
141 auto_generate_sql_autoincrement= FALSE,
142 auto_generate_sql_guid_primary= FALSE,
143 auto_generate_sql= FALSE;
144 const char *auto_generate_sql_type=
"mixed";
146 static unsigned long connect_flags= CLIENT_MULTI_RESULTS |
147 CLIENT_MULTI_STATEMENTS |
148 CLIENT_REMEMBER_OPTIONS;
151 static int verbose, delimiter_length;
152 static uint commit_rate;
153 static uint detach_rate;
154 const char *num_int_cols_opt;
155 const char *num_char_cols_opt;
158 static unsigned int num_int_cols= 1;
159 static unsigned int num_char_cols= 1;
160 static unsigned int num_int_cols_index= 0;
161 static unsigned int num_char_cols_index= 0;
162 static unsigned int iterations;
163 static uint my_end_arg= 0;
164 static char *default_charset= (
char*) MYSQL_DEFAULT_CHARSET_NAME;
165 static ulonglong actual_queries= 0;
166 static ulonglong auto_actual_queries;
167 static ulonglong auto_generate_sql_unique_write_number;
168 static ulonglong auto_generate_sql_unique_query_number;
169 static unsigned int auto_generate_sql_secondary_indexes;
170 static ulonglong num_of_query;
171 static ulonglong auto_generate_sql_number;
172 const char *concurrency_str= NULL;
173 static char *create_string;
176 const char *default_dbug_option=
"d:t:o,/tmp/mysqlslap.trace";
177 const char *opt_csv_str;
180 static uint opt_protocol= 0;
182 static int get_options(
int *argc,
char ***argv);
183 static uint opt_mysql_port= 0;
185 static const char *load_default_groups[]= {
"mysqlslap",
"client",0 };
194 size_t option_length;
204 size_t option_length;
213 unsigned long long rows;
231 unsigned long long avg_rows;
233 unsigned long long max_rows;
234 unsigned long long min_rows;
240 static statement *create_statements= NULL,
241 *query_statements= NULL;
247 uint parse_comma(
const char *
string, uint **range);
248 uint parse_delimiter(
const char *script,
statement **stmt,
char delm);
249 uint parse_option(
const char *origin,
option_string **stmt,
char delm);
250 static int drop_schema(
MYSQL *mysql,
const char *db);
251 uint get_random_string(
char *
buf);
252 static statement *build_table_string(
void);
253 static statement *build_insert_string(
void);
254 static statement *build_update_string(
void);
255 static statement * build_select_string(my_bool key);
257 static int drop_primary_key_list(
void);
258 static int create_schema(
MYSQL *mysql,
const char *db,
statement *stmt,
260 static int run_scheduler(
stats *sptr,
statement *stmts, uint concur,
262 pthread_handler_t run_task(
void *p);
267 int slap_connect(
MYSQL *mysql);
268 static int run_query(
MYSQL *mysql,
const char *
query,
int len);
270 static const char ALPHANUMERICS[]=
271 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
273 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
280 us = a.tv_usec - b.tv_usec;
282 s = a.tv_sec - b.tv_sec;
288 static int gettimeofday(
struct timeval *tp,
void *tzp)
291 ticks= GetTickCount();
292 tp->tv_usec= ticks*1000;
293 tp->tv_sec= ticks/1000;
299 int main(
int argc,
char **argv)
306 my_getopt_use_args_separator= TRUE;
307 if (load_defaults(
"my",load_default_groups,&argc,&argv))
312 my_getopt_use_args_separator= FALSE;
314 if (get_options(&argc,&argv))
316 free_defaults(defaults_argv);
322 if (auto_generate_sql)
323 srandom((uint)time(NULL));
326 delimiter_length= strlen(delimiter);
330 fprintf(stderr,
"%s: Too many arguments\n",my_progname);
331 free_defaults(defaults_argv);
337 mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
341 mysql_ssl_set(&mysql, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
342 opt_ssl_capath, opt_ssl_cipher);
343 mysql_options(&mysql, MYSQL_OPT_SSL_CRL, opt_ssl_crl);
344 mysql_options(&mysql, MYSQL_OPT_SSL_CRLPATH, opt_ssl_crlpath);
348 mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(
char*)&opt_protocol);
350 if (shared_memory_base_name)
351 mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
353 mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);
355 if (opt_plugin_dir && *opt_plugin_dir)
356 mysql_options(&mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
358 if (opt_default_auth && *opt_default_auth)
359 mysql_options(&mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
361 mysql_options(&mysql, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
362 mysql_options4(&mysql, MYSQL_OPT_CONNECT_ATTR_ADD,
363 "program_name",
"mysqlslap");
364 if (using_opt_enable_cleartext_plugin)
365 mysql_options(&mysql, MYSQL_ENABLE_CLEARTEXT_PLUGIN,
366 (
char*) &opt_enable_cleartext_plugin);
369 if (!(mysql_real_connect(&mysql, host, user, opt_password,
370 NULL, opt_mysql_port,
371 opt_mysql_unix_port, connect_flags)))
373 fprintf(stderr,
"%s: Error when connecting to server: %s\n",
374 my_progname,mysql_error(&mysql));
375 free_defaults(defaults_argv);
381 pthread_mutex_init(&counter_mutex, NULL);
382 pthread_cond_init(&count_threshhold, NULL);
383 pthread_mutex_init(&sleeper_mutex, NULL);
384 pthread_cond_init(&sleep_threshhold, NULL);
387 eptr= engine_options;
394 printf(
"Starting Concurrency Test\n");
398 for (current= concurrency; current && *current; current++)
399 concurrency_loop(&mysql, *current, eptr);
405 concurrency_loop(&mysql, infinite, eptr);
411 drop_schema(&mysql, create_schema_string);
413 }
while (eptr ? (eptr= eptr->next) : 0);
415 pthread_mutex_destroy(&counter_mutex);
416 pthread_cond_destroy(&count_threshhold);
417 pthread_mutex_destroy(&sleeper_mutex);
418 pthread_cond_destroy(&sleep_threshhold);
424 my_free(opt_password);
425 my_free(concurrency);
427 statement_cleanup(create_statements);
428 statement_cleanup(query_statements);
429 statement_cleanup(pre_statements);
430 statement_cleanup(post_statements);
431 option_cleanup(engine_options);
434 my_free(shared_memory_base_name);
436 free_defaults(defaults_argv);
448 unsigned long long client_limit;
451 head_sptr= (
stats *)my_malloc(
sizeof(
stats) * iterations,
452 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
456 if (auto_actual_queries)
457 client_limit= auto_actual_queries;
458 else if (num_of_query)
459 client_limit= num_of_query / current;
461 client_limit= actual_queries;
463 for (x= 0, sptr= head_sptr; x < iterations; x++, sptr++)
471 drop_schema(mysql, create_schema_string);
474 if (create_statements)
475 create_schema(mysql, create_schema_string, create_statements, eptr);
482 printf(
"Generating primary key list\n");
483 if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
484 generate_primary_key_list(mysql, eptr);
487 run_query(mysql,
"SET AUTOCOMMIT=0", strlen(
"SET AUTOCOMMIT=0"));
490 if ((sysret= system(pre_system)) != 0)
491 fprintf(stderr,
"Warning: Execution of pre_system option returned %d.\n",
499 run_statements(mysql, pre_statements);
501 run_scheduler(sptr, query_statements, current, client_limit);
504 run_statements(mysql, post_statements);
507 if ((sysret= system(post_system)) != 0)
508 fprintf(stderr,
"Warning: Execution of post_system option returned %d.\n",
512 if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
513 drop_primary_key_list();
517 printf(
"Generating stats\n");
519 generate_stats(&conclusion, eptr, head_sptr);
522 print_conclusions(&conclusion);
524 print_conclusions_csv(&conclusion);
531 static struct my_option my_long_options[] =
533 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
535 {
"auto-generate-sql",
'a',
536 "Generate SQL where not supplied by file or command line.",
537 &auto_generate_sql, &auto_generate_sql,
538 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
539 {
"auto-generate-sql-add-autoincrement", OPT_SLAP_AUTO_GENERATE_ADD_AUTO,
540 "Add an AUTO_INCREMENT column to auto-generated tables.",
541 &auto_generate_sql_autoincrement,
542 &auto_generate_sql_autoincrement,
543 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
544 {
"auto-generate-sql-execute-number", OPT_SLAP_AUTO_GENERATE_EXECUTE_QUERIES,
545 "Set this number to generate a set number of queries to run.",
546 &auto_actual_queries, &auto_actual_queries,
547 0, GET_ULL, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
548 {
"auto-generate-sql-guid-primary", OPT_SLAP_AUTO_GENERATE_GUID_PRIMARY,
549 "Add GUID based primary keys to auto-generated tables.",
550 &auto_generate_sql_guid_primary,
551 &auto_generate_sql_guid_primary,
552 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
553 {
"auto-generate-sql-load-type", OPT_SLAP_AUTO_GENERATE_SQL_LOAD_TYPE,
554 "Specify test load type: mixed, update, write, key, or read; default is mixed.",
555 &auto_generate_sql_type, &auto_generate_sql_type,
556 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
557 {
"auto-generate-sql-secondary-indexes",
558 OPT_SLAP_AUTO_GENERATE_SECONDARY_INDEXES,
559 "Number of secondary indexes to add to auto-generated tables.",
560 &auto_generate_sql_secondary_indexes,
561 &auto_generate_sql_secondary_indexes, 0,
562 GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
563 {
"auto-generate-sql-unique-query-number",
564 OPT_SLAP_AUTO_GENERATE_UNIQUE_QUERY_NUM,
565 "Number of unique queries to generate for automatic tests.",
566 &auto_generate_sql_unique_query_number,
567 &auto_generate_sql_unique_query_number,
568 0, GET_ULL, REQUIRED_ARG, 10, 0, 0, 0, 0, 0},
569 {
"auto-generate-sql-unique-write-number",
570 OPT_SLAP_AUTO_GENERATE_UNIQUE_WRITE_NUM,
571 "Number of unique queries to generate for auto-generate-sql-write-number.",
572 &auto_generate_sql_unique_write_number,
573 &auto_generate_sql_unique_write_number,
574 0, GET_ULL, REQUIRED_ARG, 10, 0, 0, 0, 0, 0},
575 {
"auto-generate-sql-write-number", OPT_SLAP_AUTO_GENERATE_WRITE_NUM,
576 "Number of row inserts to perform for each thread (default is 100).",
577 &auto_generate_sql_number, &auto_generate_sql_number,
578 0, GET_ULL, REQUIRED_ARG, 100, 0, 0, 0, 0, 0},
579 {
"commit", OPT_SLAP_COMMIT,
"Commit records every X number of statements.",
580 &commit_rate, &commit_rate, 0, GET_UINT, REQUIRED_ARG,
582 {
"compress",
'C',
"Use compression in server/client protocol.",
583 &opt_compress, &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
585 {
"concurrency",
'c',
"Number of clients to simulate for query to run.",
586 &concurrency_str, &concurrency_str, 0, GET_STR,
587 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
588 {
"create", OPT_SLAP_CREATE_STRING,
"File or string to use create tables.",
589 &create_string, &create_string, 0, GET_STR, REQUIRED_ARG,
591 {
"create-schema", OPT_CREATE_SLAP_SCHEMA,
"Schema to run tests in.",
592 &create_schema_string, &create_schema_string, 0, GET_STR,
593 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
594 {
"csv", OPT_SLAP_CSV,
595 "Generate CSV output to named file or to stdout if no file is named.",
596 NULL, NULL, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
598 {
"debug",
'#',
"This is a non-debug version. Catch this and exit.",
599 0, 0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
601 {
"debug",
'#',
"Output debug log. Often this is 'd:t:o,filename'.",
602 &default_dbug_option, &default_dbug_option, 0, GET_STR,
603 OPT_ARG, 0, 0, 0, 0, 0, 0},
605 {
"debug-check", OPT_DEBUG_CHECK,
"Check memory and open file usage at exit.",
606 &debug_check_flag, &debug_check_flag, 0,
607 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
608 {
"debug-info",
'T',
"Print some debug info at exit.", &debug_info_flag,
609 &debug_info_flag, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
610 {
"default_auth", OPT_DEFAULT_AUTH,
611 "Default authentication client-side plugin to use.",
612 &opt_default_auth, &opt_default_auth, 0,
613 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
615 "Delimiter to use in SQL statements supplied in file or command line.",
616 &delimiter, &delimiter, 0, GET_STR, REQUIRED_ARG,
618 {
"detach", OPT_SLAP_DETACH,
619 "Detach (close and reopen) connections after X number of requests.",
620 &detach_rate, &detach_rate, 0, GET_UINT, REQUIRED_ARG,
622 {
"enable_cleartext_plugin", OPT_ENABLE_CLEARTEXT_PLUGIN,
623 "Enable/disable the clear text authentication plugin.",
624 &opt_enable_cleartext_plugin, &opt_enable_cleartext_plugin,
625 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
626 {
"engine",
'e',
"Storage engine to use for creating the table.",
628 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
629 {
"host",
'h',
"Connect to host.", &host, &host, 0, GET_STR,
630 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
631 {
"iterations",
'i',
"Number of times to run the tests.", &iterations,
632 &iterations, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
633 {
"no-drop", OPT_SLAP_NO_DROP,
"Do not drop the schema after the test.",
634 &opt_no_drop, &opt_no_drop, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
635 {
"number-char-cols",
'x',
636 "Number of VARCHAR columns to create in table if specifying --auto-generate-sql.",
637 &num_char_cols_opt, &num_char_cols_opt, 0, GET_STR, REQUIRED_ARG,
639 {
"number-int-cols",
'y',
640 "Number of INT columns to create in table if specifying --auto-generate-sql.",
641 &num_int_cols_opt, &num_int_cols_opt, 0, GET_STR, REQUIRED_ARG,
643 {
"number-of-queries", OPT_MYSQL_NUMBER_OF_QUERY,
644 "Limit each client to this number of queries (this is not exact).",
645 &num_of_query, &num_of_query, 0,
646 GET_ULL, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
647 {
"only-print", OPT_MYSQL_ONLY_PRINT,
648 "Do not connect to the databases, but instead print out what would have "
650 &opt_only_print, &opt_only_print, 0, GET_BOOL, NO_ARG,
653 "Password to use when connecting to server. If password is not given it's "
654 "asked from the tty.", 0, 0, 0, GET_PASSWORD, OPT_ARG, 0, 0, 0, 0, 0, 0},
656 {
"pipe",
'W',
"Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
657 NO_ARG, 0, 0, 0, 0, 0, 0},
659 {
"plugin_dir", OPT_PLUGIN_DIR,
"Directory for client-side plugins.",
660 &opt_plugin_dir, &opt_plugin_dir, 0,
661 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
662 {
"port",
'P',
"Port number to use for connection.", &opt_mysql_port,
663 &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, MYSQL_PORT, 0, 0, 0, 0,
665 {
"post-query", OPT_SLAP_POST_QUERY,
666 "Query to run or file containing query to execute after tests have completed.",
667 &user_supplied_post_statements, &user_supplied_post_statements,
668 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
669 {
"post-system", OPT_SLAP_POST_SYSTEM,
670 "system() string to execute after tests have completed.",
671 &post_system, &post_system,
672 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
673 {
"pre-query", OPT_SLAP_PRE_QUERY,
674 "Query to run or file containing query to execute before running tests.",
675 &user_supplied_pre_statements, &user_supplied_pre_statements,
676 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
677 {
"pre-system", OPT_SLAP_PRE_SYSTEM,
678 "system() string to execute before running tests.",
679 &pre_system, &pre_system,
680 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
681 {
"protocol", OPT_MYSQL_PROTOCOL,
682 "The protocol to use for connection (tcp, socket, pipe, memory).",
683 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
684 {
"query",
'q',
"Query to run or file containing query to run.",
685 &user_supplied_query, &user_supplied_query,
686 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
688 {
"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
689 "Base name of shared memory.", &shared_memory_base_name,
690 &shared_memory_base_name, 0, GET_STR_ALLOC, REQUIRED_ARG,
693 {
"silent",
's',
"Run program in silent mode - no output.",
694 &opt_silent, &opt_silent, 0, GET_BOOL, NO_ARG,
696 {
"socket",
'S',
"The socket file to use for connection.",
697 &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR,
698 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
699 #include <sslopt-longopts.h>
700 #ifndef DONT_ALLOW_USER_CHANGE
701 {
"user",
'u',
"User for login if not current user.", &user,
702 &user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
705 "More verbose output; you can use this multiple times to get even more "
706 "verbose output.", &verbose, &verbose, 0, GET_NO_ARG, NO_ARG,
708 {
"version",
'V',
"Output version information and exit.", 0, 0, 0,
709 GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
710 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
714 static void print_version(
void)
716 printf(
"%s Ver %s Distrib %s, for %s (%s)\n",my_progname, SLAP_VERSION,
717 MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
721 static void usage(
void)
724 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2005"));
725 puts(
"Run a query multiple times against the server.\n");
726 printf(
"Usage: %s [OPTIONS]\n",my_progname);
727 print_defaults(
"my",load_default_groups);
728 my_print_help(my_long_options);
733 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
736 DBUG_ENTER(
"get_one_option");
742 if (argument == disabled_my_option)
743 argument= (
char*)
"";
746 char *start= argument;
747 my_free(opt_password);
748 opt_password= my_strdup(argument,MYF(MY_FAE));
749 while (*argument) *argument++=
'x';
759 opt_protocol= MYSQL_PROTOCOL_PIPE;
762 case OPT_MYSQL_PROTOCOL:
763 opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
767 DBUG_PUSH(argument ? argument : default_dbug_option);
772 argument= (
char *)
"-";
773 opt_csv_str= argument;
775 #include <sslopt-case.h>
784 case OPT_ENABLE_CLEARTEXT_PLUGIN:
785 using_opt_enable_cleartext_plugin= TRUE;
793 get_random_string(
char *
buf)
797 DBUG_ENTER(
"get_random_string");
798 for (x= RAND_STRING_SIZE; x > 0; x--)
799 *buf_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
800 DBUG_RETURN(buf_ptr - buf);
811 build_table_string(
void)
813 char buf[HUGE_STRING_LENGTH];
814 unsigned int col_count;
817 DBUG_ENTER(
"build_table_string");
819 DBUG_PRINT(
"info", (
"num int cols %u num char cols %u",
820 num_int_cols, num_char_cols));
822 init_dynamic_string(&table_string,
"", 1024, 1024);
824 dynstr_append(&table_string,
"CREATE TABLE `t1` (");
826 if (auto_generate_sql_autoincrement)
828 dynstr_append(&table_string,
"id serial");
830 if (num_int_cols || num_char_cols)
831 dynstr_append(&table_string,
",");
834 if (auto_generate_sql_guid_primary)
836 dynstr_append(&table_string,
"id varchar(32) primary key");
838 if (num_int_cols || num_char_cols || auto_generate_sql_guid_primary)
839 dynstr_append(&table_string,
",");
842 if (auto_generate_sql_secondary_indexes)
846 for (count= 0; count < auto_generate_sql_secondary_indexes; count++)
849 dynstr_append(&table_string,
",");
851 if (snprintf(buf, HUGE_STRING_LENGTH,
"id%d varchar(32) unique key", count)
852 > HUGE_STRING_LENGTH)
854 fprintf(stderr,
"Memory Allocation error in create table\n");
857 dynstr_append(&table_string, buf);
860 if (num_int_cols || num_char_cols)
861 dynstr_append(&table_string,
",");
865 for (col_count= 1; col_count <= num_int_cols; col_count++)
867 if (num_int_cols_index)
869 if (snprintf(buf, HUGE_STRING_LENGTH,
"intcol%d INT(32), INDEX(intcol%d)",
870 col_count, col_count) > HUGE_STRING_LENGTH)
872 fprintf(stderr,
"Memory Allocation error in create table\n");
878 if (snprintf(buf, HUGE_STRING_LENGTH,
"intcol%d INT(32) ", col_count)
879 > HUGE_STRING_LENGTH)
881 fprintf(stderr,
"Memory Allocation error in create table\n");
885 dynstr_append(&table_string, buf);
887 if (col_count < num_int_cols || num_char_cols > 0)
888 dynstr_append(&table_string,
",");
892 for (col_count= 1; col_count <= num_char_cols; col_count++)
894 if (num_char_cols_index)
896 if (snprintf(buf, HUGE_STRING_LENGTH,
897 "charcol%d VARCHAR(128), INDEX(charcol%d) ",
898 col_count, col_count) > HUGE_STRING_LENGTH)
900 fprintf(stderr,
"Memory Allocation error in creating table\n");
906 if (snprintf(buf, HUGE_STRING_LENGTH,
"charcol%d VARCHAR(128)",
907 col_count) > HUGE_STRING_LENGTH)
909 fprintf(stderr,
"Memory Allocation error in creating table\n");
913 dynstr_append(&table_string, buf);
915 if (col_count < num_char_cols)
916 dynstr_append(&table_string,
",");
919 dynstr_append(&table_string,
")");
921 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
922 ptr->string = (
char *)my_malloc(table_string.length+1,
923 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
924 ptr->length= table_string.length+1;
925 ptr->type= CREATE_TABLE_TYPE;
926 strmov(ptr->string, table_string.str);
927 dynstr_free(&table_string);
938 build_update_string(
void)
940 char buf[HUGE_STRING_LENGTH];
941 unsigned int col_count;
944 DBUG_ENTER(
"build_update_string");
946 init_dynamic_string(&update_string,
"", 1024, 1024);
948 dynstr_append(&update_string,
"UPDATE t1 SET ");
951 for (col_count= 1; col_count <= num_int_cols; col_count++)
953 if (snprintf(buf, HUGE_STRING_LENGTH,
"intcol%d = %ld", col_count,
954 random()) > HUGE_STRING_LENGTH)
956 fprintf(stderr,
"Memory Allocation error in creating update\n");
959 dynstr_append(&update_string, buf);
961 if (col_count < num_int_cols || num_char_cols > 0)
962 dynstr_append_mem(&update_string,
",", 1);
966 for (col_count= 1; col_count <= num_char_cols; col_count++)
968 char rand_buffer[RAND_STRING_SIZE];
969 int buf_len= get_random_string(rand_buffer);
971 if (snprintf(buf, HUGE_STRING_LENGTH,
"charcol%d = '%.*s'", col_count,
972 buf_len, rand_buffer)
973 > HUGE_STRING_LENGTH)
975 fprintf(stderr,
"Memory Allocation error in creating update\n");
978 dynstr_append(&update_string, buf);
980 if (col_count < num_char_cols)
981 dynstr_append_mem(&update_string,
",", 1);
984 if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
985 dynstr_append(&update_string,
" WHERE id = ");
989 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
991 ptr->string= (
char *)my_malloc(update_string.length + 1,
992 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
993 ptr->length= update_string.length+1;
994 if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
995 ptr->type= UPDATE_TYPE_REQUIRES_PREFIX ;
997 ptr->type= UPDATE_TYPE;
998 strmov(ptr->string, update_string.str);
999 dynstr_free(&update_string);
1011 build_insert_string(
void)
1013 char buf[HUGE_STRING_LENGTH];
1014 unsigned int col_count;
1017 DBUG_ENTER(
"build_insert_string");
1019 init_dynamic_string(&insert_string,
"", 1024, 1024);
1021 dynstr_append(&insert_string,
"INSERT INTO t1 VALUES (");
1023 if (auto_generate_sql_autoincrement)
1025 dynstr_append(&insert_string,
"NULL");
1027 if (num_int_cols || num_char_cols)
1028 dynstr_append(&insert_string,
",");
1031 if (auto_generate_sql_guid_primary)
1033 dynstr_append(&insert_string,
"uuid()");
1035 if (num_int_cols || num_char_cols)
1036 dynstr_append(&insert_string,
",");
1039 if (auto_generate_sql_secondary_indexes)
1043 for (count= 0; count < auto_generate_sql_secondary_indexes; count++)
1046 dynstr_append(&insert_string,
",");
1048 dynstr_append(&insert_string,
"uuid()");
1051 if (num_int_cols || num_char_cols)
1052 dynstr_append(&insert_string,
",");
1056 for (col_count= 1; col_count <= num_int_cols; col_count++)
1058 if (snprintf(buf, HUGE_STRING_LENGTH,
"%ld", random()) > HUGE_STRING_LENGTH)
1060 fprintf(stderr,
"Memory Allocation error in creating insert\n");
1063 dynstr_append(&insert_string, buf);
1065 if (col_count < num_int_cols || num_char_cols > 0)
1066 dynstr_append_mem(&insert_string,
",", 1);
1070 for (col_count= 1; col_count <= num_char_cols; col_count++)
1072 int buf_len= get_random_string(buf);
1073 dynstr_append_mem(&insert_string,
"'", 1);
1074 dynstr_append_mem(&insert_string, buf, buf_len);
1075 dynstr_append_mem(&insert_string,
"'", 1);
1077 if (col_count < num_char_cols)
1078 dynstr_append_mem(&insert_string,
",", 1);
1081 dynstr_append_mem(&insert_string,
")", 1);
1084 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1085 ptr->string= (
char *)my_malloc(insert_string.length + 1,
1086 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1087 ptr->length= insert_string.length+1;
1088 ptr->type= INSERT_TYPE;
1089 strmov(ptr->string, insert_string.str);
1090 dynstr_free(&insert_string);
1102 build_select_string(my_bool key)
1104 char buf[HUGE_STRING_LENGTH];
1105 unsigned int col_count;
1108 DBUG_ENTER(
"build_select_string");
1110 init_dynamic_string(&query_string,
"", 1024, 1024);
1112 dynstr_append_mem(&query_string,
"SELECT ", 7);
1113 for (col_count= 1; col_count <= num_int_cols; col_count++)
1115 if (snprintf(buf, HUGE_STRING_LENGTH,
"intcol%d", col_count)
1116 > HUGE_STRING_LENGTH)
1118 fprintf(stderr,
"Memory Allocation error in creating select\n");
1121 dynstr_append(&query_string, buf);
1123 if (col_count < num_int_cols || num_char_cols > 0)
1124 dynstr_append_mem(&query_string,
",", 1);
1127 for (col_count= 1; col_count <= num_char_cols; col_count++)
1129 if (snprintf(buf, HUGE_STRING_LENGTH,
"charcol%d", col_count)
1130 > HUGE_STRING_LENGTH)
1132 fprintf(stderr,
"Memory Allocation error in creating select\n");
1135 dynstr_append(&query_string, buf);
1137 if (col_count < num_char_cols)
1138 dynstr_append_mem(&query_string,
",", 1);
1141 dynstr_append(&query_string,
" FROM t1");
1144 (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary))
1145 dynstr_append(&query_string,
" WHERE id = ");
1148 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1149 ptr->string= (
char *)my_malloc(query_string.length + 1,
1150 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1151 ptr->length= query_string.length+1;
1153 (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary))
1154 ptr->type= SELECT_TYPE_REQUIRES_PREFIX;
1156 ptr->type= SELECT_TYPE;
1157 strmov(ptr->string, query_string.str);
1158 dynstr_free(&query_string);
1163 get_options(
int *argc,
char ***argv)
1169 DBUG_ENTER(
"get_options");
1170 if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
1172 if (debug_info_flag)
1173 my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
1174 if (debug_check_flag)
1175 my_end_arg= MY_CHECK_ERROR;
1178 user= (
char *)
"root";
1184 if (!opt_no_drop && (create_string || auto_generate_sql))
1185 opt_preserve= FALSE;
1187 if (auto_generate_sql && (create_string || user_supplied_query))
1190 "%s: Can't use --auto-generate-sql when create and query strings are specified!\n",
1195 if (auto_generate_sql && auto_generate_sql_guid_primary &&
1196 auto_generate_sql_autoincrement)
1199 "%s: Either auto-generate-sql-guid-primary or auto-generate-sql-add-autoincrement can be used!\n",
1208 if (auto_generate_sql && auto_generate_sql_type[0] ==
'k')
1209 if ( auto_generate_sql_autoincrement == FALSE &&
1210 auto_generate_sql_guid_primary == FALSE)
1213 "%s: Can't perform key test without a primary key!\n",
1220 if (auto_generate_sql && num_of_query && auto_actual_queries)
1223 "%s: Either auto-generate-sql-execute-number or number-of-queries can be used!\n",
1228 parse_comma(concurrency_str ? concurrency_str :
"1", &concurrency);
1234 if (opt_csv_str[0] ==
'-')
1236 csv_file= my_fileno(stdout);
1240 if ((csv_file= my_open(opt_csv_str, O_CREAT|O_WRONLY|O_APPEND, MYF(0)))
1243 fprintf(stderr,
"%s: Could not open csv file: %sn\n",
1244 my_progname, opt_csv_str);
1253 if (num_int_cols_opt)
1256 parse_option(num_int_cols_opt, &str,
',');
1257 num_int_cols= atoi(str->string);
1259 num_int_cols_index= atoi(str->option);
1260 option_cleanup(str);
1263 if (num_char_cols_opt)
1266 parse_option(num_char_cols_opt, &str,
',');
1267 num_char_cols= atoi(str->string);
1269 num_char_cols_index= atoi(str->option);
1271 num_char_cols_index= 0;
1272 option_cleanup(str);
1276 if (auto_generate_sql)
1278 unsigned long long x= 0;
1282 printf(
"Building Create Statements for Auto\n");
1284 create_statements= build_table_string();
1288 for (ptr_statement= create_statements, x= 0;
1289 x < auto_generate_sql_unique_write_number;
1290 x++, ptr_statement= ptr_statement->next)
1292 ptr_statement->next= build_insert_string();
1296 printf(
"Building Query Statements for Auto\n");
1298 if (auto_generate_sql_type[0] ==
'r')
1301 printf(
"Generating SELECT Statements for Auto\n");
1303 query_statements= build_select_string(FALSE);
1304 for (ptr_statement= query_statements, x= 0;
1305 x < auto_generate_sql_unique_query_number;
1306 x++, ptr_statement= ptr_statement->next)
1308 ptr_statement->next= build_select_string(FALSE);
1311 else if (auto_generate_sql_type[0] ==
'k')
1314 printf(
"Generating SELECT for keys Statements for Auto\n");
1316 query_statements= build_select_string(TRUE);
1317 for (ptr_statement= query_statements, x= 0;
1318 x < auto_generate_sql_unique_query_number;
1319 x++, ptr_statement= ptr_statement->next)
1321 ptr_statement->next= build_select_string(TRUE);
1324 else if (auto_generate_sql_type[0] ==
'w')
1332 printf(
"Generating INSERT Statements for Auto\n");
1333 query_statements= build_insert_string();
1334 for (ptr_statement= query_statements, x= 0;
1335 x < auto_generate_sql_unique_query_number;
1336 x++, ptr_statement= ptr_statement->next)
1338 ptr_statement->next= build_insert_string();
1341 else if (auto_generate_sql_type[0] ==
'u')
1343 query_statements= build_update_string();
1344 for (ptr_statement= query_statements, x= 0;
1345 x < auto_generate_sql_unique_query_number;
1346 x++, ptr_statement= ptr_statement->next)
1348 ptr_statement->next= build_update_string();
1355 query_statements= build_insert_string();
1360 for (ptr_statement= query_statements, x= 0;
1361 x < auto_generate_sql_unique_query_number;
1362 x++, ptr_statement= ptr_statement->next)
1366 ptr_statement->next= build_insert_string();
1371 ptr_statement->next= build_select_string(TRUE);
1379 if (create_string && my_stat(create_string, &sbuf, MYF(0)))
1382 if (!MY_S_ISREG(sbuf.st_mode))
1384 fprintf(stderr,
"%s: Create file was not a regular file\n",
1388 if ((data_file= my_open(create_string, O_RDWR, MYF(0))) == -1)
1390 fprintf(stderr,
"%s: Could not open create file\n", my_progname);
1393 tmp_string= (
char *)my_malloc(sbuf.st_size + 1,
1394 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1395 my_read(data_file, (uchar*) tmp_string, sbuf.st_size, MYF(0));
1396 tmp_string[sbuf.st_size]=
'\0';
1397 my_close(data_file,MYF(0));
1398 parse_delimiter(tmp_string, &create_statements, delimiter[0]);
1399 my_free(tmp_string);
1401 else if (create_string)
1403 parse_delimiter(create_string, &create_statements, delimiter[0]);
1406 if (user_supplied_query && my_stat(user_supplied_query, &sbuf, MYF(0)))
1409 if (!MY_S_ISREG(sbuf.st_mode))
1411 fprintf(stderr,
"%s: User query supplied file was not a regular file\n",
1415 if ((data_file= my_open(user_supplied_query, O_RDWR, MYF(0))) == -1)
1417 fprintf(stderr,
"%s: Could not open query supplied file\n", my_progname);
1420 tmp_string= (
char *)my_malloc(sbuf.st_size + 1,
1421 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1422 my_read(data_file, (uchar*) tmp_string, sbuf.st_size, MYF(0));
1423 tmp_string[sbuf.st_size]=
'\0';
1424 my_close(data_file,MYF(0));
1425 if (user_supplied_query)
1426 actual_queries= parse_delimiter(tmp_string, &query_statements,
1428 my_free(tmp_string);
1430 else if (user_supplied_query)
1432 actual_queries= parse_delimiter(user_supplied_query, &query_statements,
1437 if (user_supplied_pre_statements && my_stat(user_supplied_pre_statements, &sbuf, MYF(0)))
1440 if (!MY_S_ISREG(sbuf.st_mode))
1442 fprintf(stderr,
"%s: User query supplied file was not a regular file\n",
1446 if ((data_file= my_open(user_supplied_pre_statements, O_RDWR, MYF(0))) == -1)
1448 fprintf(stderr,
"%s: Could not open query supplied file\n", my_progname);
1451 tmp_string= (
char *)my_malloc(sbuf.st_size + 1,
1452 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1453 my_read(data_file, (uchar*) tmp_string, sbuf.st_size, MYF(0));
1454 tmp_string[sbuf.st_size]=
'\0';
1455 my_close(data_file,MYF(0));
1456 if (user_supplied_pre_statements)
1457 (void)parse_delimiter(tmp_string, &pre_statements,
1459 my_free(tmp_string);
1461 else if (user_supplied_pre_statements)
1463 (void)parse_delimiter(user_supplied_pre_statements,
1468 if (user_supplied_post_statements && my_stat(user_supplied_post_statements, &sbuf, MYF(0)))
1471 if (!MY_S_ISREG(sbuf.st_mode))
1473 fprintf(stderr,
"%s: User query supplied file was not a regular file\n",
1477 if ((data_file= my_open(user_supplied_post_statements, O_RDWR, MYF(0))) == -1)
1479 fprintf(stderr,
"%s: Could not open query supplied file\n", my_progname);
1482 tmp_string= (
char *)my_malloc(sbuf.st_size + 1,
1483 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1484 my_read(data_file, (uchar*) tmp_string, sbuf.st_size, MYF(0));
1485 tmp_string[sbuf.st_size]=
'\0';
1486 my_close(data_file,MYF(0));
1487 if (user_supplied_post_statements)
1488 (void)parse_delimiter(tmp_string, &post_statements,
1490 my_free(tmp_string);
1492 else if (user_supplied_post_statements)
1494 (void)parse_delimiter(user_supplied_post_statements, &post_statements,
1499 printf(
"Parsing engines to use.\n");
1505 opt_password= get_tty_password(NullS);
1510 static int run_query(
MYSQL *mysql,
const char *
query,
int len)
1514 printf(
"%.*s;\n", len, query);
1519 printf(
"%.*s;\n", len, query);
1520 return mysql_real_query(mysql, query, len);
1529 unsigned long long counter;
1530 DBUG_ENTER(
"generate_primary_key_list");
1536 if (opt_only_print || (engine_stmt &&
1537 strstr(engine_stmt->string,
"blackhole")))
1539 primary_keys_number_of= 1;
1540 primary_keys= (
char **)my_malloc((uint)(
sizeof(
char *) *
1541 primary_keys_number_of),
1542 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1544 primary_keys[0]= my_strdup(
"796c4422-1d94-102a-9d6d-00e0812d", MYF(0));
1548 if (run_query(mysql,
"SELECT id from t1", strlen(
"SELECT id from t1")))
1550 fprintf(stderr,
"%s: Cannot select GUID primary keys. (%s)\n", my_progname,
1551 mysql_error(mysql));
1555 if (!(result= mysql_store_result(mysql)))
1557 fprintf(stderr,
"%s: Error when storing result: %d %s\n",
1558 my_progname, mysql_errno(mysql), mysql_error(mysql));
1561 primary_keys_number_of= mysql_num_rows(result);
1564 if (primary_keys_number_of)
1569 primary_keys= (
char **)my_malloc((uint)(
sizeof(
char *) *
1570 primary_keys_number_of),
1571 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1572 row= mysql_fetch_row(result);
1573 for (counter= 0; counter < primary_keys_number_of;
1574 counter++, row= mysql_fetch_row(result))
1575 primary_keys[counter]= my_strdup(row[0], MYF(0));
1578 mysql_free_result(result);
1585 drop_primary_key_list(
void)
1587 unsigned long long counter;
1589 if (primary_keys_number_of)
1591 for (counter= 0; counter < primary_keys_number_of; counter++)
1592 my_free(primary_keys[counter]);
1594 my_free(primary_keys);
1604 char query[HUGE_STRING_LENGTH];
1609 DBUG_ENTER(
"create_schema");
1611 len= snprintf(query, HUGE_STRING_LENGTH,
"CREATE SCHEMA `%s`", db);
1614 printf(
"Loading Pre-data\n");
1616 if (run_query(mysql, query, len))
1618 fprintf(stderr,
"%s: Cannot create schema %s : %s\n", my_progname, db,
1619 mysql_error(mysql));
1625 printf(
"use %s;\n", db);
1630 printf(
"%s;\n", query);
1632 if (mysql_select_db(mysql, db))
1634 fprintf(stderr,
"%s: Cannot select schema '%s': %s\n",my_progname, db,
1635 mysql_error(mysql));
1642 len= snprintf(query, HUGE_STRING_LENGTH,
"set storage_engine=`%s`",
1643 engine_stmt->string);
1644 if (run_query(mysql, query, len))
1646 fprintf(stderr,
"%s: Cannot set default engine: %s\n", my_progname,
1647 mysql_error(mysql));
1656 for (ptr= after_create; ptr && ptr->length; ptr= ptr->next, count++)
1658 if (auto_generate_sql && ( auto_generate_sql_number == count))
1661 if (engine_stmt && engine_stmt->option && ptr->type == CREATE_TABLE_TYPE)
1663 char buffer[HUGE_STRING_LENGTH];
1665 snprintf(buffer, HUGE_STRING_LENGTH,
"%s %s", ptr->string,
1666 engine_stmt->option);
1667 if (run_query(mysql, buffer, strlen(buffer)))
1669 fprintf(stderr,
"%s: Cannot run query %.*s ERROR : %s\n",
1670 my_progname, (uint)ptr->length, ptr->string, mysql_error(mysql));
1676 if (run_query(mysql, ptr->string, ptr->length))
1678 fprintf(stderr,
"%s: Cannot run query %.*s ERROR : %s\n",
1679 my_progname, (uint)ptr->length, ptr->string, mysql_error(mysql));
1685 if (auto_generate_sql && (auto_generate_sql_number > count ))
1688 after_create= stmt->next;
1696 drop_schema(
MYSQL *mysql,
const char *db)
1698 char query[HUGE_STRING_LENGTH];
1700 DBUG_ENTER(
"drop_schema");
1701 len= snprintf(query, HUGE_STRING_LENGTH,
"DROP SCHEMA IF EXISTS `%s`", db);
1703 if (run_query(mysql, query, len))
1705 fprintf(stderr,
"%s: Cannot drop database '%s' ERROR : %s\n",
1706 my_progname, db, mysql_error(mysql));
1720 DBUG_ENTER(
"run_statements");
1722 for (ptr= stmt; ptr && ptr->length; ptr= ptr->next)
1724 if (run_query(mysql, ptr->string, ptr->length))
1726 fprintf(stderr,
"%s: Cannot run query %.*s ERROR : %s\n",
1727 my_progname, (uint)ptr->length, ptr->string, mysql_error(mysql));
1730 if (mysql_field_count(mysql))
1732 result= mysql_store_result(mysql);
1733 mysql_free_result(result);
1744 struct timeval start_time, end_time;
1746 pthread_t mainthread;
1747 pthread_attr_t attr;
1748 DBUG_ENTER(
"run_scheduler");
1753 pthread_attr_init(&attr);
1754 pthread_attr_setdetachstate(&attr,
1755 PTHREAD_CREATE_DETACHED);
1757 pthread_mutex_lock(&counter_mutex);
1760 pthread_mutex_lock(&sleeper_mutex);
1762 pthread_mutex_unlock(&sleeper_mutex);
1763 for (x= 0; x < concur; x++)
1766 if (pthread_create(&mainthread, &attr, run_task,
1769 fprintf(stderr,
"%s: Could not create thread\n",
1775 pthread_mutex_unlock(&counter_mutex);
1776 pthread_attr_destroy(&attr);
1778 pthread_mutex_lock(&sleeper_mutex);
1780 pthread_mutex_unlock(&sleeper_mutex);
1781 pthread_cond_broadcast(&sleep_threshhold);
1783 gettimeofday(&start_time, NULL);
1788 pthread_mutex_lock(&counter_mutex);
1789 while (thread_counter)
1793 set_timespec(abstime, 3);
1794 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
1796 pthread_mutex_unlock(&counter_mutex);
1798 gettimeofday(&end_time, NULL);
1801 sptr->timing= timedif(end_time, start_time);
1802 sptr->users= concur;
1809 pthread_handler_t run_task(
void *p)
1811 ulonglong counter= 0, queries;
1812 ulonglong detach_counter;
1813 unsigned int commit_counter;
1820 DBUG_ENTER(
"run_task");
1821 DBUG_PRINT(
"info", (
"task script \"%s\"", con->stmt ? con->stmt->string :
""));
1823 pthread_mutex_lock(&sleeper_mutex);
1824 while (master_wakeup)
1826 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
1828 pthread_mutex_unlock(&sleeper_mutex);
1830 if (!(mysql= mysql_init(NULL)))
1832 fprintf(stderr,
"%s: mysql_init() failed ERROR : %s\n",
1833 my_progname, mysql_error(mysql));
1837 if (mysql_thread_init())
1839 fprintf(stderr,
"%s: mysql_thread_init() failed ERROR : %s\n",
1840 my_progname, mysql_error(mysql));
1844 DBUG_PRINT(
"info", (
"trying to connect to host %s as user %s", host, user));
1846 if (!opt_only_print)
1848 if (slap_connect(mysql))
1852 DBUG_PRINT(
"info", (
"connected."));
1854 printf(
"connected!\n");
1859 run_query(mysql,
"SET AUTOCOMMIT=0", strlen(
"SET AUTOCOMMIT=0"));
1862 for (ptr= con->stmt, detach_counter= 0;
1864 ptr= ptr->next, detach_counter++)
1866 if (!opt_only_print && detach_rate && !(detach_counter % detach_rate))
1870 if (!(mysql= mysql_init(NULL)))
1872 fprintf(stderr,
"%s: mysql_init() failed ERROR : %s\n",
1873 my_progname, mysql_error(mysql));
1877 if (slap_connect(mysql))
1884 if ((ptr->type == UPDATE_TYPE_REQUIRES_PREFIX) ||
1885 (ptr->type == SELECT_TYPE_REQUIRES_PREFIX))
1888 unsigned int key_val;
1890 char buffer[HUGE_STRING_LENGTH];
1899 DBUG_ASSERT(primary_keys_number_of);
1900 if (primary_keys_number_of)
1902 key_val= (
unsigned int)(random() % primary_keys_number_of);
1903 key= primary_keys[key_val];
1907 length= snprintf(buffer, HUGE_STRING_LENGTH,
"%.*s '%s'",
1908 (
int)ptr->length, ptr->string, key);
1910 if (run_query(mysql, buffer, length))
1912 fprintf(stderr,
"%s: Cannot run query %.*s ERROR : %s\n",
1913 my_progname, (uint)length, buffer, mysql_error(mysql));
1920 if (run_query(mysql, ptr->string, ptr->length))
1922 fprintf(stderr,
"%s: Cannot run query %.*s ERROR : %s\n",
1923 my_progname, (uint)ptr->length, ptr->string, mysql_error(mysql));
1930 if (mysql_field_count(mysql))
1932 if (!(result= mysql_store_result(mysql)))
1933 fprintf(stderr,
"%s: Error when storing result: %d %s\n",
1934 my_progname, mysql_errno(mysql), mysql_error(mysql));
1937 while ((row= mysql_fetch_row(result)))
1939 mysql_free_result(result);
1942 }
while(mysql_next_result(mysql) == 0);
1945 if (commit_rate && (++commit_counter == commit_rate))
1948 run_query(mysql,
"COMMIT", strlen(
"COMMIT"));
1951 if (con->limit && queries == con->limit)
1955 if (con->limit && queries < con->limit)
1960 run_query(mysql,
"COMMIT", strlen(
"COMMIT"));
1962 if (!opt_only_print)
1967 pthread_mutex_lock(&counter_mutex);
1969 pthread_cond_signal(&count_threshhold);
1970 pthread_mutex_unlock(&counter_mutex);
1976 parse_option(
const char *origin,
option_string **stmt,
char delm)
1979 char *ptr= (
char *)origin;
1982 size_t length= strlen(origin);
1986 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
1987 (retstr= strchr(ptr, delm));
1989 MYF(MY_ZEROFILL|MY_FAE|MY_WME)),
1992 char buffer[HUGE_STRING_LENGTH];
1996 strncpy(buffer, ptr, (
size_t)(retstr - ptr));
1997 if ((buffer_ptr= strchr(buffer,
':')))
2001 tmp->length= (size_t)(buffer_ptr - buffer);
2002 tmp->string= my_strndup(ptr, (uint)tmp->length, MYF(MY_FAE));
2004 option_ptr= ptr + 1 + tmp->length;
2007 tmp->option_length= (size_t)(retstr - option_ptr);
2008 tmp->option= my_strndup(option_ptr, (uint)tmp->option_length,
2013 tmp->string= my_strndup(ptr, (
size_t)(retstr - ptr), MYF(MY_FAE));
2014 tmp->length= (size_t)(retstr - ptr);
2017 ptr+= retstr - ptr + 1;
2023 if (ptr != origin+length)
2027 if ((origin_ptr= strchr(ptr,
':')))
2031 tmp->length= (size_t)(origin_ptr - ptr);
2032 tmp->string= my_strndup(origin, tmp->length, MYF(MY_FAE));
2034 option_ptr= (
char *)ptr + 1 + tmp->length;
2037 tmp->option_length= (
size_t)((ptr + length) - option_ptr);
2038 tmp->option= my_strndup(option_ptr, tmp->option_length,
2043 tmp->length= (size_t)((ptr + length) - ptr);
2044 tmp->string= my_strndup(ptr, tmp->length, MYF(MY_FAE));
2055 parse_delimiter(
const char *script,
statement **stmt,
char delm)
2058 char *ptr= (
char *)script;
2061 uint length= strlen(script);
2065 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
2066 (retstr= strchr(ptr, delm));
2068 MYF(MY_ZEROFILL|MY_FAE|MY_WME)),
2072 tmp->string= my_strndup(ptr, (uint)(retstr - ptr), MYF(MY_FAE));
2073 tmp->length= (size_t)(retstr - ptr);
2074 ptr+= retstr - ptr + 1;
2079 if (ptr != script+length)
2081 tmp->string= my_strndup(ptr, (uint)((script + length) - ptr),
2083 tmp->length= (size_t)((script + length) - ptr);
2092 parse_comma(
const char *
string, uint **range)
2096 char *ptr= (
char *)
string;
2100 if (*ptr ==
',') count++;
2103 nptr= *range= (uint *)my_malloc(
sizeof(uint) * (count + 1),
2104 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
2106 ptr= (
char *)
string;
2108 while ((retstr= strchr(ptr,
',')))
2110 nptr[x++]= atoi(ptr);
2111 ptr+= retstr - ptr + 1;
2113 nptr[x++]= atoi(ptr);
2121 printf(
"Benchmark\n");
2123 printf(
"\tRunning for engine %s\n", con->engine);
2124 printf(
"\tAverage number of seconds to run all queries: %ld.%03ld seconds\n",
2125 con->avg_timing / 1000, con->avg_timing % 1000);
2126 printf(
"\tMinimum number of seconds to run all queries: %ld.%03ld seconds\n",
2127 con->min_timing / 1000, con->min_timing % 1000);
2128 printf(
"\tMaximum number of seconds to run all queries: %ld.%03ld seconds\n",
2129 con->max_timing / 1000, con->max_timing % 1000);
2130 printf(
"\tNumber of clients running queries: %d\n", con->users);
2131 printf(
"\tAverage number of queries per client: %llu\n", con->avg_rows);
2138 char buffer[HUGE_STRING_LENGTH];
2139 const char *ptr= auto_generate_sql_type ? auto_generate_sql_type :
"query";
2140 snprintf(buffer, HUGE_STRING_LENGTH,
2141 "%s,%s,%ld.%03ld,%ld.%03ld,%ld.%03ld,%d,%llu\n",
2142 con->engine ? con->engine :
"",
2144 con->avg_timing / 1000, con->avg_timing % 1000,
2145 con->min_timing / 1000, con->min_timing % 1000,
2146 con->max_timing / 1000, con->max_timing % 1000,
2150 my_write(csv_file, (uchar*) buffer, (uint)strlen(buffer), MYF(0));
2159 con->min_timing= sptr->timing;
2160 con->max_timing= sptr->timing;
2161 con->min_rows= sptr->rows;
2162 con->max_rows= sptr->rows;
2165 con->users= sptr->users;
2166 con->avg_rows= sptr->rows;
2169 for (ptr= sptr, x= 0; x < iterations; ptr++, x++)
2171 con->avg_timing+= ptr->timing;
2173 if (ptr->timing > con->max_timing)
2174 con->max_timing= ptr->timing;
2175 if (ptr->timing < con->min_timing)
2176 con->min_timing= ptr->timing;
2178 con->avg_timing= con->avg_timing/iterations;
2180 if (eng && eng->string)
2181 con->engine= eng->string;
2193 for (ptr= stmt; ptr; ptr= nptr)
2196 my_free(ptr->string);
2197 my_free(ptr->option);
2209 for (ptr= stmt; ptr; ptr= nptr)
2212 my_free(ptr->string);
2219 slap_connect(
MYSQL *mysql)
2222 static ulong connection_retry_sleep= 100000;
2223 int x, connect_error= 1;
2224 for (x= 0; x < 10; x++)
2226 if (mysql_real_connect(mysql, host, user, opt_password,
2227 create_schema_string,
2229 opt_mysql_unix_port,
2236 my_sleep(connection_retry_sleep);
2240 fprintf(stderr,
"%s: Error when connecting to server: %d %s\n",
2241 my_progname, mysql_errno(mysql), mysql_error(mysql));