📄 mysql-stress-test.pl
字号:
#!/usr/bin/perl# ======================================================================# MySQL server stress test system # ======================================================================############################################################################# SCENARIOS AND REQUIREMENTS## The system should perform stress testing of MySQL server with # following requirements and basic scenarios:# # Basic requirements:# # Design of stress script should allow one:# # - To stress test the mysqltest binary test engine.# - To stress test the regular test suite and any additional test suites# (such as mysql-test-extra-5.0).# - To specify files with lists of tests both for initialization of# stress db and for further testing itself.# - To define the number of threads to be concurrently used in testing.# - To define limitations for the test run. such as the number of tests or# loops for execution or duration of testing, delay between test# executions, and so forth.# - To get a readable log file that can be used for identification of# errors that occur during testing.# # Basic scenarios:# # * It should be possible to run stress script in standalone mode# which will allow to create various scenarios of stress workloads:# # simple ones:## box #1:# - one instance of script with list of tests #1# # and more advanced ones:# # box #1:# - one instance of script with list of tests #1# - another instance of script with list of tests #2# box #2:# - one instance of script with list of tests #3# - another instance of script with list of tests #4# that will recreate whole database to back it to clean# state# # One kind of such complex scenarios maybe continued testing# when we want to run stress tests from many boxes with various# lists of tests that will last very long time. And in such case# we need some wrapper for MySQL server that will restart it in# case of crashes.# # * It should be possible to run stress script in ad-hoc mode from# shell or perl versions of mysql-test-run. This allows developers# to reproduce and debug errors that was found in continued stress # testing#########################################################################use Config;if (!defined($Config{useithreads})){ die <<EOF;It is unable to run threaded version of stress test on this system due to disabled ithreads. Please check that installed perl binary was built with support of ithreads. EOF}use threads;use threads::shared;use IO::Socket;use Sys::Hostname;use File::Copy;use File::Spec;use File::Find;use File::Basename;use File::Path;use Cwd;use Data::Dumper;use Getopt::Long;my $stress_suite_version="1.0";$|=1;$opt_server_host="";$opt_server_logs_dir="";$opt_help="";$opt_server_port="";$opt_server_socket="";$opt_server_user="";$opt_server_password="";$opt_server_database="";$opt_cleanup="";$opt_verbose="";$opt_log_error_details="";$opt_suite="main";$opt_stress_suite_basedir="";$opt_stress_basedir="";$opt_stress_datadir="";$opt_test_suffix="";$opt_stress_mode="random";$opt_loop_count=0;$opt_test_count=0;$opt_test_duration=0;$opt_abort_on_error=0;$opt_sleep_time = 0;$opt_threads=1;$pid_file="mysql_stress_test.pid";$opt_mysqltest= ($^O =~ /mswin32/i) ? "mysqltest.exe" : "mysqltest";$opt_check_tests_file="";@mysqltest_args=("--silent", "-v", "--skip-safemalloc");# Client ip address$client_ip=inet_ntoa((gethostbyname(hostname()))[4]);$client_ip=~ s/\.//g;%tests_files=(client => {mtime => 0, data => []}, initdb => {mtime => 0, data => []});# Error codes and sub-strings with corresponding severity ## S1 - Critical errors - cause immediately abort of testing. These errors# could be caused by server crash or impossibility# of test execution## S2 - Serious errors - these errors are bugs for sure as it knowns that # they shouldn't appear during stress testing ## S3 - Non-seriuos errros - these errors could be caused by fact that # we execute simultaneously statements that# affect tests executed by other threads %error_strings = ( 'Failed in mysql_real_connect()' => S1, 'not found (Errcode: 2)' => S1 ); %error_codes = ( 1012 => S2, 1015 => S2, 1021 => S2, 1027 => S2, 1037 => S2, 1038 => S2, 1039 => S2, 1040 => S2, 1046 => S2, 1180 => S2, 1181 => S2, 1203 => S2, 1205 => S2, 1206 => S2, 1207 => S2, 1223 => S2, 2013 => S1);share(%test_counters);%test_counters=( loop_count => 0, test_count=>0);share($exiting);$exiting=0;share($test_counters_lock);$test_counters_lock=0;share($log_file_lock);$log_file_lock=0;$SIG{INT}= \&sig_INT_handler;$SIG{TERM}= \&sig_TERM_handler;GetOptions("server-host=s", "server-logs-dir=s", "server-port=s", "server-socket=s", "server-user=s", "server-password=s", "server-database=s", "stress-suite-basedir=s", "suite=s", "stress-init-file:s", "stress-tests-file:s", "stress-basedir=s", "stress-mode=s", "stress-datadir=s", "threads=s", "sleep-time=s", "loop-count=i", "test-count=i", "test-duration=i", "test-suffix=s", "check-tests-file", "verbose", "log-error-details", "cleanup", "mysqltest=s", "abort-on-error", "help") || usage();usage() if ($opt_help);#$opt_abort_on_error=1;$test_dirname=get_timestamp();$test_dirname.="-$opt_test_suffix" if ($opt_test_suffix ne '');print <<EOF;############################################################# CONFIGURATION STAGE#############################################################EOFif ($opt_stress_basedir eq '' || $opt_stress_suite_basedir eq '' || $opt_server_logs_dir eq ''){ die <<EOF;Options --stress-basedir, --stress-suite-basedir and --server-logs-dir are required. Please use these options to specify proper basedir for client, test suite and location of server logs.stress-basedir: '$opt_stress_basedir'stress-suite-basedir: '$opt_stress_suite_basedir'server-logs-dir: '$opt_server_logs_dir'EOF}#Workaround for case when we got relative but not absolute path $opt_stress_basedir=File::Spec->rel2abs($opt_stress_basedir);$opt_stress_suite_basedir=File::Spec->rel2abs($opt_stress_suite_basedir);$opt_server_logs_dir=File::Spec->rel2abs($opt_server_logs_dir);if ($opt_stress_datadir ne ''){ $opt_stress_datadir=File::Spec->rel2abs($opt_stress_datadir);}if (! -d "$opt_stress_basedir"){ die <<EOF; Directory '$opt_stress_basedir' does not exist.Use --stress-basedir option to specify proper basedir for clientEOF}if (!-d $opt_stress_suite_basedir){ die <<EOF;Directory '$opt_stress_suite_basedir' does not exist.Use --stress-suite-basedir option to specify proper basedir for test suiteEOF}$test_dataset_dir=$opt_stress_suite_basedir;if ($opt_stress_datadir ne ''){ if (-d $opt_stress_datadir) { $test_dataset_dir=$opt_stress_datadir; } else { die <<EOF;Directory '$opt_stress_datadir' not exists. Please specify proper one with --stress-datadir option.EOF } }if ($^O =~ /mswin32/i){ $test_dataset_dir=~ s/\\/\\\\/g;}else{ $test_dataset_dir.="/";}if (!-d $opt_server_logs_dir){ die <<EOF;Directory server-logs-dir '$opt_server_logs_dir' does not exist.Use --server-logs-dir option to specify proper directory for storing logs EOF}else{ #Create sub-directory for test session logs mkpath(File::Spec->catdir($opt_server_logs_dir, $test_dirname), 0, 0755); #Define filename of global session log file $stress_log_file=File::Spec->catfile($opt_server_logs_dir, $test_dirname, "mysql-stress-test.log");}if ($opt_suite ne '' && $opt_suite ne 'main' && $opt_suite ne 'default'){ $test_suite_dir=File::Spec->catdir($opt_stress_suite_basedir, "suite", $opt_suite);}else{ $test_suite_dir= $opt_stress_suite_basedir;}if (!-d $test_suite_dir){ die <<EOFDirectory '$test_suite_dir' does not exist.Use --suite options to specify proper dir for test suiteEOF}$test_suite_t_path=File::Spec->catdir($test_suite_dir,'t');$test_suite_r_path=File::Spec->catdir($test_suite_dir,'r');foreach my $suite_dir ($test_suite_t_path, $test_suite_r_path){ if (!-d $suite_dir) { die <<EOF;Directory '$suite_dir' does not exist.Please ensure that you specified proper source location for test/result files with --stress-suite-basedir option and name of test suite with --suite optionEOF }}$test_t_path=File::Spec->catdir($opt_stress_basedir,'t');$test_r_path=File::Spec->catdir($opt_stress_basedir,'r');foreach $test_dir ($test_t_path, $test_r_path){ if (-d $test_dir) { if ($opt_cleanup) { #Delete existing 't', 'r', 'r/*' subfolders in $stress_basedir rmtree("$test_dir", 0, 0); print "Cleanup $test_dir\n"; } else { die <<EOF;Directory '$test_dir' already exist. Please ensure that you specified proper location of working dirfor current test run with --stress-basedir option or in case of staleddirectories use --cleanup option to remove onesEOF } } #Create empty 't', 'r' subfolders that will be filled later mkpath("$test_dir", 0, 0777);}if (!defined($opt_stress_tests_file) && !defined($opt_stress_init_file)){ die <<EOF;You should run stress script either with --stress-tests-file or with --stress-init-file otions. See help for details.EOF}if (defined($opt_stress_tests_file)){ if ($opt_stress_tests_file eq '') { #Default location of file with set of tests for current test run $tests_files{client}->{filename}= File::Spec->catfile($opt_stress_suite_basedir, "testslist_client.txt"); } else { $tests_files{client}->{filename}= $opt_stress_tests_file; } if (!-f $tests_files{client}->{filename}) { die <<EOF;File '$tests_files{client}->{filename}' with list of tests not exists. Please ensure that this file exists, readable or specify another one with --stress-tests-file option.EOF }}if (defined($opt_stress_init_file))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -