forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlslap.cc
2065 lines (1768 loc) · 68.1 KB
/
mysqlslap.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005, 2022, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
MySQL Slap
A simple program designed to work as if multiple clients querying the
database, then reporting the timing of each stage.
MySQL slap runs three stages:
1) Create schema,table, and optionally any SP or data you want to beign
the test with. (single client)
2) Load test (many clients)
3) Cleanup (disconnection, drop table if specified, single client)
Examples:
Supply your own create and query SQL statements, with 50 clients
querying (200 selects for each):
mysqlslap --delimiter=";" \
--create="CREATE TABLE A (a int);INSERT INTO A VALUES (23)" \
--query="SELECT * FROM A" --concurrency=50 --iterations=200
Let the program build the query SQL statement with a table of two int
columns, three varchar columns, five clients querying (20 times each),
don't create the table or insert the data (using the previous test's
schema and data):
mysqlslap --concurrency=5 --iterations=20 \
--number-int-cols=2 --number-char-cols=3 \
--auto-generate-sql
Tell the program to load the create, insert and query SQL statements from
the specified files, where the create.sql file has multiple table creation
statements delimited by ';' and multiple insert statements delimited by ';'.
The --query file will have multiple queries delimited by ';', run all the
load statements, and then run all the queries in the query file
with five clients (five times each):
mysqlslap --concurrency=5 \
--iterations=5 --query=query.sql --create=create.sql \
--delimiter=";"
TODO:
Add language for better tests
String length for files and those put on the command line are not
setup to handle binary data.
More stats
Break up tests and run them on multiple hosts at once.
Allow output to be fed into a database directly.
*/
#define HUGE_STRING_LENGTH 8196
#define RAND_STRING_SIZE 126
/* Types */
#define SELECT_TYPE 0
#define UPDATE_TYPE 1
#define INSERT_TYPE 2
#define UPDATE_TYPE_REQUIRES_PREFIX 3
#define CREATE_TABLE_TYPE 4
#define SELECT_TYPE_REQUIRES_PREFIX 5
#define DELETE_TYPE_REQUIRES_PREFIX 6
#include "my_config.h"
#include <ctype.h>
#include <fcntl.h>
#include <mysqld_error.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include "caching_sha2_passwordopt-vars.h"
#include "my_dir.h"
#include "sslopt-vars.h"
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <stdio.h>
#include <time.h>
#include "client/client_priv.h"
#include "compression.h"
#include "my_alloc.h"
#include "my_dbug.h"
#include "my_default.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_systime.h"
#include "mysql/service_mysql_alloc.h"
#include "print_version.h"
#include "thr_cond.h"
#include "typelib.h"
#include "welcome_copyright_notice.h" /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
#ifdef _WIN32
#define srandom srand
#define random rand
#endif
#if defined(_WIN32)
static char *shared_memory_base_name = 0;
#endif
/* Global Thread counter */
uint thread_counter;
native_mutex_t counter_mutex;
native_cond_t count_threshold;
uint master_wakeup;
native_mutex_t sleeper_mutex;
native_cond_t sleep_threshold;
char **primary_keys;
unsigned long long primary_keys_number_of;
static char *host = nullptr, *user_supplied_query = nullptr,
*user_supplied_pre_statements = nullptr,
*user_supplied_post_statements = nullptr, *default_engine = nullptr,
*pre_system = nullptr, *post_system = nullptr,
*opt_mysql_unix_port = nullptr;
static const char *user = nullptr;
static char *opt_plugin_dir = nullptr, *opt_default_auth = nullptr;
static uint opt_enable_cleartext_plugin = 0;
static bool using_opt_enable_cleartext_plugin = false;
const char *delimiter = "\n";
const char *create_schema_string = "mysqlslap";
static bool opt_preserve = true, opt_no_drop = false;
static bool debug_info_flag = false, debug_check_flag = false;
static bool opt_only_print = false;
static bool opt_compress = false, opt_silent = false,
auto_generate_sql_autoincrement = false,
auto_generate_sql_guid_primary = false, auto_generate_sql = false;
const char *auto_generate_sql_type = "mixed";
static uint opt_zstd_compress_level = default_zstd_compression_level;
static char *opt_compress_algorithm = nullptr;
static unsigned long connect_flags =
CLIENT_MULTI_RESULTS | CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS;
static int verbose;
static uint commit_rate;
static uint detach_rate;
const char *num_int_cols_opt;
const char *num_char_cols_opt;
/* Yes, we do set defaults here */
static unsigned int num_int_cols = 1;
static unsigned int num_char_cols = 1;
static unsigned int num_int_cols_index = 0;
static unsigned int num_char_cols_index = 0;
static unsigned int iterations;
static uint my_end_arg = 0;
static const char *default_charset = MYSQL_DEFAULT_CHARSET_NAME;
static ulonglong actual_queries = 0;
static ulonglong auto_actual_queries;
static ulonglong auto_generate_sql_unique_write_number;
static ulonglong auto_generate_sql_unique_query_number;
static unsigned int auto_generate_sql_secondary_indexes;
static ulonglong num_of_query;
static ulonglong auto_generate_sql_number;
static const char *sql_mode = nullptr;
const char *concurrency_str = nullptr;
static char *create_string;
uint *concurrency;
const char *default_dbug_option = "d:t:o,/tmp/mysqlslap.trace";
const char *opt_csv_str;
File csv_file;
static uint opt_protocol = 0;
#include "multi_factor_passwordopt-vars.h"
static int get_options(int *argc, char ***argv);
static uint opt_mysql_port = 0;
static const char *load_default_groups[] = {"mysqlslap", "client", nullptr};
typedef struct statement statement;
struct statement {
char *string;
size_t length;
unsigned char type;
char *option;
size_t option_length;
statement *next;
};
typedef struct option_string option_string;
struct option_string {
char *string;
size_t length;
char *option;
size_t option_length;
option_string *next;
};
typedef struct stats stats;
struct stats {
long int timing;
uint users;
unsigned long long rows;
};
typedef struct thread_context thread_context;
struct thread_context {
statement *stmt;
ulonglong limit;
};
typedef struct conclusions conclusions;
struct conclusions {
char *engine;
long int avg_timing;
long int max_timing;
long int min_timing;
uint users;
unsigned long long avg_rows;
/* The following are not used yet */
unsigned long long max_rows;
unsigned long long min_rows;
};
static option_string *engine_options = nullptr;
static statement *pre_statements = nullptr;
static statement *post_statements = nullptr;
static statement *create_statements = nullptr, *query_statements = nullptr;
/* Prototypes */
void print_conclusions(conclusions *con);
void print_conclusions_csv(conclusions *con);
void generate_stats(conclusions *con, option_string *eng, stats *sptr);
uint parse_comma(const char *string, uint **range);
uint parse_delimiter(const char *script, statement **stmt, char delm);
int parse_option(const char *origin, option_string **stmt, char delm);
static int drop_schema(MYSQL *mysql, const char *db);
size_t get_random_string(char *buf);
static statement *build_table_string(void);
static statement *build_insert_string(void);
static statement *build_update_string(void);
static statement *build_select_string(bool key);
static int generate_primary_key_list(MYSQL *mysql, option_string *engine_stmt);
static int drop_primary_key_list(void);
static int create_schema(MYSQL *mysql, const char *db, statement *stmt,
option_string *engine_stmt);
static void set_sql_mode(MYSQL *mysql);
static int run_scheduler(stats *sptr, statement *stmts, uint concur,
ulonglong limit);
extern "C" void *run_task(void *p);
void statement_cleanup(statement *stmt);
void option_cleanup(option_string *stmt);
void concurrency_loop(MYSQL *mysql, uint current, option_string *eptr);
static int run_statements(MYSQL *mysql, statement *stmt);
int slap_connect(MYSQL *mysql);
static int run_query(MYSQL *mysql, const char *query, size_t len);
static const char ALPHANUMERICS[] =
"0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS) - 1)
static long int timedif(struct timeval a, struct timeval b) {
int us, s;
us = a.tv_usec - b.tv_usec;
us /= 1000;
s = a.tv_sec - b.tv_sec;
s *= 1000;
return s + us;
}
#ifdef _WIN32
static int gettimeofday(struct timeval *tp, void *tzp) {
unsigned int ticks;
ticks = GetTickCount();
tp->tv_usec = ticks * 1000;
tp->tv_sec = ticks / 1000;
return 0;
}
#endif
int main(int argc, char **argv) {
MYSQL mysql{};
option_string *eptr;
MY_INIT(argv[0]);
my_getopt_use_args_separator = true;
MEM_ROOT alloc{PSI_NOT_INSTRUMENTED, 512};
if (load_defaults("my", load_default_groups, &argc, &argv, &alloc)) {
my_end(0);
return EXIT_FAILURE;
}
my_getopt_use_args_separator = false;
if (get_options(&argc, &argv)) {
my_end(0);
return EXIT_FAILURE;
}
/* Seed the random number generator if we will be using it. */
if (auto_generate_sql) srandom((uint)time(nullptr));
if (argc > 2) {
fprintf(stderr, "%s: Too many arguments\n", my_progname);
my_end(0);
return EXIT_FAILURE;
}
mysql_init(&mysql);
if (opt_compress) mysql_options(&mysql, MYSQL_OPT_COMPRESS, NullS);
if (opt_compress_algorithm)
mysql_options(&mysql, MYSQL_OPT_COMPRESSION_ALGORITHMS,
opt_compress_algorithm);
mysql_options(&mysql, MYSQL_OPT_ZSTD_COMPRESSION_LEVEL,
&opt_zstd_compress_level);
if (SSL_SET_OPTIONS(&mysql)) {
fprintf(stderr, "%s", SSL_SET_OPTIONS_ERROR);
return EXIT_FAILURE;
}
if (opt_protocol)
mysql_options(&mysql, MYSQL_OPT_PROTOCOL, (char *)&opt_protocol);
#if defined(_WIN32)
if (shared_memory_base_name)
mysql_options(&mysql, MYSQL_SHARED_MEMORY_BASE_NAME,
shared_memory_base_name);
#endif
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);
if (opt_plugin_dir && *opt_plugin_dir)
mysql_options(&mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
if (opt_default_auth && *opt_default_auth)
mysql_options(&mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
mysql_options(&mysql, MYSQL_OPT_CONNECT_ATTR_RESET, nullptr);
mysql_options4(&mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "program_name",
"mysqlslap");
if (using_opt_enable_cleartext_plugin)
mysql_options(&mysql, MYSQL_ENABLE_CLEARTEXT_PLUGIN,
(char *)&opt_enable_cleartext_plugin);
set_server_public_key(&mysql);
set_get_server_public_key_option(&mysql);
set_password_options(&mysql);
if (!opt_only_print) {
if (!(mysql_real_connect(&mysql, host, user, nullptr, nullptr,
opt_mysql_port, opt_mysql_unix_port,
connect_flags))) {
fprintf(stderr, "%s: Error when connecting to server: %s\n", my_progname,
mysql_error(&mysql));
mysql_close(&mysql);
my_end(0);
return EXIT_FAILURE;
}
if (ssl_client_check_post_connect_ssl_setup(
&mysql, [](const char *err) { fprintf(stderr, "%s\n", err); })) {
mysql_close(&mysql);
my_end(0);
return EXIT_FAILURE;
}
}
set_sql_mode(&mysql);
native_mutex_init(&counter_mutex, nullptr);
native_cond_init(&count_threshold);
native_mutex_init(&sleeper_mutex, nullptr);
native_cond_init(&sleep_threshold);
/* Main iterations loop */
eptr = engine_options;
do {
/* For the final stage we run whatever queries we were asked to run */
uint *current;
if (verbose >= 2) printf("Starting Concurrency Test\n");
if (*concurrency) {
for (current = concurrency; current && *current; current++)
concurrency_loop(&mysql, *current, eptr);
} else {
uint infinite = 1;
do {
concurrency_loop(&mysql, infinite, eptr);
} while (infinite++);
}
if (!opt_preserve) drop_schema(&mysql, create_schema_string);
} while (eptr ? (eptr = eptr->next) : nullptr);
native_mutex_destroy(&counter_mutex);
native_cond_destroy(&count_threshold);
native_mutex_destroy(&sleeper_mutex);
native_cond_destroy(&sleep_threshold);
mysql_close(&mysql); /* Close & free connection */
/* now free all the strings we created */
free_passwords();
my_free(concurrency);
statement_cleanup(create_statements);
statement_cleanup(query_statements);
statement_cleanup(pre_statements);
statement_cleanup(post_statements);
option_cleanup(engine_options);
#if defined(_WIN32)
my_free(shared_memory_base_name);
#endif
mysql_server_end();
my_end(my_end_arg);
return EXIT_SUCCESS;
}
void concurrency_loop(MYSQL *mysql, uint current, option_string *eptr) {
unsigned int x;
stats *head_sptr;
stats *sptr;
conclusions conclusion;
unsigned long long client_limit;
int sysret;
head_sptr =
(stats *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(stats) * iterations,
MYF(MY_ZEROFILL | MY_FAE | MY_WME));
memset(&conclusion, 0, sizeof(conclusions));
if (auto_actual_queries)
client_limit = auto_actual_queries;
else if (num_of_query)
client_limit = num_of_query / current;
else
client_limit = actual_queries;
for (x = 0, sptr = head_sptr; x < iterations; x++, sptr++) {
/*
We might not want to load any data, such as when we are calling
a stored_procedure that doesn't use data, or we know we already have
data in the table.
*/
if (!opt_preserve) drop_schema(mysql, create_schema_string);
/* First we create */
if (create_statements)
create_schema(mysql, create_schema_string, create_statements, eptr);
/*
If we generated GUID we need to build a list of them from creation that
we can later use.
*/
if (verbose >= 2) printf("Generating primary key list\n");
if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
generate_primary_key_list(mysql, eptr);
if (commit_rate)
run_query(mysql, "SET AUTOCOMMIT=0", strlen("SET AUTOCOMMIT=0"));
if (pre_system)
if ((sysret = system(pre_system)) != 0)
fprintf(stderr,
"Warning: Execution of pre_system option returned %d.\n",
sysret);
/*
Pre statements are always run after all other logic so they can
correct/adjust any item that they want.
*/
if (pre_statements) run_statements(mysql, pre_statements);
run_scheduler(sptr, query_statements, current, client_limit);
if (post_statements) run_statements(mysql, post_statements);
if (post_system)
if ((sysret = system(post_system)) != 0)
fprintf(stderr,
"Warning: Execution of post_system option returned %d.\n",
sysret);
/* We are finished with this run */
if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
drop_primary_key_list();
}
if (verbose >= 2) printf("Generating stats\n");
generate_stats(&conclusion, eptr, head_sptr);
if (!opt_silent) print_conclusions(&conclusion);
if (opt_csv_str) print_conclusions_csv(&conclusion);
my_free(head_sptr);
}
static struct my_option my_long_options[] = {
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql", 'a',
"Generate SQL where not supplied by file or command line.",
&auto_generate_sql, &auto_generate_sql, nullptr, GET_BOOL, NO_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"auto-generate-sql-add-autoincrement", OPT_SLAP_AUTO_GENERATE_ADD_AUTO,
"Add an AUTO_INCREMENT column to auto-generated tables.",
&auto_generate_sql_autoincrement, &auto_generate_sql_autoincrement,
nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-execute-number", OPT_SLAP_AUTO_GENERATE_EXECUTE_QUERIES,
"Set this number to generate a set number of queries to run.",
&auto_actual_queries, &auto_actual_queries, nullptr, GET_ULL, REQUIRED_ARG,
0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-guid-primary", OPT_SLAP_AUTO_GENERATE_GUID_PRIMARY,
"Add GUID based primary keys to auto-generated tables.",
&auto_generate_sql_guid_primary, &auto_generate_sql_guid_primary, nullptr,
GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-load-type", OPT_SLAP_AUTO_GENERATE_SQL_LOAD_TYPE,
"Specify test load type: mixed, update, write, key, or read; default is "
"mixed.",
&auto_generate_sql_type, &auto_generate_sql_type, nullptr, GET_STR,
REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-secondary-indexes",
OPT_SLAP_AUTO_GENERATE_SECONDARY_INDEXES,
"Number of secondary indexes to add to auto-generated tables.",
&auto_generate_sql_secondary_indexes, &auto_generate_sql_secondary_indexes,
nullptr, GET_UINT, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-unique-query-number",
OPT_SLAP_AUTO_GENERATE_UNIQUE_QUERY_NUM,
"Number of unique queries to generate for automatic tests.",
&auto_generate_sql_unique_query_number,
&auto_generate_sql_unique_query_number, nullptr, GET_ULL, REQUIRED_ARG, 10,
0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-unique-write-number",
OPT_SLAP_AUTO_GENERATE_UNIQUE_WRITE_NUM,
"Number of unique queries to generate for auto-generate-sql-write-number.",
&auto_generate_sql_unique_write_number,
&auto_generate_sql_unique_write_number, nullptr, GET_ULL, REQUIRED_ARG, 10,
0, 0, nullptr, 0, nullptr},
{"auto-generate-sql-write-number", OPT_SLAP_AUTO_GENERATE_WRITE_NUM,
"Number of row inserts to perform for each thread (default is 100).",
&auto_generate_sql_number, &auto_generate_sql_number, nullptr, GET_ULL,
REQUIRED_ARG, 100, 0, 0, nullptr, 0, nullptr},
{"commit", OPT_SLAP_COMMIT, "Commit records every X number of statements.",
&commit_rate, &commit_rate, nullptr, GET_UINT, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"compress", 'C', "Use compression in server/client protocol.",
&opt_compress, &opt_compress, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr,
0, nullptr},
{"concurrency", 'c', "Number of clients to simulate for query to run.",
&concurrency_str, &concurrency_str, nullptr, GET_STR, REQUIRED_ARG, 0, 0,
0, nullptr, 0, nullptr},
{"create", OPT_SLAP_CREATE_STRING, "File or string to use create tables.",
&create_string, &create_string, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"create-schema", OPT_CREATE_SLAP_SCHEMA, "Schema to run tests in.",
&create_schema_string, &create_schema_string, nullptr, GET_STR,
REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"csv", OPT_SLAP_CSV,
"Generate CSV output to named file or to stdout if no file is named.",
nullptr, nullptr, nullptr, GET_STR, OPT_ARG, 0, 0, 0, nullptr, 0, nullptr},
#ifdef NDEBUG
{"debug", '#', "This is a non-debug version. Catch this and exit.", nullptr,
nullptr, nullptr, GET_DISABLED, OPT_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"debug-check", OPT_DEBUG_CHECK,
"This is a non-debug version. Catch this and exit.", nullptr, nullptr,
nullptr, GET_DISABLED, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"debug-info", 'T', "This is a non-debug version. Catch this and exit.",
nullptr, nullptr, nullptr, GET_DISABLED, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
#else
{"debug", '#', "Output debug log. Often this is 'd:t:o,filename'.",
&default_dbug_option, &default_dbug_option, nullptr, GET_STR, OPT_ARG, 0,
0, 0, nullptr, 0, nullptr},
{"debug-check", OPT_DEBUG_CHECK,
"Check memory and open file usage at exit.", &debug_check_flag,
&debug_check_flag, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"debug-info", 'T', "Print some debug info at exit.", &debug_info_flag,
&debug_info_flag, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
#endif
{"default_auth", OPT_DEFAULT_AUTH,
"Default authentication client-side plugin to use.", &opt_default_auth,
&opt_default_auth, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"delimiter", 'F',
"Delimiter to use in SQL statements supplied in file or command line.",
&delimiter, &delimiter, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr,
0, nullptr},
{"detach", OPT_SLAP_DETACH,
"Detach (close and reopen) connections after X number of requests.",
&detach_rate, &detach_rate, nullptr, GET_UINT, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"enable_cleartext_plugin", OPT_ENABLE_CLEARTEXT_PLUGIN,
"Enable/disable the clear text authentication plugin.",
&opt_enable_cleartext_plugin, &opt_enable_cleartext_plugin, nullptr,
GET_BOOL, OPT_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"engine", 'e', "Storage engine to use for creating the table.",
&default_engine, &default_engine, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"host", 'h', "Connect to host.", &host, &host, nullptr, GET_STR,
REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"iterations", 'i', "Number of times to run the tests.", &iterations,
&iterations, nullptr, GET_UINT, REQUIRED_ARG, 1, 1, UINT_MAX, nullptr, 0,
nullptr},
{"no-drop", OPT_SLAP_NO_DROP, "Do not drop the schema after the test.",
&opt_no_drop, &opt_no_drop, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"number-char-cols", 'x',
"Number of VARCHAR columns to create in table if specifying "
"--auto-generate-sql.",
&num_char_cols_opt, &num_char_cols_opt, nullptr, GET_STR, REQUIRED_ARG, 0,
0, 0, nullptr, 0, nullptr},
{"number-int-cols", 'y',
"Number of INT columns to create in table if specifying "
"--auto-generate-sql.",
&num_int_cols_opt, &num_int_cols_opt, nullptr, GET_STR, REQUIRED_ARG, 0, 0,
0, nullptr, 0, nullptr},
{"number-of-queries", OPT_MYSQL_NUMBER_OF_QUERY,
"Limit each client to this number of queries (this is not exact).",
&num_of_query, &num_of_query, nullptr, GET_ULL, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"only-print", OPT_MYSQL_ONLY_PRINT,
"Do not connect to the databases, but instead print out what would have "
"been done.",
&opt_only_print, &opt_only_print, nullptr, GET_BOOL, NO_ARG, 0, 0, 0,
nullptr, 0, nullptr},
#include "multi_factor_passwordopt-longopts.h"
#ifdef _WIN32
{"pipe", 'W', "Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
{"plugin_dir", OPT_PLUGIN_DIR, "Directory for client-side plugins.",
&opt_plugin_dir, &opt_plugin_dir, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"port", 'P', "Port number to use for connection.", &opt_mysql_port,
&opt_mysql_port, nullptr, GET_UINT, REQUIRED_ARG, MYSQL_PORT, 0, 0,
nullptr, 0, nullptr},
{"post-query", OPT_SLAP_POST_QUERY,
"Query to run or file containing query to execute after tests have "
"completed.",
&user_supplied_post_statements, &user_supplied_post_statements, nullptr,
GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"post-system", OPT_SLAP_POST_SYSTEM,
"system() string to execute after tests have completed.", &post_system,
&post_system, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"pre-query", OPT_SLAP_PRE_QUERY,
"Query to run or file containing query to execute before running tests.",
&user_supplied_pre_statements, &user_supplied_pre_statements, nullptr,
GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"pre-system", OPT_SLAP_PRE_SYSTEM,
"system() string to execute before running tests.", &pre_system,
&pre_system, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"protocol", OPT_MYSQL_PROTOCOL,
"The protocol to use for connection (tcp, socket, pipe, memory).", nullptr,
nullptr, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"query", 'q', "Query to run or file containing query to run.",
&user_supplied_query, &user_supplied_query, nullptr, GET_STR, REQUIRED_ARG,
0, 0, 0, nullptr, 0, nullptr},
#if defined(_WIN32)
{"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
"Base name of shared memory.", &shared_memory_base_name,
&shared_memory_base_name, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0,
0},
#endif
{"silent", 's', "Run program in silent mode - no output.", &opt_silent,
&opt_silent, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"socket", 'S', "The socket file to use for connection.",
&opt_mysql_unix_port, &opt_mysql_unix_port, nullptr, GET_STR, REQUIRED_ARG,
0, 0, 0, nullptr, 0, nullptr},
{"sql_mode", 0, "Specify sql-mode to run mysqlslap tool.", &sql_mode,
&sql_mode, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
#include "caching_sha2_passwordopt-longopts.h"
#include "sslopt-longopts.h"
{"user", 'u', "User for login if not current user.", &user, &user, nullptr,
GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"verbose", 'v',
"More verbose output; you can use this multiple times to get even more "
"verbose output.",
&verbose, &verbose, nullptr, GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"version", 'V', "Output version information and exit.", nullptr, nullptr,
nullptr, GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"compression-algorithms", 0,
"Use compression algorithm in server/client protocol. Valid values "
"are any combination of 'zstd','zlib','uncompressed'.",
&opt_compress_algorithm, &opt_compress_algorithm, nullptr, GET_STR,
REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"zstd-compression-level", 0,
"Use this compression level in the client/server protocol, in case "
"--compression-algorithms=zstd. Valid range is between 1 and 22, "
"inclusive. Default is 3.",
&opt_zstd_compress_level, &opt_zstd_compress_level, nullptr, GET_UINT,
REQUIRED_ARG, 3, 1, 22, nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
static void usage(void) {
print_version();
puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2005"));
puts("Run a query multiple times against the server.\n");
printf("Usage: %s [OPTIONS]\n", my_progname);
print_defaults("my", load_default_groups);
my_print_help(my_long_options);
}
extern "C" {
static bool get_one_option(int optid, const struct my_option *opt,
char *argument) {
DBUG_TRACE;
switch (optid) {
case 'v':
verbose++;
break;
PARSE_COMMAND_LINE_PASSWORD_OPTION;
case 'W':
#ifdef _WIN32
opt_protocol = MYSQL_PROTOCOL_PIPE;
#endif
break;
case OPT_MYSQL_PROTOCOL:
opt_protocol =
find_type_or_exit(argument, &sql_protocol_typelib, opt->name);
break;
case '#':
DBUG_PUSH(argument ? argument : default_dbug_option);
debug_check_flag = true;
break;
case OPT_SLAP_CSV:
if (!argument) argument = const_cast<char *>("-"); /* use stdout */
opt_csv_str = argument;
break;
#include "sslopt-case.h"
case 'V':
print_version();
exit(0);
break;
case '?':
case 'I': /* Info */
usage();
exit(0);
case OPT_ENABLE_CLEARTEXT_PLUGIN:
using_opt_enable_cleartext_plugin = true;
break;
}
return false;
}
}
size_t get_random_string(char *buf) {
char *buf_ptr = buf;
int x;
DBUG_TRACE;
for (x = RAND_STRING_SIZE; x > 0; x--)
*buf_ptr++ = ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
return buf_ptr - buf;
}
/*
build_table_string
This function builds a create table query if the user opts to not supply
a file or string containing a create table statement
*/
static statement *build_table_string(void) {
char buf[HUGE_STRING_LENGTH];
unsigned int col_count;
statement *ptr;
DYNAMIC_STRING table_string;
DBUG_TRACE;
DBUG_PRINT("info",
("num int cols %u num char cols %u", num_int_cols, num_char_cols));
init_dynamic_string(&table_string, "", 1024);
dynstr_append(&table_string, "CREATE TABLE `t1` (");
if (auto_generate_sql_autoincrement) {
dynstr_append(&table_string, "id serial");
if (num_int_cols || num_char_cols) dynstr_append(&table_string, ",");
}
if (auto_generate_sql_guid_primary) {
dynstr_append(&table_string, "id varchar(36) primary key");
if (num_int_cols || num_char_cols || auto_generate_sql_guid_primary)
dynstr_append(&table_string, ",");
}
if (auto_generate_sql_secondary_indexes) {
unsigned int count;
for (count = 0; count < auto_generate_sql_secondary_indexes; count++) {
if (count) /* Except for the first pass we add a comma */
dynstr_append(&table_string, ",");
if (snprintf(buf, HUGE_STRING_LENGTH, "id%d varchar(36) unique key",
count) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in create table\n");
exit(1);
}
dynstr_append(&table_string, buf);
}
if (num_int_cols || num_char_cols) dynstr_append(&table_string, ",");
}
if (num_int_cols)
for (col_count = 1; col_count <= num_int_cols; col_count++) {
if (num_int_cols_index) {
if (snprintf(buf, HUGE_STRING_LENGTH,
"intcol%d INT(32), INDEX(intcol%d)", col_count,
col_count) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in create table\n");
exit(1);
}
} else {
if (snprintf(buf, HUGE_STRING_LENGTH, "intcol%d INT(32) ", col_count) >
HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in create table\n");
exit(1);
}
}
dynstr_append(&table_string, buf);
if (col_count < num_int_cols || num_char_cols > 0)
dynstr_append(&table_string, ",");
}
if (num_char_cols)
for (col_count = 1; col_count <= num_char_cols; col_count++) {
if (num_char_cols_index) {
if (snprintf(buf, HUGE_STRING_LENGTH,
"charcol%d VARCHAR(128), INDEX(charcol%d) ", col_count,
col_count) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in creating table\n");
exit(1);
}
} else {
if (snprintf(buf, HUGE_STRING_LENGTH, "charcol%d VARCHAR(128)",
col_count) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in creating table\n");
exit(1);
}
}
dynstr_append(&table_string, buf);
if (col_count < num_char_cols) dynstr_append(&table_string, ",");
}
dynstr_append(&table_string, ")");
ptr = (statement *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(statement),
MYF(MY_ZEROFILL | MY_FAE | MY_WME));
ptr->string = (char *)my_malloc(PSI_NOT_INSTRUMENTED, table_string.length + 1,
MYF(MY_ZEROFILL | MY_FAE | MY_WME));
ptr->length = table_string.length + 1;
ptr->type = CREATE_TABLE_TYPE;
my_stpcpy(ptr->string, table_string.str);
dynstr_free(&table_string);
return ptr;
}
/*
build_update_string()
This function builds insert statements when the user opts to not supply
an insert file or string containing insert data
*/
static statement *build_update_string(void) {
char buf[HUGE_STRING_LENGTH];
unsigned int col_count;
statement *ptr;
DYNAMIC_STRING update_string;
DBUG_TRACE;
init_dynamic_string(&update_string, "", 1024);
dynstr_append(&update_string, "UPDATE t1 SET ");
if (num_int_cols)
for (col_count = 1; col_count <= num_int_cols; col_count++) {
if (snprintf(buf, HUGE_STRING_LENGTH, "intcol%d = %ld", col_count,
random()) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in creating update\n");
exit(1);
}
dynstr_append(&update_string, buf);
if (col_count < num_int_cols || num_char_cols > 0)
dynstr_append_mem(&update_string, ",", 1);
}
if (num_char_cols)
for (col_count = 1; col_count <= num_char_cols; col_count++) {
char rand_buffer[RAND_STRING_SIZE];
size_t buf_len = get_random_string(rand_buffer);
if (snprintf(buf, HUGE_STRING_LENGTH, "charcol%d = '%.*s'", col_count,
(int)buf_len, rand_buffer) > HUGE_STRING_LENGTH) {
fprintf(stderr, "Memory Allocation error in creating update\n");
exit(1);
}
dynstr_append(&update_string, buf);
if (col_count < num_char_cols) dynstr_append_mem(&update_string, ",", 1);
}
if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
dynstr_append(&update_string, " WHERE id = ");
ptr = (statement *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(statement),
MYF(MY_ZEROFILL | MY_FAE | MY_WME));
ptr->string =
(char *)my_malloc(PSI_NOT_INSTRUMENTED, update_string.length + 1,
MYF(MY_ZEROFILL | MY_FAE | MY_WME));
ptr->length = update_string.length + 1;
if (auto_generate_sql_autoincrement || auto_generate_sql_guid_primary)
ptr->type = UPDATE_TYPE_REQUIRES_PREFIX;
else
ptr->type = UPDATE_TYPE;
my_stpcpy(ptr->string, update_string.str);
dynstr_free(&update_string);
return ptr;
}
/*
build_insert_string()
This function builds insert statements when the user opts to not supply
an insert file or string containing insert data
*/
static statement *build_insert_string(void) {
char buf[HUGE_STRING_LENGTH];
unsigned int col_count;
statement *ptr;
DYNAMIC_STRING insert_string;
DBUG_TRACE;
init_dynamic_string(&insert_string, "", 1024);
dynstr_append(&insert_string, "INSERT INTO t1 VALUES (");
if (auto_generate_sql_autoincrement) {
dynstr_append(&insert_string, "NULL");
if (num_int_cols || num_char_cols) dynstr_append(&insert_string, ",");
}
if (auto_generate_sql_guid_primary) {
dynstr_append(&insert_string, "uuid()");
if (num_int_cols || num_char_cols) dynstr_append(&insert_string, ",");
}
if (auto_generate_sql_secondary_indexes) {
unsigned int count;