Skip to content

Instantly share code, notes, and snippets.

@kawa-
Created July 24, 2014 15:09
Show Gist options
  • Save kawa-/ffd9b9c2ecd2e9255c4b to your computer and use it in GitHub Desktop.
Save kawa-/ffd9b9c2ecd2e9255c4b to your computer and use it in GitHub Desktop.
insertのベンチマークを取る。mybenchと組み合わせて使う。このgistの前のgenerate_unique_pair_t11.phpでファイルを生成し、それを読み込んで実行。
#!/usr/bin/perl -w
# usage : $ ./insert_bench_t11.pl -n 1 -d db01 -t t11 -f unique_pairs_t11.txt
use strict;
use MyBench;
use Getopt::Std;
use Time::HiRes qw(gettimeofday tv_interval);
use DBI;
use Redis;
my %opt;
Getopt::Std::getopt('n:h:d:t:f', \%opt);
my $num_kids = $opt{n} or die "Error. -n: number of kids 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";
our @pairs = ();
our $num_lines = 0;
open(my $fh, "<", $file) or die "Error. Cannot open the file.\n";
while(my $line = readline $fh){
chomp $line;
my $uid;
my $fid;
my $ts;
($uid, $fid, $ts) = split(/\t+/, $line);
push @pairs, [$uid, $fid, $ts];
$num_lines++;
}
close $fh;
our $num_per_process = int($num_lines / $num_kids);
print "# of kids: " . $num_kids . "\n";
print "# of records: " . $num_lines . "\n";
print "# per process: " . $num_per_process . "\n";
our $redis = Redis->new( server => 'localhost:6379');
$redis->flushall;
$redis->set('pcounter', $num_kids);
my $callback = sub
{
my $id = shift;
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
my $sth = $dbh->prepare("insert into " . $table . " values (?,?)");
my $cnt = 0;
my @times = ();
## wait for the parent to HUP me
local $SIG{HUP} = sub { };
sleep 600;
my $pcounter = $redis->decr('pcounter') + 1;
while ($cnt < $num_per_process) {
my $index = $pcounter * $num_per_process - $cnt - 1;
my @values = ($pairs[$index][0] . "-" . $pairs[$index][1], $pairs[$index][2]);
# debug by print and Redis
# print $values[0] .",". $values[1] .",". $values[2], "\n";
# $redis->lpush($pcounter, $values[0]);
my $t0 = [gettimeofday];
$sth->execute(@values);
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