📄 mysql-stress-test.pl
字号:
@stderr EOF } if (-e $reject_filename) { move_to_logs($env->{reject_logs}, $reject_filename, $reject_file); } if (-e $output_filename) { move_to_logs($env->{screen_logs}, $output_filename, $output_file); } }sub test_loop{ my %client_env=(); my $test_name=""; # KEY for session identification: IP-THREAD_ID $client_env{ip} = shift; $client_env{thread_id} = shift; $client_env{mode} = shift; $client_env{tests_file}=shift; $client_env{test_seq_idx}=0; #Initialize session variables test_init(\%client_env);LOOP: while(!$exiting) { if ($opt_check_tests_file) { #Check if tests_file was modified and reread it in this case read_tests_names($client_env{tests_file}, 0); } { lock($test_counters_lock); if (($limits{loop_count} && $limits{loop_count} <= $test_counters{loop_count}*1) || ($limits{test_count} && $limits{test_count} <= $test_counters{test_count}*1) ) { $exiting=1; next LOOP; } } #Get random file name if (($test_name = get_test(\%client_env)) ne '') { { lock($test_counters_lock); #Save current counters values $client_env{loop_count}=$test_counters{loop_count}; $client_env{test_count}=$test_counters{test_count}; } #Run test and analyze results test_execute(\%client_env, $test_name); print "test_loop[".$limits{loop_count}.":". $limits{test_count}." ". $client_env{loop_count}.":". $client_env{test_count}."]:". " TID ".$client_env{thread_id}. " test: '$test_name' ". " Errors: ".join(" ",@{$client_env{test_status}}),"\n"; print "\n"; } sleep($opt_sleep_time) if($opt_sleep_time); }}sub move_to_logs ($$$){ my $path_to_logs = shift; my $src_file = shift; my $random_filename = shift; my $dst_file = File::Spec->catfile($path_to_logs, $random_filename); move ($src_file, $dst_file) or warn<<EOF;ERROR: move_to_logs: File $src_file cannot be moved to $dst_file: $!EOF}sub copy_test_files (){ if (/\.test$/) { $src_file = $File::Find::name; #print "## $File::Find::topdir - $File::Find::dir - $src_file\n"; if ($File::Find::topdir eq $File::Find::dir && $src_file !~ /SCCS/) { $test_filename = basename($src_file); $dst_file = File::Spec->catfile($test_t_path, $test_filename); copy($src_file, $dst_file) or die "ERROR: copy_test_files: File cannot be copied. $!"; } }}sub copy_result_files (){ if (/\.result$/) { $src_file = $File::Find::name; if ($File::Find::topdir eq $File::Find::dir && $src_file !~ /SCCS/) { $result_filename = basename($src_file) ; $dst_file = File::Spec->catfile($r_folder, $result_filename); copy($src_file, $dst_file) or die "ERROR: copy_result_files: File cannot be copied. $!"; } }}sub get_timestamp{ my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime(); return sprintf("%04d%02d%02d%02d%02d%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);}sub read_tests_names{ my $tests_file = shift; my $force_load = shift; if ($force_load || ( (stat($tests_file->{filename}))[9] != $tests_file->{mtime}) ) { open (TEST, $tests_file->{filename}) || die ("Could not open file <". $tests_file->{filename}."> $!"); @{$tests_file->{data}}= grep {!/^[#\r\n]|^$/} map { s/[\r\n]//g; $_ } <TEST>; close (TEST); $tests_file->{mtime}=(stat(_))[9]; }}sub get_random_test{ my $envt=shift; my $tests= $envt->{tests_file}->{data}; my $random = int(rand(@{$tests})); my $test = $tests->[$random]; return $test;}sub get_next_test{ my $envt=shift; my $test; if (@{$envt->{tests_file}->{data}}) { $test=${$envt->{tests_file}->{data}}[$envt->{test_seq_idx}]; $envt->{test_seq_idx}++; } #If we reach bound of array, reset seq index and increment loop counter if ($envt->{test_seq_idx} == scalar(@{$envt->{tests_file}->{data}})) { $envt->{test_seq_idx}=0; { lock($test_counters_lock); $test_counters{loop_count}++; } } return $test; }sub get_test{ my $envt=shift; { lock($test_counters_lock); $test_counters{test_count}++; } if ($envt->{mode} eq 'seq') { return get_next_test($envt); } elsif ($envt->{mode} eq 'random') { return get_random_test($envt); }}sub stress_log{ my ($log_file, $line)=@_; { open(SLOG,">>$log_file") or warn "Error during opening log file $log_file"; print SLOG $line,"\n"; close(SLOG); }}sub log_session_errors{ my ($env, $test_name) = @_; my $line=''; { lock ($log_file_lock); #header in the begining of log file if (!-e $stress_log_file) { stress_log($stress_log_file, "TestID TID Suite TestFileName Found Errors"); stress_log($stress_log_file, "======================================================="); } $line=sprintf('%6d %3d %10s %20s %s', $env->{test_count}, threads->self->tid, $opt_suite, $test_name, join(",", @{$env->{test_status}})); stress_log($stress_log_file, $line); #stress_log_with_lock($stress_log_file, "\n"); if ($opt_log_error_details) { foreach $severity (sort {$a cmp $b} keys %{$env->{errors}}) { stress_log($stress_log_file, ""); foreach $error (keys %{$env->{errors}->{$severity}}) { if ($error ne 'total') { stress_log($stress_log_file, "$severity: Count:". $env->{errors}->{$severity}->{$error}->[0]. " Error:". $env->{errors}->{$severity}->{$error}->[1]); } } } } }}sub sig_INT_handler{ $SIG{INT}= \&sig_INT_handler; $exiting=1; print STDERR "$$: Got INT signal-------------------------------------------\n";}sub sig_TERM_handler{ $SIG{TERM}= \&sig_TERM_handler; $exiting=1; print STDERR "$$: Got TERM signal\n";}sub usage{ print <<EOF;The MySQL Stress suite Ver $stress_suite_versionmysql-stress-test.pl --stress-basedir=<dir> --stress-suite-basedir=<dir> --server-logs-dir=<dir>--server-host--server-port--server-socket--server-user--server-password--server-logs-dir Directory where all clients session logs will be stored. Usually this is shared directory associated with server that used in testing Required option.--stress-suite-basedir=<dir> Directory that has r/ t/ subfolders with test/result files which will be used for testing. Also by default we are looking in this directory for 'stress-tests.txt' file which contains list of tests. It is possible to specify other location of this file with --stress-tests-file option. Required option.--stress-basedir=<dir> Working directory for this test run. This directory will be used as temporary location for results tracking during testing Required option.--stress-datadir=<dir> Location of data files used which will be used in testing. By default we search for these files in <dir>/data where dir is value of --stress-suite-basedir option.--stress-init-file[=/path/to/file with tests for initialization of stress db] Using of this option allows to perform initialization of database by execution of test files. List of tests will be taken either from specified file or if it omited from default file 'stress-init.txt' located in <--stress-suite-basedir/--suite> dir --stress-tests-file[=/path/to/file with tests] Using of this option allows to run stress test itself. Tests for testing will be taken either from specified file or if it omited from default file 'stress-tests.txt' located in <--stress-suite-basedir/--suite> dir--stress-mode= [random|seq] There are two possible modes which affect order of selecting tests from the list: - in random mode tests will be selected in random order - in seq mode each thread will execute tests in the loop one by one as they specified in the list file. --sleep-time=<time in seconds> Delay between test execution. Could be usefull in continued testsing when one of instance of stress script perform periodical cleanup or recreating of some database objects--threads=#number of threads Define number of threads--check-tests-file Check file with list of tests. If file was modified it will force to reread list of tests. Could be usefull in continued testing for adding/removing tests without script interruption --mysqltest=/path/to/mysqltest binary--verbose--cleanup Force to clean up working directory (specified with --stress-basedir)--log-error-details Enable errors details in the global error log file. (Default: off)--test-count=<number of executed tests before we have to exit>--loop-count=<number of executed loops in sequential mode before we have to exit>--test-duration=<number of seconds that stress test should run>Example of tool usage:perl mysql-stress-test.pl \--stress-suite-basedir=/opt/qa/mysql-test-extra-5.0/mysql-test \--stress-basedir=/opt/qa/test \--server-logs-dir=/opt/qa/logs \--test-count=20 \--stress-tests-file=innodb-tests.txt \--stress-init-file=innodb-init.txt \--threads=5 \--suite=funcs_1 \--mysqltest=/opt/mysql/mysql-5.0/client/mysqltest \--server-user=root \--server-database=test \--cleanup \EOFexit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -