| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Getopt::Long; |
| 7 |
use File::Basename; |
| 8 |
use IO::Handle; |
| 9 |
|
| 10 |
# Setup to find modules in lib |
| 11 |
use Cwd (); |
| 12 |
use File::Spec (); |
| 13 |
use lib File::Spec->catdir(File::Basename::dirname(Cwd::abs_path __FILE__), |
| 14 |
'../../lib'); |
| 15 |
use My::Config; |
| 16 |
|
| 17 |
my $vardir = $ENV{MYSQLTEST_VARDIR} || "/tmp"; |
| 18 |
my $testfile = $ENV{MYSQLTEST_FILE}; |
| 19 |
if (defined $testfile) { |
| 20 |
# Redirect to var/log/report_<testname>.log based on the mysqltest file name |
| 21 |
my $testname = fileparse($testfile, ".test"); |
| 22 |
my $logfilename = "$vardir/log/report_$testname.log"; |
| 23 |
open(STDOUT, '>', $logfilename) or die "Can't redirect STDOUT: $!"; |
| 24 |
} |
| 25 |
|
| 26 |
STDOUT->autoflush(1); |
| 27 |
|
| 28 |
print "# args: ", join(" ", @ARGV), "\n"; |
| 29 |
|
| 30 |
my @opt_ports; |
| 31 |
my @opt_sockets; |
| 32 |
|
| 33 |
# Populate list of ports and sockets from my.cnf, this will make use of |
| 34 |
# all configured mysqld(s) unless overridden by arguments given by caller. |
| 35 |
my $mycnf = "$vardir/my.cnf"; |
| 36 |
if (-f $mycnf) { |
| 37 |
my $config = My::Config->new($mycnf); |
| 38 |
|
| 39 |
foreach my $mysqld ($config->like("mysqld.")) { |
| 40 |
|
| 41 |
# Don't use binlogging mysqld(s) as client |
| 42 |
if ($mysqld->if_exist('ndb-log-bin')) { |
| 43 |
print "# - skip " . $mysqld->name() . " since it's binlogging\n"; |
| 44 |
next; |
| 45 |
} |
| 46 |
|
| 47 |
# Hardcode second to not be used as well |
| 48 |
if ($mysqld->name() eq 'mysqld.2.crunch') { |
| 49 |
print "# - skip also " . $mysqld->name() . "\n"; |
| 50 |
next; |
| 51 |
} |
| 52 |
|
| 53 |
push(@opt_ports, $mysqld->value('port')); |
| 54 |
push(@opt_sockets, $mysqld->value('socket')); |
| 55 |
} |
| 56 |
} |
| 57 |
my $opt_bench_name; # Benchmark name (aka. "testname") |
| 58 |
my $opt_engine = $ENV{CRUNCH_ENGINE} || "ndbcluster"; |
| 59 |
my $opt_warmup = 0; |
| 60 |
my $opt_tables = 8; |
| 61 |
my $opt_rows = $ENV{CRUNCH_ROWS} || 10000000; # 10M |
| 62 |
my $opt_time = $ENV{CRUNCH_TIME} || 30; # seconds |
| 63 |
my $opt_events = $ENV{CRUNCH_EVENTS} || 0; # => run until time elapsed |
| 64 |
my $opt_threads = $ENV{CRUNCH_THREADS}; # Use specific threads number |
| 65 |
my $opt_report_interval = 1; # seconds |
| 66 |
my $opt_verbosity = 3; # Default of sysbench is 3 |
| 67 |
my $opt_debug; |
| 68 |
# Turn off AUTO_INCREMENT primary key by default |
| 69 |
my $opt_autoinc = $ENV{CRUNCH_AUTOINC} || 0; |
| 70 |
my $opt_secondary_index = 0; |
| 71 |
# Use uniform random numbers by default (this is the default on current |
| 72 |
# sysbench version so only useful when using older sysbench), this changes |
| 73 |
# transactions to be distributed over all rows in the tables and thus reduces |
| 74 |
# deadlocks. This is important since deadlocks in NDB are detected using |
| 75 |
# timeout and synchronously executing clients will be stalled meanwhile, |
| 76 |
# thus reducing throughput |
| 77 |
my $rand_type = "uniform"; |
| 78 |
|
| 79 |
Getopt::Long::Configure("pass_through", "no_auto_abbrev"); |
| 80 |
GetOptions( |
| 81 |
'port=i' => \@opt_ports, |
| 82 |
'socket=s' => \@opt_sockets, |
| 83 |
'benchmark=s' => \$opt_bench_name, |
| 84 |
'engine=s' => \$opt_engine, |
| 85 |
'warmup' => \$opt_warmup, |
| 86 |
'tables=i' => \$opt_tables, |
| 87 |
'rows=i' => \$opt_rows, |
| 88 |
'events=i' => \$opt_events, |
| 89 |
'time=i' => \$opt_time, |
| 90 |
'threads=i' => \$opt_threads, |
| 91 |
'autoinc' => \$opt_autoinc, |
| 92 |
'secondary' => \$opt_secondary_index, |
| 93 |
'verbose=i' => \$opt_verbosity, |
| 94 |
'debug' => \$opt_debug, |
| 95 |
'report-interval=i' => \$opt_report_interval |
| 96 |
) or die "Could not read arguments"; |
| 97 |
|
| 98 |
my $sb = "sysbench"; |
| 99 |
# Check that sysbench exists |
| 100 |
my $bench_check = `$sb --version 2>&1`; |
| 101 |
die "Could not execute '$sb'" if ($? == -1); |
| 102 |
|
| 103 |
# Check parameters |
| 104 |
die "Need --port for MySQL Server" unless @opt_ports > 0; |
| 105 |
die "Need benchmark name" unless $opt_bench_name; |
| 106 |
|
| 107 |
my @args; |
| 108 |
push(@args, "--verbosity=$opt_verbosity") if $opt_verbosity != 3; |
| 109 |
push(@args, "--debug=on") if defined $opt_debug; |
| 110 |
|
| 111 |
# Connection parameters |
| 112 |
push(@args, "--db-driver=mysql"); |
| 113 |
push(@args, "--mysql-storage-engine=$opt_engine"); |
| 114 |
push(@args, "--mysql-host=localhost"); |
| 115 |
push(@args, "--mysql-port=" . join (",", @opt_ports)); |
| 116 |
push(@args, "--mysql-socket=" . join (",", @opt_sockets)) if @opt_sockets > 0; |
| 117 |
push(@args, "--mysql-user=root"); |
| 118 |
push(@args, "--mysql-password="); |
| 119 |
push(@args, "--report-interval=$opt_report_interval"); |
| 120 |
|
| 121 |
# Benchmark parameters |
| 122 |
push(@args, "--tables=$opt_tables"); |
| 123 |
push(@args, "--table_size=$opt_rows"); |
| 124 |
push(@args, "--events=$opt_events"); |
| 125 |
push(@args, "--time=$opt_time"); |
| 126 |
|
| 127 |
# Benchmark tuning |
| 128 |
push(@args, "--auto_inc=$opt_autoinc"); |
| 129 |
push(@args, "--rand-type=$rand_type"); |
| 130 |
push(@args, "--create-secondary=$opt_secondary_index"); |
| 131 |
|
| 132 |
push(@args, @ARGV); |
| 133 |
|
| 134 |
print_header("Prepare"); |
| 135 |
bench($opt_bench_name, "prepare", @args); |
| 136 |
|
| 137 |
if ($opt_warmup) { |
| 138 |
print_header("Warmup"); |
| 139 |
bench($opt_bench_name, "warmup", @args); |
| 140 |
} |
| 141 |
|
| 142 |
foreach my $thread ( threads() ) { |
| 143 |
my $threads = "--threads=$thread"; |
| 144 |
print_header("Run $threads"); |
| 145 |
bench($opt_bench_name, "run", @args, $threads); |
| 146 |
} |
| 147 |
|
| 148 |
print_header("Cleanup"); |
| 149 |
bench($opt_bench_name, "cleanup", @args); |
| 150 |
|
| 151 |
exit(0); |
| 152 |
|
| 153 |
sub threads { |
| 154 |
my @threads; |
| 155 |
# Only one specific thread |
| 156 |
if ($opt_threads) { |
| 157 |
return ($opt_threads); |
| 158 |
} |
| 159 |
|
| 160 |
# Geometric serie of threads |
| 161 |
for (my $i = 1; $i <= 1024; $i = $i*2) { |
| 162 |
push(@threads, $i); |
| 163 |
} |
| 164 |
return @threads; |
| 165 |
} |
| 166 |
|
| 167 |
sub bench { |
| 168 |
print "running: '$sb ", join(' ', @_), "'\n"; |
| 169 |
system($sb, @_) |
| 170 |
and print "command failed: $!\n" and exit(1); |
| 171 |
} |
| 172 |
|
| 173 |
sub print_header { |
| 174 |
my ($what)= @_; |
| 175 |
print "\n"; |
| 176 |
print "# ######################################################\n"; |
| 177 |
print "# $what\n"; |
| 178 |
print "# ######################################################\n"; |
| 179 |
} |