Skip to content

Instantly share code, notes, and snippets.

@kawa-
Created July 24, 2014 14:24
Show Gist options
  • Save kawa-/bde822bc7988464bc10a to your computer and use it in GitHub Desktop.
Save kawa-/bde822bc7988464bc10a to your computer and use it in GitHub Desktop.
有効なuseridの群のファイルを読み込んで、selectのベンチマークをする。非verbose版。
#!/usr/bin/perl -w
# usage : $ ./select.pl -n 100 -r 1000 -d db01 -t t01 -f users.txt
use strict;
use MyBench;
use Getopt::Std;
use Time::HiRes qw(gettimeofday tv_interval);
use DBI;
my %opt;
Getopt::Std::getopt('n:r:h:d:t:f', \%opt);
my $num_kids = $opt{n} or die "Error. -n: number of kids is empty!\n";
my $num_runs = $opt{r} or die "Error. -r: number of runs is empty!\n";
my $db = $opt{d} or die "Error. -d: database name is empty!\n";
my $table = $opt{t} or die "Error. -t: table name is empty!\n";
my $user = "root";
my $pass = "root";
my $port = 3306;
my $host = $opt{h} || "localhost";
my $dsn = "DBI:mysql:$db:$host;port=$port";
my $file = $opt{f} or die "Error. -f: filename is empty! Please do like : -f ./users.txt\n";
# 有効なuseridを@usersに格納+行数を$num_linesに格納
my @users = ();
my $num_lines = 0;
open(my $fh, "<", $file) or die "Error. Cannot open the file.\n";
while(my $line = readline $fh){
chomp $line;
push @users, $line;
$num_lines++;
}
close $fh;
my $callback = sub
{
my $id = shift;
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
my $sth = $dbh->prepare("SELECT f FROM " . $table . " WHERE i = ?");
my $cnt = 0;
my @times = ();
## wait for the parent to HUP me
local $SIG{HUP} = sub { };
sleep 600;
while ($cnt < $num_runs)
{
my $v = $users[int(rand($num_lines))];
## time the query
my $t0 = [gettimeofday];
$sth->execute($v);
=pod
while (my $row = $sth->fetchrow_hashref()){
print $row->{f},"\n";
}
die $sth->errstr if $sth->err;
=cut
my $t1 = tv_interval($t0, [gettimeofday]);
push @times, $t1;
$sth->finish();
$cnt++;
}
## cleanup
$dbh->disconnect();
my @r = ($id, scalar(@times), min(@times), max(@times), avg(@times), tot(@times));
return @r;
};
my @results = MyBench::fork_and_work($num_kids, $callback);
MyBench::compute_results('test', @results);
exit;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment