Created
July 26, 2014 02:49
-
-
Save kawa-/0015569d88bb7ba6139a to your computer and use it in GitHub Desktop.
my benchmark script for inserts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# description: | |
# inserting benchmark | |
# usage: | |
# $ php gen_upt01.php 10 > upt01.txt | |
# $ ./ibt01.pl -n 1 -r 10 -d db01 -t t01 -f upt01.txt | |
use strict; | |
use MyBench; | |
use Getopt::Std; | |
use Time::HiRes qw(gettimeofday tv_interval); | |
use DBI; | |
use Redis; | |
use Data::Dumper; | |
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 $rows_per_kid = $opt{r} or die "Error. -r: the number of rows per kid 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 $filename = $opt{f} or die "Error. -f: filename is empty! Please do like : -f ./users.txt\n"; | |
# ファイルの行数が、$num_kids * $rows_per_kid 以上あるか確認 | |
my $num_lines = 0; | |
open(my $fh, "<", $filename) or die "Error. Cannot open the file.\n"; | |
while(my $line = readline $fh){ | |
$num_lines++; | |
} | |
close $fh; | |
if ($num_kids * $rows_per_kid > $num_lines) { | |
print 'Error. Too small file. Please make bigger file.' . "\n"; | |
print 'The number of lines of the file: ' . $num_lines . "\n"; | |
print 'Total necessary queries: ' . $num_kids * $rows_per_kid . "\n"; | |
die; | |
} | |
print "# of kids: " . $num_kids . "\n"; | |
print "# rows per kid: " . $rows_per_kid . "\n"; | |
# 後にforkするプロセスが、番号を取得できるようにプロセス数を格納 | |
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; | |
# 必要な行数だけ取得 | |
my $from = ($pcounter - 1) * $rows_per_kid; | |
my $to = $pcounter * $rows_per_kid - 1; | |
my @pairs = readFile($filename, $from, $to); | |
while ($cnt < $rows_per_kid) { | |
my ($uid, $fid, $t) = split(/\t+/, $pairs[$cnt]); | |
my @values = ($uid, $fid, $t); | |
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); | |
sub readFile { | |
my ($filename, $from, $to) = @_; | |
my @lines = (); | |
open(my $fh, "<", $filename) or die "Error. Cannot open the file.\n"; | |
my $num_lines = 0; | |
while(my $line = readline $fh){ | |
if ($from <= $num_lines && $num_lines <= $to){ | |
chomp $line; | |
push @lines, $line; | |
} | |
$num_lines++; | |
} | |
close $fh; | |
return @lines; | |
} | |
exit; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment